应用于通用枚举收集结果的无效转换异常

应用于通用枚举收集结果的无效转换异常

本文介绍了投<&INT GT; .Cast<诠释>应用于通用枚举收集结果的无效转换异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$ {pre> 枚举性别{男,女}

var k = new [] {Gender.Male} .Cast< int>()ToList()。投<诠释>()ToList(); // alright

var p = new [] {Gender.Male} .Cast< int>()。Cast< int?>()ToList(); // InvalidCastException

第二种情况是什么原因?我知道我不能直接将一个盒装枚举 int?,但我做了两个阶段的转换,即 Cast< int> .Cast< int?> 应该正常工作。



编辑:



考虑到以下作品,这是令人惊讶的:

 对象o =性别。 
int i =(int)o; //所以在这里,演员不是一个完全不同的类型,它的作品是


解决方案 Cast< T> 实现!



这是如何 Cast< T> 被实现:

  public static IEnumerable< TResult> Cast< TResult>(此IEnumerable源)
{
IEnumerable< TResult> enumerable = source as IEnumerable< TResult> ;;
if(enumerable!= null)
{
return枚举; //这是罪魁祸首..
}
if(source == null)
{
throw Error.ArgumentNull(source);
}
return Enumerable.CastIterator< TResult>(source);
}

private static IEnumerable< TResult> $($)
{

$ b {b
yield return(TResult)((object)current);
}
yield break;
}

现在问题出在第一个 Cast< int> call:

  new [] {Gender.Male}。 Cast< int>()

这里源为IEnumerable< TResult> code>其中 new [] {Gender.Male} TResult int 在通用方法中返回一个非空值(这基本上意味着( new [] {Gender.Male} 在通用上下文中是一个 IEnumerable< int> ),因此它返回相同的枚举返回是code> Gender [] ,而在下一个 Cast< int?> 调用中,执行实际的转换, code>性别 to int? which failed。至于为什么在通用上下文中发生这种情况,。


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 GT; .Cast&LT;诠释&GT;应用于通用枚举收集结果的无效转换异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 12:08