익명 형식은 속성의 데이터 형식을 직접 지정하는 메커니즘을 제공하지 않습니다. 모든 속성의 형식이 유추됩니다. 다음 예제에서 Name과 Price의 형식은 그들 각각을 초기화하는 데 사용된 값에서 직접 유추됩니다.
' Variable product is an instance of a simple anonymous type.
Dim product = New With {Key .Name = "paperclips", .Price = 1.29}
익명 형식은 다른 원본의 속성 이름 및 형식을 유추할 수도 있습니다. 다음 섹션에서는 유추가 가능한 상황 목록과 그렇지 않은 상황의 예를 제공합니다.
성공적인 유추
익명 형식은 다음 원본에서 속성 이름 및 형식을 유추할 수 있습니다.
변수 이름에서 무명 형식
anonProduct에는 두 개의 속성productName과productPrice. 해당 데이터 형식은 각각 원래 변수String와Double의 형식이 됩니다.Dim productName As String = "paperclips" Dim productPrice As Double = 1.29 Dim anonProduct = New With {Key productName, Key productPrice} ' To create uppercase variable names for the new properties, ' assign variables productName and productPrice to uppercase identifiers. Dim anonProduct1 = New With {Key .Name = productName, Key .Price = productPrice}다른 개체의 속성이나 필드 이름에서부터 예를 들어
car유형의CarClass객체를 고려해 보세요. 이 객체는Name및ID속성을 포함합니다. 새 익명 형식 인스턴스car1를 만들고,Name및ID속성을car개체의 값으로 초기화하려면 다음을 작성할 수 있습니다.Dim car1 = New With {Key car.Name, Key car.ID}이전 선언은 익명 형식
car2을 정의하는 긴 코드 줄과 동일합니다.Dim car2 = New With {Key .Name = car.Name, Key .ID = car.ID}XML 멤버 이름에서부터
Dim books = <Books> <Book Author="Jesper Aaberg"> Advanced Programming Methods </Book> </Books> Dim anon = New With {books...<Book>}결과 형식
anon에는Book(Of XElement) 형식의 IEnumerable 속성이 하나 있습니다.다음 예제와 같이
SomeFunction매개 변수가 없는 함수에서Dim sc As New SomeClass Dim anon1 = New With {Key sc.SomeFunction()}다음 코드의 변수
anon2는 하나의 속성, 즉 이름이 지정된First문자를 포함하는 익명 형식입니다. 이 코드는 함수 First에서 반환하는 문자 "E"를 표시합니다.Dim aString As String = "Example String" Dim anon2 = New With {Key aString.First()} ' The variable anon2 has one property, First. Console.WriteLine(anon2.First)
유추 실패
이름 유추는 다음과 같은 여러 상황에서 실패합니다.
유추는 인수가 필요한 메서드, 생성자 또는 매개 변수가 있는 속성의 호출에서 파생됩니다. 하나 이상의 인수가 있는 경우
anon1이전 선언someFunction이 실패합니다.' Not valid. ' Dim anon3 = New With {Key sc.someFunction(someArg)}새 속성 이름에 할당하면 문제가 해결됩니다.
' Valid. Dim anon4 = New With {Key .FunResult = sc.someFunction(someArg)}유추는 복잡한 식에서 파생됩니다.
Dim aString As String = "Act " ' Not valid. ' Dim label = New With {Key aString & "IV"}식의 결과를 속성 이름에 할당하여 오류를 해결할 수 있습니다.
' Valid. Dim label1 = New With {Key .someLabel = aString & "IV"}여러 속성에 대한 유추는 이름이 같은 두 개 이상의 속성을 생성합니다. 이전 예제의 선언을 참조하면,
product.Name와car1.Name을 동일한 무명 형식의 속성으로 나열할 수 없습니다. 이런 각 항목의 유추된 식별자는Name입니다.' Not valid. ' Dim anon5 = New With {Key product.Name, Key car1.Name}고유 속성 이름에 값을 할당하여 문제를 해결할 수 있습니다.
' Valid. Dim anon6 = New With {Key .ProductName = product.Name, Key .CarName = car1.Name}대/소문자 변경(대문자와 소문자 간 변경)은 두 이름을 구분하지 않습니다.
Dim price = 0 ' Not valid, because Price and price are the same name. ' Dim anon7 = New With {Key product.Price, Key price}한 속성의 초기 형식 및 값은 아직 설정되지 않은 다른 속성에 따라 달라집니다. 예를 들어
.IDName = .LastName이미 초기화되지 않은 경우.LastName익명 형식 선언에서는 유효하지 않습니다.' Not valid. ' Dim anon8 = New With {Key .IDName = .LastName, Key .LastName = "Jones"}이 예제에서는 속성이 선언되는 순서를 반대로 변경하여 문제를 해결할 수 있습니다.
' Valid. Dim anon9 = New With {Key .LastName = "Jones", Key .IDName = .LastName}익명 형식의 속성 이름은 멤버의 Object이름과 같습니다. 예를 들어,
Equals가 Object의 메서드이기 때문에 다음 선언이 실패합니다.' Not valid. ' Dim relationsLabels1 = New With {Key .Equals = "equals", Key .Greater = _ ' "greater than", Key .Less = "less than"}속성 이름을 변경하여 문제를 해결할 수 있습니다.
' Valid Dim relationsLabels2 = New With {Key .EqualString = "equals", Key .GreaterString = "greater than", Key .LessString = "less than"}
참고하십시오
.NET