Results 1 to 9 of 9

Thread: [VS2010] Code Compiler

  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

  2. #2

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

    Re: [VS2010] Code Compiler

    Update:
    I'm Uploading a small project to better demonstrate it's use
    Attached Images Attached Images  
    Attached Files Attached Files
    Last edited by BlindSniper; Dec 8th, 2011 at 06:39 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

  3. #3
    Lively Member CoderKid's Avatar
    Join Date
    Oct 2011
    Location
    In the middle of nowhere
    Posts
    72

    Re: [VS2010] Code Compiler

    So...it's kind of a non-visual version of Visual Studio!

    Cool!
    <-- If you like my post, click on the to the left! It only takes a second...

  4. #4
    New Member
    Join Date
    Feb 2012
    Posts
    1

    Resolved Re: [VS2010] Code Compiler

    Cool code! I've made some modifications like the toolstrip with new,load,save,test and build.

    Screenshot:
    Attached Images Attached Images  
    Attached Files Attached Files

  5. #5
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    629

    Re: [VS2010] Code Compiler

    Nice this is cool, may try and make something out of this if I have time. Thanks for shareing.

  6. #6

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

    Re: [VS2010] Code Compiler

    It's a pleasure. The point of this project was just to show people how easy it is to actually compile code from a string. The code that I wrote was a while back and I'm afraid that it isn't up to par.

    I actually thought about making a very small portable visual basic project editor that you can put on a memory stick and develop in vb anywhere, but that would take me a while to code, but I simply don't have the time during school days.

    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

  7. #7
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    629

    Re: [VS2010] Code Compiler

    I actually thought about making a very small portable visual basic project editor that you can put on a memory stick and develop in vb anywhere, but that would take me a while to code, but I simply don't have the time during school days.
    That is a good idea for a project. I remmber I made something like this with the VB Script Control for VB6 even got as far as makeing a form designer.I have a lot of time but seeing how easy it is to compile VB or C# with the code compiler is ace may try and work on a little side project.

  8. #8
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,226

    Re: [VS2010] Code Compiler

    Have you heard of a tool called Snippy?
    It does something similar for C# code.
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  9. #9
    New Member
    Join Date
    Feb 2013
    Posts
    13

    Re: [VS2010] Code Compiler

    Quote Originally Posted by Wesleylucas View Post
    Cool code! I've made some modifications like the toolstrip with new,load,save,test and build.

    Screenshot:
    Nvm it works fine thanks ;D
    Last edited by santtuz112; Nov 1st, 2013 at 05:50 PM.

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