我一个枚举,延伸短:为什么不能使用这种通用方法调用?
public enum EnumType : short
{
Value1 = 1,
Value2 = 2
}
然后,我有一个看起来像这样的实用方法:
public static class EnumUtilities
{
public static IEnumerable<Tuple<TEnum, TBase>> GetTuples<TEnum, TBase>()
where TEnum : struct, TBase
{
return GetValues<TEnum>().Select(x => new Tuple<TEnum, TBase>(x, x));
}
public static IEnumerable<T> GetValues<T>() where T : struct
{
return Enum.GetValues(typeof(T)).Cast<T>();
}
}
当我尝试调用它,我得到一个编译错误说的”类型EnumType必须可以转换为'short'才能在通用方法'IEnumerable> GetTuples()'中使用它作为参数'TEnum''
我无法理解EnumType如何不能转换为'当它被定义为分机时,“简短”结束'短'。我还没有理解如何以下编译,但上面的例子没有。任何帮助表示赞赏!
var enumType = EnumType.Value1;
var value1 = (short)enumType;
'Enum'默认的类型是'int'的。所以隐式转换为“short”是不可能的。 – AgentFire 2014-10-02 18:21:23