hi,
i have a program that used reflection to execute methods. now i want to
execute the reflected method on a new thread but cant figure out how or if it
can be done. take the below code, for instance.
// Declare the types.
string AssemblyToRun = "C:\MyAss";
string ClassToRun = "MyClass"
string MethodToRun = "MyMethod"
// Setup reflection.
Assembly MyAssemblyToRun = Assembly.LoadFr om(@AssemblyToR un + ".dll");
Type MyClassToRun = MyAssemblyToRun .GetType(Assemb lyToRun + "." +
ClassToRun, true, true);
MethodInfo MyMethodToRun = MyClassToRun.Ge tMethod(MethodT oRun);
// Create an instance of the class.
object MyClassInstance = Activator.Creat eInstance(MyCla ssToRun);
// Execute the method.
object RetVal = MyMethodToRun.I nvoke(MyClassIn stance, BindingFlags.De fault,
null, null, null);
simple right. ok. all the above works fine. now if i want to execute the
method on a new thread I would add something like:
Thread MyNewThread = new Thread(new ThreadStart(Cla ssToRun + "."
MethodToRun));
MyNewThread.Sta rt();
that does not work because "ThreadStar t" is a delegate that wants a "void"
method and all i can pass it is a class instance or the method name as a
string. it also wont work when i use the class instance or the MyMethodToRun
instance of the reflected method.
how can i use reflection and multithreading together? is this possibble? is
there a way to invoke a reflected method on a new thread?
HELP !!!
i have a program that used reflection to execute methods. now i want to
execute the reflected method on a new thread but cant figure out how or if it
can be done. take the below code, for instance.
// Declare the types.
string AssemblyToRun = "C:\MyAss";
string ClassToRun = "MyClass"
string MethodToRun = "MyMethod"
// Setup reflection.
Assembly MyAssemblyToRun = Assembly.LoadFr om(@AssemblyToR un + ".dll");
Type MyClassToRun = MyAssemblyToRun .GetType(Assemb lyToRun + "." +
ClassToRun, true, true);
MethodInfo MyMethodToRun = MyClassToRun.Ge tMethod(MethodT oRun);
// Create an instance of the class.
object MyClassInstance = Activator.Creat eInstance(MyCla ssToRun);
// Execute the method.
object RetVal = MyMethodToRun.I nvoke(MyClassIn stance, BindingFlags.De fault,
null, null, null);
simple right. ok. all the above works fine. now if i want to execute the
method on a new thread I would add something like:
Thread MyNewThread = new Thread(new ThreadStart(Cla ssToRun + "."
MethodToRun));
MyNewThread.Sta rt();
that does not work because "ThreadStar t" is a delegate that wants a "void"
method and all i can pass it is a class instance or the method name as a
string. it also wont work when i use the class instance or the MyMethodToRun
instance of the reflected method.
how can i use reflection and multithreading together? is this possibble? is
there a way to invoke a reflected method on a new thread?
HELP !!!
Comment