none
MethodInfo.CreateDelegate example ?

Answers

  • Try below code:

        partial class MainPage
        {
            public MainPage()
            {
                InitializeComponent();
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                A a = new A();
    
                TypeInfo t = this.GetType().GetTypeInfo();
                MethodInfo m = t.GetDeclaredMethod("a_AeventHanlder");
                Delegate d = m.CreateDelegate(typeof(dele), this);
                a.A_event += (dele)d;
            }
    
            string a_AeventHanlder(string p) { return ""; }
        }
    
    
    public delegate string dele(string p);
    public class A
    {
    
        public event dele A_event;
    }
    

     

    Sincerely,

     


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Monday, January 09, 2012 8:56 AM

All replies

  • Can you give us a code example of CreateDelegate using MethodInfo ?

    I need to create a Delegate for an Event Handler.
    Thursday, January 05, 2012 3:02 PM
  • Try below code:

        partial class MainPage
        {
            public MainPage()
            {
                InitializeComponent();
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                A a = new A();
    
                TypeInfo t = this.GetType().GetTypeInfo();
                MethodInfo m = t.GetDeclaredMethod("a_AeventHanlder");
                Delegate d = m.CreateDelegate(typeof(dele), this);
                a.A_event += (dele)d;
            }
    
            string a_AeventHanlder(string p) { return ""; }
        }
    
    
    public delegate string dele(string p);
    public class A
    {
    
        public event dele A_event;
    }
    

     

    Sincerely,

     


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Monday, January 09, 2012 8:56 AM
  • OK. Thanks !
    Monday, January 09, 2012 11:21 AM