Hello!

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!

-Prims0

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
        parameters.ReferencedAssemblies.Add("System.dll")

        Dim compileUnit As CodeCompileUnit = New CodeCompileUnit()
        Dim daniWeb As CodeNamespace = New CodeNamespace("DaniWeb")
        compileUnit.Namespaces.Add(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)
        quickExample.Members.Add(member)
        daniWeb.Types.Add(quickExample)

        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

        Console.Read()


    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;
        parameters.ReferencedAssemblies.Add("System.dll");

        CodeCompileUnit compileUnit = new CodeCompileUnit();
        CodeNamespace daniWeb = new CodeNamespace("DaniWeb");
        compileUnit.Namespaces.Add(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);
        quickExample.Members.Add(member);
        daniWeb.Types.Add(quickExample);

        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]);


        Console.Read();
    }
}
commented: thanks i'm looking for this vb code .... +2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.