【发布时间】:2011-04-26 08:18:24
【问题描述】:
编辑:我提交了关于 microsoft connect:: 的错误报告 https://connect.microsoft.com/VisualStudio/feedback/details/614234/delegate-createdelegate-allows-binding-functions-with-enum-parameters-to-the-enum-base-type#details
考虑以下事情:
public static Int32 Test(Int16 @short)
{
return @short;
}
从调用代码::
Func<Int16,Int32> test = Test; //valid method group conversion.
Func<Int16,StringComparison> test2 = Test; // doesn't compile, not valid method group conversion.
Func<Int16,StringComparison> test3 = test; // doesn't compile, invalid contra-variance.
Func<Int16,StringComparison> test4 = Delegate.CreateDelegate(typeof(Func<Int16,StringComparison>),test.Method) as Func<Int16,StringComparison>; // works fine.
为什么 Delegate.CreateDelegate 可以进行这种其他创建函数的方法都不允许的奇怪转换?更糟糕的是,说Int64 或Object 都失败了。我意识到StringComparison“扩展”Int32,但我认为这更像是一个编译器技巧,因为枚举扩展了Enum 类。
此外,此转换也适用于 DynamicMethod.CreateDelegate。
Edit 刚刚用Nullable<Int32> 尝试过,它不起作用,这很令人困惑。我认为唯一的“免费”转换是将Enum 类型转换为其基础类型,但为什么呢?
请注意,这不允许您将 int 实例方法(如 GetHashCode)转换为采用 enum 类型的开放方法,这就是为什么我认为这是一个错误,因为行为不一致。
编辑: 如果我们删除 test2 和 test3 行,我们可以测试以查看方法、委托和“非法”委托是否都按预期工作。
Console.WriteLine(Test(0)); // prints 0
Console.WriteLine(test(0)); // prints 0
Console.WriteLine(test4(0)); //prints CurrentCulture
编辑:
这是我在大约 10 分钟内写的一个非常大的滥用。这为TEnum 创建了一个IEqualityComparer<T>,基本上抓住了它的底层类型,然后只包装Equals 和HashCode 并使用这个技巧/滥用将参数转换为TEnums,而不是底层类型。如果这是一个错误,我想知道这样我就不会尝试依赖这种行为。
class EnumComparer<TEnum> : EqualityComparer<TEnum> where TEnum : struct
{
static Func<TEnum, TEnum, bool> s_Equals;
static Func<TEnum, int> s_HashCode;
static EnumComparer<TEnum> s_default;
static EnumComparer()
{
if (!typeof(TEnum).IsEnum) throw new Exception("Not an enum type");
Type underlyingType = Enum.GetUnderlyingType(typeof(TEnum));
object equalityComparer = typeof(EqualityComparer<>).MakeGenericType(new[] { underlyingType }).GetProperty("Default").GetGetMethod().Invoke(null, null);
s_Equals = Delegate.CreateDelegate(typeof(Func<TEnum, TEnum, bool>), equalityComparer,equalityComparer.GetType().GetMethod("Equals", new[]{underlyingType,underlyingType})) as Func<TEnum,TEnum,bool>;
s_HashCode = Delegate.CreateDelegate(typeof(Func<TEnum, int>), equalityComparer, equalityComparer.GetType().GetMethod("GetHashCode", new[]{underlyingType})) as Func<TEnum, int>;
s_default = new EnumComparer<TEnum>();
}
public static new EnumComparer<TEnum> Default
{
get
{
return s_default;
}
}
public override bool Equals(TEnum x, TEnum y)
{
return s_Equals(x, y);
}
public override int GetHashCode(TEnum obj)
{
return s_HashCode(obj);
}
private EnumComparer()
{
}
}
【问题讨论】:
-
这里不错的 Jon Skeet 帖子:msmvps.com/blogs/jon_skeet/archive/2008/08/09/…
-
当你使用这种方法时,你的文章都没有提到 Enum 和 Int 之间的隐式转换。他们都在谈论使用 LGC 编写代码,而这种方法正在避免。