0
下面的源工程,并使用.NET在Visual Studio中,当如预期,但它使用单声道框架时抛出上述异常:的ArgumentException:方法的返回类型是不兼容的(单声道)
class Foo<TEnum> where TEnum : struct {
private static Func<int, int> Identity = (value) => value;
private static Func<int, TEnum> IntToEnum = Delegate.CreateDelegate(typeof(Func<int, TEnum>), Identity.Method) as Func<int, TEnum>;
private static Func<TEnum, int> EnumToInt = Delegate.CreateDelegate(typeof(Func<TEnum, int>), Identity.Method) as Func<TEnum, int>;
public static bool EnumEquals(TEnum lhs, TEnum rhs) {
return EnumToInt(lhs) == EnumToInt(rhs);
}
}
Foo<SomeEnum>.EnumEquals(SomeEnum.A, SomeEnum.B);
有一种解决方法对此,如上所述,避免拳击/拆箱价值?
我坚持的单执行驻留在Unity:/
例外
ArgumentException: method return type is incompatible
System.Delegate.CreateDelegate (System.Type type, System.Object firstArgument, System.Reflection.MethodInfo method, Boolean throwOnBindFailure) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System/Delegate.cs:190)
System.Delegate.CreateDelegate (System.Type type, System.Reflection.MethodInfo method, Boolean throwOnBindFailure) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System/Delegate.cs:291)
System.Delegate.CreateDelegate (System.Type type, System.Reflection.MethodInfo method) (at /Users/builduser/buildslave/monoAndRuntimeClassLibs/build/mcs/class/corlib/System/Delegate.cs:295)
Foo`1[SomeEnum]..cctor()
...
我不确定这个建议有多可靠,但它似乎*工作(http://hastebin.com/ahoxoleyed.cs)。任何想法? –