In my application, i want to make the user write the code by himself.
Like, he have a textbox, there he is going to put the code like, msgbox("Hello") then when he press the button, the msgbox will open.

So, anyone out there got any ideas?

Thank you!


Here's an example in VB and C#. This is adapted from the first result I found in google for "create code on the fly C#".

Imports System.CodeDom
Imports System.CodeDom.Compiler
Imports System.Reflection

Module Module1

    Sub Main()

        Dim provider As CodeDomProvider = New Microsoft.VisualBasic.VBCodeProvider()
        Dim compiler As ICodeCompiler = provider.CreateCompiler()
        Dim parameters As CompilerParameters = New CompilerParameters()

        parameters.GenerateExecutable = False
        parameters.GenerateInMemory = True
        parameters.IncludeDebugInformation = False

        Dim compileUnit As CodeCompileUnit = New CodeCompileUnit()
        Dim daniWeb As CodeNamespace = New CodeNamespace("DaniWeb")
        daniWeb.Imports.Add(New CodeNamespaceImport("System"))
        Dim quickExample As CodeTypeDeclaration = New CodeTypeDeclaration("QuickExample")

        Dim code As String = "Public Shared Sub ExecuteCode() " & vbCrLf & "Console.WriteLine(""code generated on the fly"")" & vbCrLf & " End Sub"

        Dim member As CodeSnippetTypeMember = New CodeSnippetTypeMember(code)

        Dim results As CompilerResults = compiler.CompileAssemblyFromDom(parameters, compileUnit)

        Dim t As Type = results.CompiledAssembly.GetType("DaniWeb.QuickExample")
        Dim info As MethodInfo = t.GetMethod("ExecuteCode")
        Dim o As Object = Nothing
        If Not (info Is Nothing) Then
            o = info.Invoke(Nothing, Nothing)
        End If


    End Sub

End Module
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Reflection;

class Program
    static void Main(string[] args)
        CodeDomProvider provider = new Microsoft.CSharp.CSharpCodeProvider();

        ICodeCompiler compiler = provider.CreateCompiler();
        CompilerParameters parameters = new CompilerParameters();

        parameters.GenerateExecutable = false;
        parameters.GenerateInMemory = true;
        parameters.IncludeDebugInformation = false;

        CodeCompileUnit compileUnit = new CodeCompileUnit();
        CodeNamespace daniWeb = new CodeNamespace("DaniWeb");
        daniWeb.Imports.Add(new CodeNamespaceImport("System"));
        CodeTypeDeclaration quickExample = new CodeTypeDeclaration("QuickExample");

        string code = @"public static void ExecuteCode() { Console.WriteLine(""code generated on the fly""); }";

        CodeSnippetTypeMember member = new CodeSnippetTypeMember(code);

        CompilerResults results = compiler.CompileAssemblyFromDom(parameters, compileUnit);

        Type t = results.CompiledAssembly.GetType("DaniWeb.QuickExample");
        MethodInfo info = t.GetMethod("ExecuteCode");
        object o = null;
        if (info != null)
            o = info.Invoke(null, new object[0]);

thanks i'm looking for this vb code ....
This article has been dead for over six months. Start a new discussion instead.