2010-08-03 5 views
2

컨텍스트의 테이블을 반복 한 다음 해당 테이블의 속성을 사용하여 컨텍스트의 모든 열을 열심히로드합니다. 다른 질문을 통해 도움을 받았지만 실제 테이블의 열 속성을 반복하는 방법을 알 수없는 것 같습니다.컨텍스트에서 테이블 반복 및 해당 테이블의 속성


최종 작업 코드 :

public static void DisableLazyLoading(this DataContext context) 
{ 
    DataLoadOptions options = new DataLoadOptions(); 

    var contextTables = context.GetType().GetProperties().Where(n => n.PropertyType.Name == "Table`1"); 
    foreach (var contextTable in contextTables) 
    { 
     var tableType = contextTable.GetValue(context, null).GetType().GetGenericArguments()[0]; 
     var tableProperties = tableType.GetProperties().Where(n => n.PropertyType.Name != "EntitySet`1"); 
     foreach (var tableProperty in tableProperties) 
     { 
      ParameterExpression paramExp = Expression.Parameter(tableType, "s"); 
      Expression expr = Expression.Property(paramExp, tableProperty.Name); 
      options.LoadWith(Expression.Lambda(expr, paramExp)); 
     } 
    } 

    context.LoadOptions = options; 
} 
+0

왜 그때의 속성을 얻을 수 있습니까? – GenericTypeTea

+0

'PropertyInfo' 클래스 중. 나는 실제 테이블을 얻는 방법을 알아낼 수 없다. –

답변

1

당신은 단지 ProperInfo의가 있어요. 당신은PropertyInfo의에서 값 을 얻을 필요가 :

var tablePropertInfos = context.GetType().GetProperties().Where(
    n => n.PropertyType.Name == "Table`1"); 

foreach (var tablePropertyInfo in tablePropertInfos) 
{ 

    // Get the actual table 
    var table = tablePropertyInfo.GetValue(context, null); 

    // Do the same for the actual table properties 

} 

당신이 PropertyInfo 클래스가되면, 당신은 GetValue 방법을 사용하여 값을 얻을 필요가있다.

+0

아,'GetValue'는 현재하는 일에 대해 너무나 의미가 있습니다. 그것은 각각에 대해'Table'1 타입을 반환하기 때문에 아주 가깝습니다. 그래도 테이블 열을 정의하는 실제 클래스/유형으로 캐스팅하는 방법을 알고 있습니까? 이제부터는 'Order' 및'OrderItem' 테이블의 속성보다는 'Table'1의 속성 만 가져옵니다. –

+0

질문을 현재 코드로 업데이트했습니다. 나는 내부의'foreach'를 디버깅 할 때에 만이 두가지를 얻는다. http://msdn.microsoft.com/en-us/library/bb336216.aspx#propertyTableToggle –

+0

그래서 제네릭 타입의 'Table '결과? 나는. 표 에서'GetProperties'가'Foo'의 속성을 반환하기를 원합니까? – GenericTypeTea

관련 문제