Dynamic assign delegate function?

  • Thread starter Thread starter ogled
  • Start date Start date
O

ogled

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){...}
}

}
 
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
 
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
 
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
 
Back
Top