' Collection of code snippets by Arne Vajhøj 
' (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
Imports System
Imports System.CodeDom.Compiler
Imports Microsoft.VisualBasic

Namespace E
    Public Class MainClass
        Public Shared Sub Main(ByVal args As String())
            Dim src As String = "Public Class X" & Environment.NewLine & "    Public Overloads Overrides Function ToString() As String" & Environment.NewLine & "        return ""I am a X""" & Environment.NewLine & "    End Function" & Environment.NewLine & "End Class"
            Dim comp As ICodeCompiler = (New VBCodeProvider).CreateCompiler
            Dim param As CompilerParameters = New CompilerParameters
            param.GenerateInMemory = True
            param.ReferencedAssemblies.Add("System.dll")
            Dim res As CompilerResults = comp.CompileAssemblyFromSource(param, src)
            Dim asm As System.Reflection.Assembly = res.CompiledAssembly
            Dim o As Object = asm.CreateInstance("X")
            Console.WriteLine(o)
        End Sub
    End Class 
End Namespace
