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 actually exist,
and I don't know how to implement it (I've sorted out the 'LoadClassFromDLL'
function).

Thanks in Advance.

Regards,

Ahmad
 
J

Jon Skeet [C# MVP]

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 actually exist,
and I don't know how to implement it (I've sorted out the 'LoadClassFromDLL'
function).

Yes, that's basically possible. Look at Type.GetMethod and
MethodInfo.Invoke. If you're still stuck after a bit of looking, let
me know and I'll write up a short example program. (I'd do it now, but
I really ought to get on with some work...)

Jon
 
A

Ahmad

Hi,

Originally I had:

IModule objModule =
ModuleManager.GetInstance(@"C:\projects\bin\library\Module.dll");
Type objType = objModule.GetType();
MethodInfo objMethodInfo = objType.GetMethod("GetString");
Thread objThread = new Thread(objMethodInfo);

but that doesn't work because the Thread Constructor is expecting a method
rather than an object (in this case MethodInfo). Even using:

IModule objModule =
ModuleManager.GetInstance(@"C:\projects\bin\library\Module.dll");

Type objType = objModule.GetType();


MethodInfo objMethodInfo = objType.GetMethod("GetString");
ThreadStart objThreadStart= new (objMethodInfo)
Thread objThread = new Thread(objThreadStart);

won't work because [yet again] ThreadStart Constructur expecting a method
rather than an object.

Thanks in Advance.

Regards,

Ahmad
 
J

Jon Skeet [C# MVP]

Ahmad said:
IModule objModule =
ModuleManager.GetInstance(@"C:\projects\bin\library\Module.dll");
Type objType = objModule.GetType();
MethodInfo objMethodInfo = objType.GetMethod("GetString");
Thread objThread = new Thread(objMethodInfo);

but that doesn't work because the Thread Constructor is expecting a method
rather than an object (in this case MethodInfo). Even using:

IModule objModule =
ModuleManager.GetInstance(@"C:\projects\bin\library\Module.dll");

Type objType = objModule.GetType();


MethodInfo objMethodInfo = objType.GetMethod("GetString");
ThreadStart objThreadStart= new (objMethodInfo)
Thread objThread = new Thread(objThreadStart);

won't work because [yet again] ThreadStart Constructur expecting a method
rather than an object.

Well, it's expecting a delegate.

You could change it to:

ThreadStart threadStart = delegate { objMethodInfo.Invoke(null); }
 

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