.NET4:如何将动态绑定应用于具有返回的匿名委托? class-MyClass { 公共事件行动; 公共活动职能; } 班级计划 { 静态void Main(字符串[]参数) { MyClass mc=新的MyClass(); ///我需要处理任意事件。 ///但我看不到在运行时创建匿名委托的方法 ///具有任意返回和参数。因此我选择 ///使用不同的参数创建多个“签名” ///数字(最多10个)和不同的返回(操作或 ///虽然操作可以很好地工作,但是函数不能。 Action someAction=delegate(动态p1){}; Func someFunc=委托(动态p1){return 42;}; //嗯 mc.OnAction+=某个动作; //错误:“无法隐式转换类型'System.Func' //至“System.Func” mc.OnFunc+=someFunc; //它也不能以这种方式工作(相同的错误消息): //动态someFunc=newfunc((动态n1)=>{return 42;}); //让我们试试另一种方法 // 1: //无法将匿名方法转换为委托类型“System.Func” //因为参数类型与委托参数类型不匹配。 //2(更有趣的是): //参数1声明为“动态”类型,但应为“字符串”。 mc.OnFunc+=委托(动态p1){return 42;}; } }

.NET4:如何将动态绑定应用于具有返回的匿名委托? class-MyClass { 公共事件行动; 公共活动职能; } 班级计划 { 静态void Main(字符串[]参数) { MyClass mc=新的MyClass(); ///我需要处理任意事件。 ///但我看不到在运行时创建匿名委托的方法 ///具有任意返回和参数。因此我选择 ///使用不同的参数创建多个“签名” ///数字(最多10个)和不同的返回(操作或 ///虽然操作可以很好地工作,但是函数不能。 Action someAction=delegate(动态p1){}; Func someFunc=委托(动态p1){return 42;}; //嗯 mc.OnAction+=某个动作; //错误:“无法隐式转换类型'System.Func' //至“System.Func” mc.OnFunc+=someFunc; //它也不能以这种方式工作(相同的错误消息): //动态someFunc=newfunc((动态n1)=>{return 42;}); //让我们试试另一种方法 // 1: //无法将匿名方法转换为委托类型“System.Func” //因为参数类型与委托参数类型不匹配。 //2(更有趣的是): //参数1声明为“动态”类型,但应为“字符串”。 mc.OnFunc+=委托(动态p1){return 42;}; } },.net,anonymous,dynamic-binding,.net,Anonymous,Dynamic Binding,为什么它适用于动作而不适用于函数? 换句话说,我只是想知道为什么会采取行动→ 操作正常,而Func→ Func不可用。谢谢。您的操作和Func定义之间的唯一区别是操作没有返回类型。如果您从以下位置更改代码: class MyClass { public event Action<string> OnAction; public event Func<string, int> OnFunc; } class Program { static voi

为什么它适用于动作而不适用于函数?
换句话说,我只是想知道为什么
会采取行动→ 操作
正常,而
Func→ Func
不可用。谢谢。

您的操作和Func定义之间的唯一区别是操作没有返回类型。如果您从以下位置更改代码:

class MyClass
{
    public event Action<string> OnAction;
    public event Func<string, int> OnFunc;
}

class Program
{
    static void Main(string[] args)
    {
        MyClass mc = new MyClass();

        /// I need to handle arbitrary events.
        /// But I see no way to create anonymous delegates in runtime
        /// with arbitrary returns and parameters. So I choosed to
        /// create multiple “signatures” with different parameter
        /// number (up to 10) and different returns (either the Action or
        /// Func). While Actions<> work pretty well, the Funcs<> do not.
        Action<dynamic> someAction = delegate(dynamic p1) { };
        Func<dynamic, dynamic> someFunc = delegate(dynamic p1) { return 42;};

        // OK
        mc.OnAction += someAction;

        // Error: “Cannot implicitly convert type 'System.Func<dynamic,dynamic>'
        // to 'System.Func<string,int>'”
        mc.OnFunc += someFunc;

        // It doesn't work this way as well (the same error message):
        // dynamic someFunc = new Func<dynamic, dynamic>((dynamic n1) => { return 42; });

        // Let's try another way
        // 1:
        // Cannot convert anonymous method to delegate type 'System.Func<string,int>'
        // because the parameter types do not match the delegate parameter types.
        // 2 (even more funny):
        // Parameter 1 is declared as type 'dynamic' but should be 'string'.
        mc.OnFunc += delegate(dynamic p1) { return 42; };
    }
}
mc.OnFunc+=新函数(someFunc);//+=someFunc;

mc.OnFunc+=新函数(someFunc);//+=someFunc;

它可以工作。

操作和Func定义之间的唯一区别是操作没有返回类型。如果您从以下位置更改代码:

class MyClass
{
    public event Action<string> OnAction;
    public event Func<string, int> OnFunc;
}

class Program
{
    static void Main(string[] args)
    {
        MyClass mc = new MyClass();

        /// I need to handle arbitrary events.
        /// But I see no way to create anonymous delegates in runtime
        /// with arbitrary returns and parameters. So I choosed to
        /// create multiple “signatures” with different parameter
        /// number (up to 10) and different returns (either the Action or
        /// Func). While Actions<> work pretty well, the Funcs<> do not.
        Action<dynamic> someAction = delegate(dynamic p1) { };
        Func<dynamic, dynamic> someFunc = delegate(dynamic p1) { return 42;};

        // OK
        mc.OnAction += someAction;

        // Error: “Cannot implicitly convert type 'System.Func<dynamic,dynamic>'
        // to 'System.Func<string,int>'”
        mc.OnFunc += someFunc;

        // It doesn't work this way as well (the same error message):
        // dynamic someFunc = new Func<dynamic, dynamic>((dynamic n1) => { return 42; });

        // Let's try another way
        // 1:
        // Cannot convert anonymous method to delegate type 'System.Func<string,int>'
        // because the parameter types do not match the delegate parameter types.
        // 2 (even more funny):
        // Parameter 1 is declared as type 'dynamic' but should be 'string'.
        mc.OnFunc += delegate(dynamic p1) { return 42; };
    }
}
mc.OnFunc+=新函数(someFunc);//+=someFunc;

mc.OnFunc+=新函数(someFunc);//+=someFunc;
它可以工作。

如果C#期望一个返回类型为
int
的委托,则声明为返回
dynamic
的委托是不兼容的。您的委托被声明为
Func
,而C#是强类型的。它不在乎你的函数是否真的返回一个int并接受一个字符串;它仍然声明为
Func

<> P>代码不编译的原因是显而易见的,如果你考虑这个例子:

mc.OnFunc += new Func<dynamic, int>(someFunc);// += someFunc;
Func myFunc=delegate(字符串foo){return 42;};
int result=myFunc(“Foo”);
//如果允许的话。。。
Func otherFunc=委托(字符串foo){return 42;};
myFunc=其他func;
结果=myFunc(“Foo”);
//那么这也是允许的,但将是一个运行时错误。
otherFunc=委托(字符串foo){返回“Bar”};//有效的
myFunc=其他func;
结果=myFunc(“Foo”);//哎呀。。。不会返回int。
如果C#期望一个返回类型为
int
的委托,则声明返回
dynamic
的委托是不兼容的。您的委托被声明为
Func
,而C#是强类型的。它不在乎你的函数是否真的返回一个int并接受一个字符串;它仍然声明为
Func

<> P>代码不编译的原因是显而易见的,如果你考虑这个例子:

mc.OnFunc += new Func<dynamic, int>(someFunc);// += someFunc;
Func myFunc=delegate(字符串foo){return 42;};
int result=myFunc(“Foo”);
//如果允许的话。。。
Func otherFunc=委托(字符串foo){return 42;};
myFunc=其他func;
结果=myFunc(“Foo”);
//那么这也是允许的,但将是一个运行时错误。
otherFunc=委托(字符串foo){返回“Bar”};//有效的
myFunc=其他func;
结果=myFunc(“Foo”);//哎呀。。。不会返回int。
好的,我知道了

该操作定义如下:
委托无效操作(T arg)
,而func是这样的:
委托TResult func(T arg)问题在于out关键字,它表示协方差,但不像in关键字那样表示逆变换。同时
typeof(dynamic)==typeof(object)
为真。所以,动态,作为一种更普遍的类型,对我们所采取的任何行动都不是协变的。嗯,我认为动态绑定更灵活。”

进一步阅读:

顺便说一句,我后来在那里找到了一句话:

如果泛型类型参数仅用作方法返回类型,而未用作形式方法参数的类型,则可以将其标记为协变。 反之亦然,如果类型仅用作形式方法参数的类型,而不用作方法返回类型,则可以将其标记为逆变类型

好的,我知道了

操作的定义如下:
委托无效操作(T arg);
,而函数是这样的:
委托树结果函数(T arg);
问题在于out关键字,它表示协方差,而不是in关键字所表示的逆方差。同时
typeof(dynamic)=typeof(object
是正确的。因此,作为一种更一般的类型,动态与我们所采取的任何方式都不一致。嗯,我认为动态绑定更灵活。”

进一步阅读:

顺便说一句,我后来在那里找到了一句话:

如果泛型类型参数仅用作方法返回类型,而未用作形式方法参数的类型,则可以将其标记为协变。 反之亦然,如果类型仅用作形式方法参数的类型,而不用作方法返回类型,则可以将其标记为逆变类型


@恩特雷斯,世上没有万能的解决方案……如果你在寻找万能的答案,那就是42;)让我们保持联系。我不是在寻找生命的意义。我只是想知道为什么要采取行动→ 操作正常,而Func→ Func不是。请告诉我你