Results 1 to 9 of 9

Thread: [VS2010] Code Compiler

Threaded View

  1. #1

    Thread Starter
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    [VS2010] Code Compiler

    Hi all,
    Here is a very simple class to compile a string into an assembly. This code probably won't make it into any app, but it is just to show how easy it actually is.

    You can compile any class and then invoke any member.
    For functions you can just get the object returned by the invoke call, and cast that to the appropriate type.

    I have no idea what practical use this has, but it is kinda cool
    vb.net Code:
    1. Imports System.CodeDom.CompilerImports System.ReflectionClass VbCompiler
    2.  
    3.     Public Property Source As String
    4.     Public Property DLLS As String()
    5.     Public Sub New(ByVal Source As String, ByVal DLLS() As String)
    6.         Me.Source = Source
    7.         Me.DLLS = DLLS
    8.     End Sub
    9.     Private _errors As CompilerErrorCollection
    10.     Public ReadOnly Property Errors As CompilerErrorCollection
    11.         Get
    12.             Return _errors
    13.         End Get
    14.     End Property
    15.     Private _successful As Boolean
    16.     Public ReadOnly Property Successful As Boolean
    17.         Get
    18.             Return _successful
    19.         End Get
    20.     End Property
    21.     Public Function Compile() As Assembly
    22.         Dim Provider As New VBCodeProvider
    23.         Dim Parameters As New CompilerParameters
    24.         For Each DLL In Me.DLLS
    25.             Parameters.ReferencedAssemblies.Add(DLL)
    26.         Next
    27.         Parameters.GenerateInMemory = True
    28.         Dim results As CompilerResults = Provider.CompileAssemblyFromSource(Parameters, Me.Source)
    29.         If results.Errors.Count = 0 Then
    30.             _successful = True
    31.             Return results.CompiledAssembly
    32.         Else
    33.             _successful = False
    34.             _errors = results.Errors
    35.             Return Nothing
    36.         End If
    37.  
    38.     End Function
    39. End Class

    And use it like so
    vb.net Code:
    1. Dim SourceCode As String =<Source>
    2. Imports System.Windows.Forms
    3. Namespace TestCode
    4.         Class Class1
    5.             Sub ShowMessage()
    6.                 Messagebox.Show("Hello, This code was compiled from a string!")
    7.             End Sub
    8.         End Class
    9. End Namespace</Source>.Value
    10. 'Very Short program
    11.  
    12.  
    13.         Dim Dlls() As String = {"System.dll", "System.Core.dll", "System.Data.dll", "System.Windows.Forms.dll"}'Any referenced dll's
    14.         Dim Compiler As New VbCompiler(SourceCode, Dlls)
    15.         Dim CodeAssembly As Assembly = Compiler.Compile
    16.         If Compiler.Successful Then
    17.             Dim instance As Object = CodeAssembly.CreateInstance("TestCode.Class1")
    18.             Dim CodeType As Type = instance.GetType
    19.             Dim Info As MethodInfo = CodeType.GetMethod("ShowMessage")
    20.             Info.Invoke(instance, Nothing)
    21.         Else
    22.             For Each i As CompilerError In Compiler.Errors
    23.                 MsgBox(i.ErrorText)
    24.             Next
    25.         End If

    With this class you could easily create a RunCode function that takes a string as parameter and run's it.
    Last edited by BlindSniper; Dec 8th, 2011 at 06:10 AM.

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width