471,337 Members | 867 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

C#, Dynamic Functions, Threads

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
3 1660
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
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
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.

Similar topics

1 post views Thread by George Adams | last post: by
4 posts views Thread by Leslaw Bieniasz | last post: by
6 posts views Thread by Materialised | last post: by
6 posts views Thread by junky_fellow | last post: by
23 posts views Thread by Russ Chinoy | last post: by
reply views Thread by Pascal Costanza | last post: by
14 posts views Thread by vivek | last post: by
3 posts views Thread by =?Utf-8?B?QWhtYWQ=?= | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.