1
看着示例代碼在MSDN:使用的CodeDOM寫VB.Net屬性
這
' Declares a type.
Dim type1 As New CodeTypeDeclaration("Type1")
' Declares a constructor.
Dim constructor1 As New CodeConstructor
constructor1.Attributes = MemberAttributes.Public
type1.Members.Add(constructor1)
' Declares an integer field.
Dim field1 As New CodeMemberField("System.Int32", "integerField")
type1.Members.Add(field1)
' Declares a property.
Dim property1 As New CodeMemberProperty
property1.Name = "integerProperty"
property1.Type = New CodeTypeReference(GetType(Integer))
' Declares a property get statement to return the value of the integer field.
property1.GetStatements.Add(New CodeMethodReturnStatement(New CodeFieldReferenceExpression(New CodeThisReferenceExpression, "integerField")))
' Declares a property set statement to set the value to the integer field.
' The CodePropertySetValueReferenceExpression represents the value argument passed to the property set statement.
property1.SetStatements.Add(New CodeAssignStatement(New CodeFieldReferenceExpression(New CodeThisReferenceExpression, "integerField"), New CodePropertySetValueReferenceExpression))
type1.Members.Add(property1)
Dim dump As New VBCodeProvider
Dim gen As Compiler.ICodeGenerator = dump.CreateGenerator
Dim opt As New Compiler.CodeGeneratorOptions
gen.GenerateCodeFromType(type1, Console.Out, opt)
應該產生
Public Class Type1
Private integerField As Integer
Public Sub New()
MyBase.New()
End Sub
Private Property integerProperty() As Integer
Get
Return Me.integerField
End Get
Set(ByVal Value As Integer)
Me.integerField = value
End Set
End Property
End Class
但我沒有看到它產生的參數到Set方法,也就是說我得到:
Set
Me.integerField = value
End Set
既然我已經在位添加到示例代碼中對它進行命名的屬性,我懷疑有一個細節缺失,是造成這場比賽他們的榜樣輸出,但我可以似乎沒有跟蹤它?在編譯代碼的同時,我無法在不手動添加參數的情況下使用這些屬性,這在一定程度上破壞了目的。任何想法缺少什麼?在this DevCity article