473,390 Members | 1,348 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,390 software developers and data experts.

Dynamic assign delegate function?

Hi

I have an odd question: is it possible to have a delegate that recieves
a methodname dynamically?

Like this: aClassObj.OnGo += new ANamespace.Invoke(fName);
where fName would be a string with the function name;

Maybe this is totally conceptually wrong, but right now I'm nont sure
which way to go.

I have a timer that trigger an event OnGo that fires a function within
a class and now I'd like to have a number of functions that can be
called and is decided in an xml document. Possibly a stupid idea, but i
don't know how to proceed.

TopObj->holds instance of callerObj
callerObj-> creates a instance of delegateObj
callerObj -> assigns one of its own functions to the event
delegateObj.OnGo

If it doesn't make any sense tell me to bury it and rewrite...:-(

Example on what I would like to do:
namespace ANamespace{
public delegate void Invoke(string ObjId);
Class A{
public event Invoke OnGo;
.....
}

Class B {

A aClassObj;
...
public B()
{
aClassObj = new A();
}
public void functionAssign(string fName)
{
aClassObj.OnGo += new ANamespace.Invoke(fName);
}

public void callerFunction(string ID){...}
}

}

Jul 21 '05 #1
3 2170
>I have an odd question: is it possible to have a delegate that recieves
a methodname dynamically?

Like this: aClassObj.OnGo += new ANamespace.Invoke(fName);
where fName would be a string with the function name;


Yes, with System.Delegate.CreateDelegate().

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Jul 21 '05 #2
Hi

Thanks for answering...but i have a question here:

Do you mean that you set a delgate on a delegate?

example:
public void callerFunction1(string ID){...}
public void callerFunction2(string ID){...}
public void callerFunction3(string ID){...}

program calls ->
functionAssign(callerFunction2) ;

/erik

Jul 21 '05 #3
Hi again Mattias,

Ignore my last...i just read up again on my delegates and Iunderstand
what you mean now. It works just perfectly...

I'm a bit slow :-)

/Erik

Jul 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Nate | last post by:
Help please, I need a genius!!! I have an application that loads assemblies at runtime. These assemblies have many classes (objects) and some do have several delegates. I want to be able to...
2
by: JC | last post by:
Hello, I'm looking for examples of how to make dynamic creation of code and dynamic execution of the same code. I want to do some code dynamically and then I want to use it to get some results....
3
by: lallous | last post by:
Hello I'm new to C# and wonder if it is possible, as it is possible in C++, to dynamically load a DLL and use it in C#. The dll is written in C++ (unmanaged) and has the following export: ...
1
by: Nate | last post by:
Help please, I need a genius!!! I have an application that loads assemblies at runtime. These assemblies have many classes (objects) and some do have several delegates. I want to be able to...
3
by: ogled | last post by:
Hi I have an odd question: is it possible to have a delegate that recieves a methodname dynamically? Like this: aClassObj.OnGo += new ANamespace.Invoke(fName); where fName would be a string...
4
by: Dave | last post by:
Is there a way dynamically execute a a piece of code at run-time in C#? Such as representing a custom method "UserInfo.GetEmail()" as a string and get 'evaluated" during execution and return it's...
2
by: Luis Arvayo | last post by:
Hi, In c#, I need to dynamically create types at runtime that will consist of the following: - inherits from a given interface - will have a constructor with an int argument
0
by: pjr | last post by:
Using VS2005, I dynamically create an event delegate. Code follows question. My method gets the event's parameters and passes them onto a common event handler. My delegate gets called when expected...
3
by: vainstah | last post by:
Hello Guys and Galls, To start off, I have reached the solution I was looking for, but I would like comments and feedback on the solution I have reached and tips/tricks on making it more elegant....
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.