-
Sep 20th, 2011, 02:03 PM
#1
Thread Starter
Fanatic Member
[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:
Imports System.CodeDom.CompilerImports System.ReflectionClass VbCompiler Public Property Source As String Public Property DLLS As String() Public Sub New(ByVal Source As String, ByVal DLLS() As String) Me.Source = Source Me.DLLS = DLLS End Sub Private _errors As CompilerErrorCollection Public ReadOnly Property Errors As CompilerErrorCollection Get Return _errors End Get End Property Private _successful As Boolean Public ReadOnly Property Successful As Boolean Get Return _successful End Get End Property Public Function Compile() As Assembly Dim Provider As New VBCodeProvider Dim Parameters As New CompilerParameters For Each DLL In Me.DLLS Parameters.ReferencedAssemblies.Add(DLL) Next Parameters.GenerateInMemory = True Dim results As CompilerResults = Provider.CompileAssemblyFromSource(Parameters, Me.Source) If results.Errors.Count = 0 Then _successful = True Return results.CompiledAssembly Else _successful = False _errors = results.Errors Return Nothing End If End Function End Class
And use it like so
vb.net Code:
Dim SourceCode As String =<Source> Imports System.Windows.Forms Namespace TestCode Class Class1 Sub ShowMessage() Messagebox.Show("Hello, This code was compiled from a string!") End Sub End Class End Namespace</Source>.Value 'Very Short program Dim Dlls() As String = {"System.dll", "System.Core.dll", "System.Data.dll", "System.Windows.Forms.dll"}'Any referenced dll's Dim Compiler As New VbCompiler(SourceCode, Dlls) Dim CodeAssembly As Assembly = Compiler.Compile If Compiler.Successful Then Dim instance As Object = CodeAssembly.CreateInstance("TestCode.Class1") Dim CodeType As Type = instance.GetType Dim Info As MethodInfo = CodeType.GetMethod("ShowMessage") Info.Invoke(instance, Nothing) Else For Each i As CompilerError In Compiler.Errors MsgBox(i.ErrorText) Next 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.
-
Dec 8th, 2011, 06:35 AM
#2
Thread Starter
Fanatic Member
-
Dec 10th, 2011, 11:04 AM
#3
Lively Member
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...
-
Feb 10th, 2012, 11:29 AM
#4
New Member
-
Feb 14th, 2012, 03:35 AM
#5
Re: [VS2010] Code Compiler
Nice this is cool, may try and make something out of this if I have time. Thanks for shareing.
-
Feb 14th, 2012, 11:31 AM
#6
Thread Starter
Fanatic Member
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.
-
Feb 15th, 2012, 02:47 PM
#7
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.
-
Apr 27th, 2012, 02:58 AM
#8
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
-
Feb 19th, 2013, 10:09 AM
#9
New Member
Re: [VS2010] Code Compiler
Originally Posted by Wesleylucas
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|