I want to give the consumer of my event the possibility to modify a buffer through EventArgs
, but I can't pin the solution properly. I'm well prepared in C/C++ but rather inexperienced in C#.
My Event definition is:
public class ResponseEventArgs : EventArgs
{
public byte[] Buffer { get; set; }
public ResponseEventArgs(byte[] buffer)
{
this.Buffer = buffer;
}
}
public delegate void ResponseEventHandler(object sender, ResponseEventArgs e);
public event ResponseEventHandler Response;
I raise the event with:
byte[] buffer = new byte[BUFSIZE];
Response(this, new ResponseEventArgs(buffer));
A sample event handler, where I convert to UTF8, replace, and back to byte, e.g:
void Response_Test(object sender, ResponseEventArgs e)
{
string stringBuf = System.Text.Encoding.UTF8.GetString(e.Buffer);
stringBuf = stringBuf.Replace("A", "B");
e.Buffer = new byte[stringBuf.Length * sizeof(char)];
System.Buffer.BlockCopy(stringBuf.ToCharArray(), 0, e.Buffer, 0,
e.Buffer.Length);
}
When returning from the event, the byte buffer is still with the old content.