Cast delegate to Func in C#

后端 未结 8 2043
小蘑菇
小蘑菇 2020-12-01 12:03

I have code:

public delegate int SomeDelegate(int p);

public static int Inc(int p) {
    return p + 1;
}

I can cast Inc to

相关标签:
8条回答
  • 2020-12-01 12:13

    I like examples. Here is my example code:

    class Program
    {
        class A
        {
            public A(D d) { d.Invoke("I'm A!"); }
            public delegate string D(string s);
        }
    
        class B
        {
            public delegate string D(string s);
        }
        static void Main(string[] args)
        {
            //1. Func to delegates 
    
            string F(dynamic s) { Console.WriteLine(s); return s; }
            Func<string, string> f = F;
            //new A(f);//Error CS1503  Argument 1: cannot convert from 'System.Func<string, string>' to 'ConsoleApp3.Program.A.D'  
            new A(new A.D(f));//I'm A!
            new A(x=>f(x));//I'm A!
    
            Func<string, string> f2 = s => { Console.WriteLine(s); return s; };
            //new A(f2);//Same as A(f)
            new A(new A.D(f2));//I'm A!
            new A(x => f2(x));//I'm A!
    
            //You can even convert between delegate types
            new A(new A.D(new B.D(f)));//I'm A!
    
    
    
            //2. delegate to F
    
            A.D d = s => { Console.WriteLine(s); return s; };
    
            Func<string, string> f3 = d.Invoke;
            f3("I'm f3!");//I'm f3!
            Func<string, string> f4 = new Func<string, string>(d);
            f4("I'm f4!");//I'm f4!
    
    
            Console.ReadLine();
        }
    }
    

    The output is:

    0 讨论(0)
  • 2020-12-01 12:17

    It is the same kind of problem as this:

    public delegate int SomeDelegate1(int p);
    public delegate int SomeDelegate2(int p);
    ...
      SomeDelegate1 a = new SomeDelegate1(Inc);
      SomeDelegate2 b = (SomeDelegate2)a;  // CS0030
    

    which is the same kind of problem as:

    public class A { int prop { get; set; } }
    public class B { int prop { get; set; } }
    ...
      A obja = new A();
      B objb = (B)obja;  // CS0029
    

    Objects cannot be casted from one type to an unrelated other type, even though the types are otherwise completely compatible. For lack of a better term: an object has type identity that it carries along at runtime. That identity cannot be changed after the object is created. The visible manifestation of this identity is Object.GetType().

    0 讨论(0)
  • 2020-12-01 12:21

    There's a much simpler way to do it, which all the other answers have missed:

    Func<int, int> c = a.Invoke; 
    

    See this blog post for more info.

    0 讨论(0)
  • 2020-12-01 12:21

    This works (in C# 4.0 at least - not tried in earlier versions):

    SomeDelegate a = Inc;
    Func<int, int> c = new Func<int, int>(a);
    

    If you look at the IL, this compiles into exactly the same code as Winston's answer. Here's the IL for the second line of what I just wrote:

    ldloc.0
    ldftn      instance int32 ConsoleApplication1.Program/SomeDelegate::Invoke(int32)
    newobj     instance void class [mscorlib]System.Func`2<int32,int32>::.ctor(object, native int)
    

    And that's also precisely what you see if you assign a.Invoke into c.

    Incidentally, although Diego's solution is more efficient, in that the resulting delegate refers directly to the underlying method rather than going through the other delegate, it doesn't handle multicast delegates correctly. Winston's solution does, because it just defers completely to the other delegate. If you want a direct solution that also handles delegates with multiple targets, you need something a little more complex:

    public static TResult DuplicateDelegateAs<TResult>(MulticastDelegate source)
    {
        Delegate result = null;
        foreach (Delegate sourceItem in source.GetInvocationList())
        {
            var copy = Delegate.CreateDelegate(
                typeof(TResult), sourceItem.Target, sourceItem.Method);
            result = Delegate.Combine(result, copy);
        }
    
        return (TResult) (object) result;
    }
    

    This does the right thing for delegates with a single target by the way—it will end up producing just a single delegate of the target type that refers directly to whatever method (and where applicable, object) the input delegate referred to.

    0 讨论(0)
  • 2020-12-01 12:28
    SomeDelegate a = Inc;
    Func<int, int> b = Inc;
    

    is short for

    SomeDelegate a = new SomeDelegate(Inc); // no cast here
    Func<int, int> b = new Func<int, int>(Inc);
    

    You can't cast an instance of SomeDelegate to a Func<int, int> for the same reason you can't cast a string to a Dictionary<int, int> -- they're different types.

    This works:

    Func<int, int> c = x => a(x);
    

    which is syntactic sugar for

    class MyLambda
    {
       SomeDelegate a;
       public MyLambda(SomeDelegate a) { this.a = a; }
       public int Invoke(int x) { return this.a(x); }
    }
    
    Func<int, int> c = new Func<int, int>(new MyLambda(a).Invoke);
    
    0 讨论(0)
  • 2020-12-01 12:29

    Try this:

    Func<int, int> c = (Func<int, int>)Delegate.CreateDelegate(typeof(Func<int, int>), 
                                                               b.Target,
                                                               b.Method);
    
    0 讨论(0)
提交回复
热议问题