C# 无法获取对象的泛型类型T

C# 无法获取对象的泛型类型T,c#,generics,C#,Generics,我将对象传递给一个名为fromProperty和TopProperty的泛型方法。这些对象是泛型类型的列表,但当我尝试获取泛型类型时,会得到一个“System.IndexOutOfRangeException”异常。这与方法本身是泛型的有关,但它需要保持这种方式 private static Dictionary<string, List<object>> GetListAttributeDifferences<TFrom, TTo>(TFrom fr

我将对象传递给一个名为fromProperty和TopProperty的泛型方法。这些对象是泛型类型的列表,但当我尝试获取泛型类型时,会得到一个“System.IndexOutOfRangeException”异常。这与方法本身是泛型的有关,但它需要保持这种方式

    private static Dictionary<string, List<object>> GetListAttributeDifferences<TFrom, TTo>(TFrom fromProperty, TTo toProperty)
        where TFrom : class
        where TTo : class
    {
        //Get types for list containing types
        Type fromPropertyListContainingType = fromProperty.GetType().GenericTypeArguments[0];
        Type toPropertyListContainingType = toProperty.GetType().GenericTypeArguments[0]; return null;}
专用静态字典GetListAttributeDifferences(从属性到属性)
其中TFrom:class
在哪里上课
{
//获取包含类型的列表的类型
类型fromPropertyListContainingType=fromProperty.GetType().GenericTypeArguments[0];
键入TopPropertyListContainingType=TopProperty.GetType().GenericTypeArguments[0];返回null;}
这种方法称为

        public static Dictionary<string, List<object>> GetAttributeDifferences<TFrom, TTo>(TFrom fromObject, TTo toObject)
                where TFrom : class
                where TTo : class

            if (fromObject is IList && toObject is IList)
            {
                Dictionary<string, List<object>> listAttributeDifferences = GetListAttributeDifferences(fromObject, toObject);
            }}
公共静态字典GetAttributeDifferences(从对象到对象)
其中TFrom:class
在哪里上课
if(fromObject为IList&&toObject为IList)
{
Dictionary listAttributeDifferences=GetListAttributeDifferences(从对象到对象);
}}

使用
getgenericalarguments
,这是
系统的方法

private static Dictionary<string, List<object>> GetListAttributeDifferences<TFrom, TTo>(TFrom fromProperty, TTo toProperty)
        where TFrom : class
        where TTo : class
{
    //Get types for list containing types
    Type fromPropertyListContainingType = fromProperty.GetType().GetGenericArguments()[0];
    Type toPropertyListContainingType = toProperty.GetType().GetGenericArguments()[0];
    return null;
}
专用静态字典GetListAttributeDifferences(从属性到属性)
其中TFrom:class
在哪里上课
{
//获取包含类型的列表的类型
类型fromPropertyListContainingType=fromProperty.GetType().GetGenericArguments()[0];
类型toPropertyListContainingType=toProperty.GetType().GetGenericArguments()[0];
返回null;
}

请说明如何调用此方法
fromProperty
的类型是否具有泛型类型参数?那么,
fromProperty
是否类似于
List
,因此它有一个泛型参数?签名中没有任何东西表明它确实存在…此解决方案仍然会给我“System.IndexOutofFrangeException”异常。因此它们不是泛型的。如果(fromPropertyListContainingType.GetType().IsGenericType){}
您是对的,请在测试之前使用
if(fromPropertyListContainingType.GetType().IsGenericType){},计算结果为false——对象如何作为泛型列表传递并失去此状态?如何调用
GetAttributeDifferences
?如果知道这是一个列表,为什么不将函数签名更改为:私有静态字典GetListAttributeDifferences(list fromProperty,list toProperty)然后使用类型TFrom和TTo?