-4

I have Delegate type and function in string format. how can i add that function in the delegate?

Datatable Events;

/* Having Records

Control Events      MethodName
ControlName     Validating      Leave       Enter
TextBox1        Textbox1_Validating TextBox1_Leave  --

*/

foreach(DataRow Row in Events.Rows)
{
    if (Row["Validating"].ToString() != "")
    {
//Here is I need a help that how can i add Textbox1_Validating event in 
//TextBox1.Validating
    }
}
AliasgerSW
  • 31
  • 1
  • 5
  • 7
    show your code so far – Rajeev Kumar Mar 20 '13 at 10:15
  • 2
    You're trying to call a delegate? Or are you trying to call a method on an object? Or maybe you're trying to raise an event? Be much clearer on **exactly** what you need please. – Mike Perrenoud Mar 20 '13 at 10:16
  • 1
    Instead of infinitely begging users for improvements in their incomplete questions, I'd suggest to simply close them and save rubbish questions on SO. It's not difficult to find resoruces to learn how to ask minimally decent questions! p.s. Quality question cares about English syntax too, so "I", not "i". Don't be lazy! – mloskot Mar 20 '13 at 10:22
  • Actually i am having the event in string form like "TextBox1_Validating" and it was declared too but now i want to add this method to the textbox1.Validating Event on runtime. Then how can i add this "TextBox1_Validating" in textbox1.Validating event. – AliasgerSW Mar 20 '13 at 10:23
  • @AliasgerSW: your comment should be part of the question. – Stephan Mar 20 '13 at 10:40
  • Please check my question once again. I need it urgent. – AliasgerSW Mar 20 '13 at 10:47

3 Answers3

2

Some naive implementation is to use another level of indirection when calling MethodIndo from lambda and assign it to delegate instance of type Func<int, string>. Note that there exist more complex delegate conversions:

void Main()
{
    string methodName = "SomeMethod";

    var methodInfo = this.GetType().GetMethod(methodName);

    Action<string> myFunc = str => methodInfo.Invoke(this, new object[]{str});

    MyEvent += myFunc;

    MyEvent("value");

}
public event Action<string> MyEvent = delegate{}; 
                                    //default so that not to check for null

public void SomeMethod(string val)
{
    Console.WriteLine ("event called with: " + val);
}

prints

event called with: value

Consider another articles about this topic. It really hard to tell what you want to achieve, but I think you can use this implementation as a starting point.

Community
  • 1
  • 1
Ilya Ivanov
  • 23,148
  • 4
  • 64
  • 90
0

A delegate is a type that safely encapsulates a method, similar to a function pointer in C and C++. Unlike C function pointers, delegates are object

for more information you can refer msdn

suhail
  • 360
  • 1
  • 5
  • 15
0

You could use one of the Delegate.CreateDelegate overloads to achieve this. But you need to know the target object (the object, which contains the method you want to get called when the event raises)

An example (pseudo code):

public void TextBox1_Validating(object sender, CancelEventArgs args)
{
    // handle the event
}


public void RegisterEvent()
{
    CancelEventHandler handler = (CancelEventHandler)Delegate.CreateDelegate(
        typeof(CancelEventHandler), 
        this,
        "TextBox1_Validating");
    textBox1.Validating += handler;
}

You can also read this MSDN article, which shows some other ways: How to: Hook Up a Delegate Using Reflection

Stephan
  • 4,187
  • 1
  • 21
  • 33