C#, Dynamic Functions, Threads

A

Ahmad

Hi,

Is it possible to do something like the the following:

IBaseClass objClass = new LoadClassFromDLL( "Module.DLL");
ThreadStart objThreadStart= new
(objClass.GetFunctionFromName("GetString"))

Thread objThread = new Thread (objThreadStart)
objThread.Start();

Bearing in mind the function 'GetFunctionFromName' doesn't exist and I have
no idea how to implement it.

Thanks in Advance.

Regards,

Ahmad
 
C

Ciaran O''Donnell

Something along the lines of:

interface IBaseType
{
public void GetString();
}


void RunThread()
{

IBaseType obj = Activator.CreateInstance("c:\\myassembly\file.dll",
"MyAssembly.MyType");
Thread t = new Thread(new ThreadStart(obj.GetString()));
t.Start();
}
 
C

Ciaran O''Donnell

That is the closest thing to what you wrote that makes sense to me. It seems
like you either want to make an object from a class in the assembly and run a
method on it in a new thread or you want to extract a class and run a static
method on it in a new thread. The last post was the first.
This is the second incase thats what your after. I'm havent tested it but
its there of there abouts. You might need to have a little play to get the
GetMethod to find the right method etc.

Assembly ass = Assembly.LoadFrom("c:\\myassembly\file.dll");
Type mytype = ass.GetType("MyAssembly.MyType");
MethodInfo method = mytype.GetMethod("StaticMethodName", BindingFlags.Static
| BindingFlags.Public);
Thread t = new Thread(new ThreadStart(delegate() {
method.Invoke(null, new object[] { });
}));
t.Start();
 
A

Ahmad

I want the function to be dynamically loaded. GetString was an example I was
using but I placed it quoted deliberately
 
C

Ciaran O''Donnell

Is it static or Instance. You can change this to dynamic function name by
taking the Type.GetMethod bit from my second reply.
e.g

IBaseType obj = Activator.CreateInstance("c:\\myassembly\file.dll",
"MyAssembly.MyType");
Type mytype = obj.GetType();
MethodInfo method = mytype.GetMethod("MethodName", BindingFlags.Instance
| BindingFlags.Public);
Thread t = new Thread(new ThreadStart(delegate()
{
method.Invoke(obj, new object[] {/* put parameters here */ });
}));
t.Start();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top