다음을 통해 공유


@@@Cannot convert anonymous type to expression tree because it contains a field that is used in the initialization of another field

업데이트: 2007년 11월

Cannot convert anonymous type to expression tree because it contains a field that is used in the initialization of another field

컴파일러에서는 익명 형식의 속성 하나가 익명 형식의 다른 속성을 초기화하는 데 사용되는 경우, 해당 익명 형식을 식 트리로 변환하는 것을 허용하지 않습니다. 예를 들어 다음 코드에서 Prop1은 초기화 목록에 선언된 후 Prop2의 초기 값으로 사용됩니다.

Module M2

    Sub ExpressionExample(Of T)(ByVal x As Expressions.Expression(Of Func(Of T)))
    End Sub

    Sub Main()
        ' The following line causes the error.
        ' ExpressionExample(Function() New With {.Prop1 = 2, .Prop2 = .Prop1})

    End Sub
End Module

오류 ID: BC36548

이 오류를 해결하려면

  • Prop1의 초기 값으로 지역 변수를 할당한 후 다음 코드와 같이 이 변수를 Prop1과 Prop2 모두에 할당합니다.

    Sub Main()
    
        Dim temp = 2
        ExpressionExample(Function() New With {.Prop1 = temp, .Prop2 = temp})
    
    End Sub
    

참고 항목

작업

방법: 식 트리를 사용하여 동적 쿼리 빌드

개념

익명 형식

LINQ의 식 트리