By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,420 Members | 1,417 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,420 IT Pros & Developers. It's quick & easy.

C#, Dynamic Functions, Threads

P: n/a
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

Jul 10 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Jul 10, 2:28*pm, Ahmad <Ah...@discussions.microsoft.comwrote:
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
Jul 10 '08 #2

P: n/a
Hi,

Originally I had:

IModule objModule =
ModuleManager.GetInstance(@"C:\projects\bin\librar y\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\librar y\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

"Jon Skeet [C# MVP]" wrote:
On Jul 10, 2:28 pm, Ahmad <Ah...@discussions.microsoft.comwrote:
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
Jul 10 '08 #3

P: n/a
Ahmad <Ah***@discussions.microsoft.comwrote:
IModule objModule =
ModuleManager.GetInstance(@"C:\projects\bin\librar y\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\librar y\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); }

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jul 10 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.