共用方式為


如何:推斷匿名類型宣告中的屬性名稱和類型 (Visual Basic)

匿名型別不提供直接指定屬性數據類型的機制。 所有屬性的類型皆為推斷而得。 在下列範例中,NamePrice 的類型會直接從用來初始化它們的值推斷。

' Variable product is an instance of a simple anonymous type.
Dim product = New With {Key .Name = "paperclips", .Price = 1.29}

匿名類型也可以推斷來自其他來源的屬性名稱和類型。 下列各節提供推斷可能的情況清單,以及不存在的情況範例。

成功的推斷

匿名類型可以從下列來源推斷屬性名稱和類型:

  • 從變數名稱。 匿名類型 anonProduct 會有兩個屬性和 productNameproductPrice。 其數據類型分別是StringDouble的原始變數的數據類型。

    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類型的物件,該類型包含CarClassName屬性。 若要建立新的匿名型別實例 car1,該實例的 NameID 屬性要用 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,其類型為IEnumerable(Of XElement)。

  • 從沒有參數的函式,例如 SomeFunction 在下列範例中。

    Dim sc As New SomeClass
    Dim anon1 = New With {Key sc.SomeFunction()}
    

    下列程式代碼中的變數 anon2 是具有一個屬性的匿名型別,名為 First的字元。 此程式代碼會顯示字母 「E」,函式 所 First傳回的字母 。

    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.Namecar1.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名稱相同。 例如,下列宣告會失敗,因為 EqualsObject 的一個方法。

    ' 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"}
    

另請參閱