' 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 System.Collections.Generic
Imports System.Reflection

Imports Microsoft.CSharp

Namespace E
    Public Interface IDemo
        Sub DoIt()
    End Interface
    Public Class Program
        Public Shared Sub Exec(src As String, typnam As String)
            Dim comp As CodeDomProvider = New CSharpCodeProvider()
            Dim param As New CompilerParameters()
            param.GenerateInMemory = True
            param.ReferencedAssemblies.Add("System.dll")
            param.ReferencedAssemblies.Add("System.Core.dll")
            param.ReferencedAssemblies.Add(Assembly.GetExecutingAssembly().Location)
            Dim res As CompilerResults = comp.CompileAssemblyFromSource(param, src)
            If res.Errors.Count > 0 Then
                Console.WriteLine(src)
                For Each ce As CompilerError In res.Errors
                    Console.WriteLine(ce)
                Next
            End If
            Dim asm As Assembly = res.CompiledAssembly
            Dim o As IDemo = DirectCast(asm.CreateInstance(typnam), IDemo)
            o.DoIt()
        End Sub
        Public Shared Sub Main(args As String())
            Dim c1 As String = "using System;" & vbCrLf & _
                               vbCrLf & _
                               "using E;" & vbCrLf & _
                               vbCrLf & _
                               "namespace Demo" & vbCrLf & _
                               "{" & vbCrLf & _
                               "    public class C1 : IDemo" & vbCrLf & _
                               "    {" & VbCrLf & _
                               "        public void DoIt()" & VbCrLf & _
                               "        {" & VbCrLf & _
                               "            Console.WriteLine(""Hello from C1"");" & VbCrLf & _
                               "        }" & VbCrLf & _
                               "    }" & VbCrLf & _
                               "}" & VbCrLf
            Dim c2 As String = "using System;" & vbCrLf & _
                               vbCrLf & _
                               "using E;" & vbCrLf & _
                               vbCrLf & _
                               "namespace Demo" & vbCrLf & _
                               "{" & vbCrLf & _
                               "    public class C2 : IDemo" & vbCrLf & _
                               "    {" & VbCrLf & _
                               "        public void DoIt()" & VbCrLf & _
                               "        {" & VbCrLf & _
                               "            Console.WriteLine(""Hello from C2"");" & VbCrLf & _
                               "        }" & VbCrLf & _
                               "    }" & VbCrLf & _
                               "}" & VbCrLf
            Exec(c1, "Demo.C1")
            Exec(c2, "Demo.C2")
        End Sub
    End Class
End Namespace
