namespace ConsoleApplication
{
class Program
{
public static void s_TestEvent(string msg)
{
Console.WriteLine(msg);
}
static void Main(string[] args)
{
Assembly ass = Assembly.Load("ConsoleApplication");
object structInstance = ass.CreateInstance("ConsoleApplication.PrivateField");
Type structType = structInstance.GetType();
EventInfo eventInfo = structType.GetEvent("TestEvent", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static);
Type tDelegate = eventInfo.EventHandlerType;
MethodInfo miHandler = typeof(Program).GetMethod("s_TestEvent", BindingFlags.Public | BindingFlags.Static);
Delegate d = Delegate.CreateDelegate(tDelegate, miHandler);
MethodInfo miAddHandler = eventInfo.GetAddMethod();
object[] addHandlerArgs = { d };
miAddHandler.Invoke(structInstance, addHandlerArgs);
FieldInfo _Field = structType.GetField("TestEvent", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static);
if (_Field != null)
{
object _FieldValue = _Field.GetValue(structInstance);
if (_FieldValue != null && _FieldValue is Delegate)
{
Delegate _ObjectDelegate = _FieldValue as Delegate;
_ObjectDelegate.DynamicInvoke(new object[] {"Sandy" });
}
}
}
}
}