2013-05-24 3 views
0

리 리플렉션을 사용하여 데이터를 동적으로 가져옵니다 (엔터티 유형은 런타임에 정의됩니다). 현재 currentObject가 1 : N 관계가 아니라면 ("First"제네릭 메소드 리플렉션 구현을 통해) 항상 하나의 객체를 반환하지만 EntityCollection 인 < T>와 1 : N 하위를 가져와야합니다.EntityCollection <T>을 통해 <T>을 리플렉션

는 사실은 내 "valoresp"변수가 EntityCollection의 480 개 다른 유형 (I 수동 입력 확인하지 않습니다 왜 그게 전부) (EntityCollection <를 표 1>, EntityCollection < 표 2> ...)

수 있습니다
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>; 


      } 
     } 

입니다

자식 개체의 목록이 필요하지만 리플렉션을 사용하여 EntityCollection을 List로 변환하는 방법을 찾을 수 없습니다.

대신 EntityCollection으로 캐스팅하려고 노력의 첫 번째 (EntityCollection는 IEnumerable을 구현하기 때문에)이 열거 있는지 확인하고 캐스트 Enumerable에서에 :

답변

0

은 그냥 방법을 알아 냈어. 그런 다음

(IEnumerable을가 .ToList() 메소드이 있기 때문에, 당신은 아직 할 수 을하지만) 당신은 등 LINQ 쿼리, 방법을 사용하여 ... 그리고 플러스 목록에 캐스팅을 방지 할 수 있습니다

if (valoresp is IEnumerable<object>) 
{ 

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