【发布时间】:2014-05-03 17:28:34
【问题描述】:
以下源代码在 Visual Studio 中使用 .NET 时按预期工作,但在使用 Mono 框架时会引发上述异常:
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 中的 Mono 实现卡住了:/
例外
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 ()
...
【问题讨论】:
-
我不确定这个提议有多可靠,但它似乎有效 (hastebin.com/ahoxoleyed.cs)。有什么想法吗?
标签: c# exception delegates mono argumentexception