Tutorial :Using Codedom to write VB.Net Properties



Question:

Looking at the example code on MSDN:

This

    ' 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)  

should generate

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  

But I'm not seeing it generate the parameter to the Set method, I.e. I get:

       Set             Me.integerField = value         End Set  

Given that I already had to add in bits to the example code to actually name the property to match their example output, I suspect that there is a detail missing that is causing this, but I can't seem to track it down? Whilst the code compiles, I'm unable to use the properties without manually adding in the parameters, which defeats the purpose somewhat. Any idea what is missing?


Solution:1

Comment #3 on this DevCity article suggests it shouldn't matter, but I do know that Reflector does behave as you're expecting, although it may have it's own implementation for code generation...


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »