C# 实体集合<;T>;列出<;T>;反省

C# 实体集合<;T>;列出<;T>;反省,c#,list,reflection,entitycollection,C#,List,Reflection,Entitycollection,Im使用反射动态获取数据(实体类型在运行时定义)。当我的currentObject没有1:N关系时(通过“First”泛型方法反射实现),我当前返回一个对象,但我还需要得到1:N子对象,即EntityCollection var valoresp = getFilho(pai, filho, raizAtual); if (valoresp == null) return new object(); if (!filho.A_Ocorrencia_Tab

Im使用反射动态获取数据(实体类型在运行时定义)。当我的currentObject没有1:N关系时(通过“First”泛型方法反射实现),我当前返回一个对象,但我还需要得到1:N子对象,即EntityCollection

var valoresp = getFilho(pai, filho, raizAtual);
        if (valoresp == null)
            return new object();
 if (!filho.A_Ocorrencia_Tabela.Contains("1:N"))
        {
            var firstMethod = typeof(Enumerable).GetMethods().Single(method => method.Name == "First"
                              && method.IsStatic && method.GetParameters().Length == 1);
            var interfaceImplementation = MethodResolver.GetImplementationOfInterface(valoresp.GetType(),
                              firstMethod.GetParameters().First().ParameterType.GetGenericTypeDefinition());

            var genericArgumentsTypes = interfaceImplementation.GetGenericArguments();
            var genericMethod = firstMethod.MakeGenericMethod(new[] { genericArgumentsTypes[0] });
            try
            {
                var resultado = genericMethod.Invoke(null, new[] { valoresp });
                return resultado;
            }
            catch (Exception)
            {
                return new object();
            }

        }
        else
        {
            if (valoresp.GetType().IsGenericType && (valoresp.GetType().GetGenericTypeDefinition()  == typeof(EntityCollection<>))  )
            {
                   //here is the problem:
                   var typeValoresp = valoresp as EntityCollection<object>;


            }
        }
var valoresp=getFilho(pai、filho、raizAtual);
如果(从价==null)
返回新对象();
如果(!filho.A_ocorrecia_Tabela.包含(“1:N”))
{
var firstMethod=typeof(可枚举).GetMethods().Single(method=>method.Name==“First”
&&method.IsStatic&&method.GetParameters().Length==1);
var interfaceImplementation=MethodResolver.GetImplementationOfInterface(valoresp.GetType(),
firstMethod.GetParameters().First().ParameterType.GetGenericTypeDefinition());
var genericalargumentstypes=interfaceImplementation.getgenericalarguments();
var genericMethod=firstMethod.MakeGenericMethod(new[]{genericArgumentsTypes[0]});
尝试
{
var resultado=genericMethod.Invoke(null,new[]{valoresp});
返回resultado;
}
捕获(例外)
{
返回新对象();
}
}
其他的
{
if(valoresp.GetType().IsGenericType&&(valoresp.GetType().GetGenericTypeDefinition()==typeof(EntityCollection)))
{
//问题是:
var TYPE valoresp=作为实体集合的valoresp;
}
}
事实上,我的“valoresp”变量可以是480种不同类型的EntityCollection(这就是我不手动检查类型的原因)(EntityCollection,EntityCollection…)


我需要子对象的列表,但找不到使用反射将EntityCollection转换为列表的方法

刚刚想出了如何:

不要尝试强制转换为EntityCollection,首先检查它是否可枚举(因为EntityCollection实现IEnumerable)并强制转换为可枚举

然后您将能够使用Linq查询、方法等。。。另外,还要避免强制转换到列表(但您仍然可以,因为IEnumerable有.ToList()方法)

if(从价是IEnumerable)
{
var valorEnum=作为IEnumerable的valoresp;
...
//从价第一/从价,其中(…)等。。
}
if (valoresp is IEnumerable<object>)
{

    var valorEnum = valoresp as IEnumerable<object>;
    ...
    //valorEnum.First / valorEnum.where(...) etc..
}