locked
pass any function to Delegate type RRS feed

  • Question

  • Hello, there,

    I have a function like   public void MyMethod(Delegate myDelagate) { .... }

    Is that possible to pass any function (class instance's method and static method) to this function? For example, how can i do:

    MyMethod(Convert.ToInt32)

    thanks.

    Thursday, June 10, 2010 8:25 PM

Answers

  • Hi,

    Thanks for your post.

    I afraid if it's possible because delegates are strongly dependant to their signatures. however, you can near it using 'Generic Delegates' but I afraid if it's possible completely as you wish.

    Anyway, for functions which have same signatures, you can do something like this:

    using System;
    namespace ConsoleApplication2
    {
      class Program
      {
        delegate int dlg(string s);
        static void Main(string[] args)
        {
          dlg dlg1 = new dlg(Convert.ToInt32);
          int i1 = MyMethod(dlg1, "2010");
    
          dlg dlg2 = new dlg(int.Parse);
          int i2 = MyMethod(dlg1, "2010");
    
          Console.WriteLine("{0} = {1}", i1, i2);
          Console.ReadKey();
        }
        static int MyMethod(dlg myDelagate, string s)
        {
          return myDelagate(s);
        }
      }
    }

    Hope this helps.

    Sincerely,

    Yasser


    LEARN HOW WPF IS FLEXIBLE IN PRESENTATION DURING A QUICK SIMPLE WALKTHROUGH:
    Walkthrough: Displaying multi column ComboBox by using Windows Presentation Foundation (WPF) data templating
    • Marked as answer by Tao Liu Thursday, June 10, 2010 9:38 PM
    Thursday, June 10, 2010 8:51 PM
  • There exist an implicit conversion from a method to any delegate with the same signature. Since Delegate, as the base class of all delegates, doesn't have a signature attached to it, there isn't that implicit conversion.

    There exist, however, an implicit conversion from any delegate to the Delegate class. So, you only need to cast the method to a specific delegate.

    For example, you can do:

    MyMethod((Func<string,int>)Convert.ToInt32);

    • Marked as answer by Tao Liu Thursday, June 10, 2010 9:37 PM
    Thursday, June 10, 2010 9:23 PM

All replies

  • Hi,

    Thanks for your post.

    I afraid if it's possible because delegates are strongly dependant to their signatures. however, you can near it using 'Generic Delegates' but I afraid if it's possible completely as you wish.

    Anyway, for functions which have same signatures, you can do something like this:

    using System;
    namespace ConsoleApplication2
    {
      class Program
      {
        delegate int dlg(string s);
        static void Main(string[] args)
        {
          dlg dlg1 = new dlg(Convert.ToInt32);
          int i1 = MyMethod(dlg1, "2010");
    
          dlg dlg2 = new dlg(int.Parse);
          int i2 = MyMethod(dlg1, "2010");
    
          Console.WriteLine("{0} = {1}", i1, i2);
          Console.ReadKey();
        }
        static int MyMethod(dlg myDelagate, string s)
        {
          return myDelagate(s);
        }
      }
    }

    Hope this helps.

    Sincerely,

    Yasser


    LEARN HOW WPF IS FLEXIBLE IN PRESENTATION DURING A QUICK SIMPLE WALKTHROUGH:
    Walkthrough: Displaying multi column ComboBox by using Windows Presentation Foundation (WPF) data templating
    • Marked as answer by Tao Liu Thursday, June 10, 2010 9:38 PM
    Thursday, June 10, 2010 8:51 PM
  • There exist an implicit conversion from a method to any delegate with the same signature. Since Delegate, as the base class of all delegates, doesn't have a signature attached to it, there isn't that implicit conversion.

    There exist, however, an implicit conversion from any delegate to the Delegate class. So, you only need to cast the method to a specific delegate.

    For example, you can do:

    MyMethod((Func<string,int>)Convert.ToInt32);

    • Marked as answer by Tao Liu Thursday, June 10, 2010 9:37 PM
    Thursday, June 10, 2010 9:23 PM