演员LT;> INT; .Cast<&诠释GT;应用于无效强制转换异常通用枚举集合结果 [英] Cast<int>.Cast<int?> applied on generic enum collection results in invalid cast exception
问题描述
枚举性别{男,女}
变种K =新[] {Gender.Male} .Cast< INT方式>()了ToList()。铸造<&诠释GT;?()了ToList()。 //好吧
变种p值=新[] {} Gender.Male&.Cast LT; INT>()演员LT;诠释>?()了ToList(); // InvalidCastException的
什么是第二种情况的原因是什么?我知道我不能投了盒装枚举
到 INT?
直接,但我做了两阶段铸造,即演员LT; INT> .Cast<诠释>
这应该是工作
编辑:
这是令人惊讶的考虑以下工作:
对象o = Gender.Male;
INT I =(INT)O; //所以在这里投是不是一个完全不同的类型,它的工作原理
好,我是来找出病因,这仍然是陌生的。我应该检查演员LT; T>!首先
实施自己
这是如何演员LT; T>
实施
公共静态的IEnumerable< TResult>铸造< TResult>(这个IEnumerable的源)
{
IEnumerable的< TResult>枚举=来源为IEnumerable< TResult取代;
如果(枚举!= NULL)
{
返回枚举; //这是罪魁祸首..
}
如果(来源== NULL)
{
掷Error.ArgumentNull(源);
}
返回Enumerable.CastIterator< TResult>(源);
}
私有静态的IEnumerable< TResult> CastIterator< TResult>(IEnumerable的源)
{
的foreach(在源对象电流),
{
收益回报(TResult)((对象)的电流);
}
产量突破;
}
现在的问题是在第一 演员LT; INT>
电话:
新[] {Gender.Male}。铸造< INT>()
下面来源为IEnumerable< TResult>
,其中源
是新[] {Gender.Male}
和 TResult
是 INT
在泛型方法返回非空值(这基本上意味着(新[] {Gender.Male}
是的IEnumerable< INT>
在通用的上下文中),因此它返回相同的枚举后面是性别[]
,并在接下来的演员LT;诠释>
通话,实际投中进行,这是自性别
到 INT
从而未能至于为什么会出现这种情况在一般情况下,的catch~~V它在这个问题上。
enum Gender { Male, Female }
var k = new[] { Gender.Male }.Cast<int>().ToList().Cast<int?>().ToList(); //alright
var p = new[] { Gender.Male }.Cast<int>().Cast<int?>().ToList(); //InvalidCastException
What is the cause for the second case? I know I cant cast a boxed enum
to int?
directly, but I do a two stage casting, ie Cast<int>.Cast<int?>
which should be working.
Edit:
This is surprising considering the below works:
object o = Gender.Male;
int i = (int)o; // so here the cast is not to an entirely different type, which works
Ok I have come to find out the cause, which is still strange. I should have checked up Cast<T>
implementation myself first of all!
This is how Cast<T>
is implemented:
public static IEnumerable<TResult> Cast<TResult>(this IEnumerable source)
{
IEnumerable<TResult> enumerable = source as IEnumerable<TResult>;
if (enumerable != null)
{
return enumerable; // this is the culprit..
}
if (source == null)
{
throw Error.ArgumentNull("source");
}
return Enumerable.CastIterator<TResult>(source);
}
private static IEnumerable<TResult> CastIterator<TResult>(IEnumerable source)
{
foreach (object current in source)
{
yield return (TResult)((object)current);
}
yield break;
}
Now the problem is with the first Cast<int>
call:
new[] { Gender.Male }.Cast<int>()
Here source as IEnumerable<TResult>
where source
is new[] { Gender.Male }
and TResult
is int
in the generic method returns a non null value (which basically means (new[] { Gender.Male }
is an IEnumerable<int>
in generic context), and hence it returns the same enumerable back which is Gender[]
, and in the next Cast<int?>
call, the actual cast is performed, which is from Gender
to int?
which fails. As to why this happens in generic context, catch it in this question.
这篇关于演员LT;> INT; .Cast<&诠释GT;应用于无效强制转换异常通用枚举集合结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!