473,386 Members | 1,832 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,386 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 1736
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.