G
Guest
I am trying to dynamically instantiate a class and subscribe to an event that
it throws. I am have no luck finding a proper code sample or documentation
on doing this. Here is what I am attempting and my questions are inline with
the code snippet.
// Load Assembly
Assembly testAssembly =
Assembly.LoadFile@"MyClasses.dll");
// Get the type i am interested in
Type testType =
testAssembly.GetType("MyType");
// Instantiate the object
object obj =
Activator.CreateInstance(testType, args);
// Why does this fail with invalid cast exception??
MyType mytype = (MyType)obj
// Here I am getting the EventInfo
EventInfo ei = testType.GetEvent("_TestCompleted");
// Create the delegate to handle the event fired by testType
Delegate del = new tests.TestCompletedHandler(OnTestComplete);
// Now I want to subcribe to this even
// This fails and i have tried numerous syntax's etc.
// I need clarity on how to subscribt to this event properly.
//
ei.AddEventHandler(??????)
it throws. I am have no luck finding a proper code sample or documentation
on doing this. Here is what I am attempting and my questions are inline with
the code snippet.
// Load Assembly
Assembly testAssembly =
Assembly.LoadFile@"MyClasses.dll");
// Get the type i am interested in
Type testType =
testAssembly.GetType("MyType");
// Instantiate the object
object obj =
Activator.CreateInstance(testType, args);
// Why does this fail with invalid cast exception??
MyType mytype = (MyType)obj
// Here I am getting the EventInfo
EventInfo ei = testType.GetEvent("_TestCompleted");
// Create the delegate to handle the event fired by testType
Delegate del = new tests.TestCompletedHandler(OnTestComplete);
// Now I want to subcribe to this even
// This fails and i have tried numerous syntax's etc.
// I need clarity on how to subscribt to this event properly.
//
ei.AddEventHandler(??????)