473,231 Members | 1,807 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 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 1730
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: George Adams | last post by:
I like the idea of compiling DSO modules for Apache. It allows me to turn on or off things we may or may not need at a given time (like mod_ssl, mod_auth_mysql, mod_auth_ldap, etc.) and also...
2
by: Manisha | last post by:
Hi, I am creating a C++ dll which is used to process data passed to it through one of its exported functions. It should be able to process 160 simultaneous requests. For this reason, I have...
4
by: Leslaw Bieniasz | last post by:
Cracow, 20.10.2004 Hello, As far as I understand, the generic programming basically consists in using templates for achieving a static polymorphism of the various code fragments, and their...
0
by: Mike Meyer | last post by:
The recent thread on threads caused me to reread the formal definition of SCOOP, and I noticed something I hadn't really impressed me the first time around: it's using staticly checkable rules to...
6
by: Materialised | last post by:
Hi Everyone, I apologise if this is covered in the FAQ, I did look, but nothing actually stood out to me as being relative to my subject. I want to create a 2 dimensional array, a 'array of...
6
by: junky_fellow | last post by:
what are reentrant functions? What are the characteristics of a reentrant code ? what things should be kept in mind while writing a reentrant code ?
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
0
by: Pascal Costanza | last post by:
Dynamic Languages Day @ Vrije Universiteit Brussel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Monday, February 13, 2006, VUB Campus Etterbeek The VUB (Programming Technology Lab,...
14
by: vivek | last post by:
i have some doubts on dynamic memory allocation and stacks and heaps where is the dynamic memory allocation used? in function calls there are some counters like "i" in the below function. Is...
3
by: =?Utf-8?B?QWhtYWQ=?= | last post by:
Hi, Is it possible to do something like the the following: IBaseClass objClass = new LoadClassFromDLL( "Module.DLL"); ThreadStart objThreadStart= new...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.