Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从IEnumerable集合的ISymbol获取其基础类型_C#_.net_Collections_Roslyn - Fatal编程技术网

C# 从IEnumerable集合的ISymbol获取其基础类型

C# 从IEnumerable集合的ISymbol获取其基础类型,c#,.net,collections,roslyn,C#,.net,Collections,Roslyn,我有一个可枚举集合的ISymbol对象,需要获取底层类型 e、 g List intList; 我有intList的ISymbol,需要找到底层类型——在本例中,它是int 我尝试使用列出的代码,但这里的反射似乎不起作用 以下是我的代码片段: private Type GetUnderlyingTypeFromEnumerable(ISymbol symbol) { Type eType = null; Type[] interfaces = symbol.GetTyp

我有一个可枚举集合的ISymbol对象,需要获取底层类型

e、 g

List intList;
我有intList的ISymbol,需要找到底层类型——在本例中,它是int

我尝试使用列出的代码,但这里的反射似乎不起作用

以下是我的代码片段:

private Type GetUnderlyingTypeFromEnumerable(ISymbol symbol)
{  
    Type eType = null;  
    Type[] interfaces = symbol.GetType().GetInterfaces();  
    foreach (Type i in interfaces)  
        if (i.IsGenericType && i.GetGenericTypeDefinition().Equals(typeof(IEnumerable<>)))  
        {  
            eType = i.GetGenericArguments()[0];  
            break;  
        }  

    return eType;  
}
私有类型GetUnderlyingTypeFromEnumerable(ISymbol符号)
{  
类型eType=null;
Type[]interfaces=symbol.GetType().GetInterfaces();
foreach(接口中的i型)
如果(i.IsGenericType&&i.GetGenericTypeDefinition().Equals(typeof(IEnumerable)))
{  
eType=i.GetGenericArguments()[0];
打破
}  
返回词缀;
}

谢谢大家。我翻阅了INamedTypeSymbol界面,找到了一个简单的方法来解决这个问题:

    private ITypeSymbol GetUnderlyingTypeFromEnumerable(INamedTypeSymbol symbol)
    {
        return symbol.TypeArguments.First();
    }

请分享您尝试的代码。如果您有类似的代码:IList collName。。。您可以执行-collName.GetType()并查找底层类型@pratekshrivastava的可能重复项I只有ISymbol对象,而没有IList。
ISymbol
告诉您有关代码的信息。您必须查看文档
GetType
只返回
ISymbol
本身的类型。反思无助于你。Roslyn已经分析了代码
    private ITypeSymbol GetUnderlyingTypeFromEnumerable(INamedTypeSymbol symbol)
    {
        return symbol.TypeArguments.First();
    }