SHARE
TWEET

SO12963383

MarkHurd Oct 20th, 2012 38 Never
  1. Imports System.CodeDom
  2.  
  3. Module SO12963383Main
  4. Sub Main
  5.     Dim compileUnit As New CodeCompileUnit
  6.  
  7.     Dim ns As New CodeNamespace()
  8.     compileUnit.Namespaces.Add(ns)
  9.     ns.Imports.Add(New CodeNamespaceImport("System"))
  10.     ns.Imports.Add(New CodeNamespaceImport("System.Collections.Generic"))
  11.  
  12.     Dim fooClass As New CodeTypeDeclaration("Foo")
  13.     ns.Types.Add(fooClass)
  14.     fooClass.TypeAttributes = Reflection.TypeAttributes.Public
  15.     fooClass.IsClass = True
  16.  
  17.     Dim fooCollection As New CodeTypeDeclaration("FooCollection")
  18.     ns.Types.Add(fooCollection)
  19.     fooCollection.TypeAttributes = Reflection.TypeAttributes.Public
  20.     fooCollection.IsClass = True
  21.     fooCollection.BaseTypes.Add(New CodeTypeReference(GetType(System.Object)))
  22.     fooCollection.BaseTypes.Add(New CodeTypeReference( _
  23.         "System.Collections.Generic.IEnumerable" _
  24.         , New CodeTypeReference() {New CodeTypeReference("Foo")} _
  25.     ))
  26.  
  27.     Dim method As New CodeMemberMethod
  28.     fooCollection.Members.Add(method)
  29.     method.Attributes = MemberAttributes.Private
  30.     method.Name = "GetEnumerator"
  31.     method.ReturnType = New CodeTypeReference( _
  32.         "System.Collections.Generic.IEnumerator" _
  33.         , New CodeTypeReference() {New CodeTypeReference("Foo")} _
  34.     )
  35.     method.ImplementationTypes.Add(New CodeTypeReference( _
  36.         "System.Collections.Generic.IEnumerable" _
  37.         , New CodeTypeReference() {New CodeTypeReference("Foo")} _
  38.     ))
  39.  
  40.     method = New CodeMemberMethod
  41.     fooCollection.Members.Add(method)
  42.     method.Attributes = MemberAttributes.Private
  43.     method.Name = "GetEnumerator"
  44.     method.ReturnType = New CodeTypeReference( _
  45.         "System.Collections.IEnumerator" _
  46.     )
  47.     method.PrivateImplementationType = New CodeTypeReference( _
  48.         "System.Collections.IEnumerable" _
  49.     )
  50.  
  51.     Dim provider As New Microsoft.VisualBasic.VBCodeProvider
  52.     Dim options As New Compiler.CodeGeneratorOptions
  53.     Dim writer As New IO.StringWriter
  54.     provider.GenerateCodeFromCompileUnit(compileUnit, writer, options)
  55.     Console.WriteLine(writer.ToString())
  56. End Sub
  57. End Module
RAW Paste Data
Top