Ma Dude
Ma Dude

Reputation: 557

Dynamically Execute C# Code with .NET Framework Support

string dyncode = "return param1 + \" \" + param2;";
CodeDomProvider provider = CodeDomProvider.CreateProvider("C#");
string src = @"
    using System;
    using System.Text;
    using System.Text.RegularExpressions;
    public class CustomTextFunction {
        public string f(param1, param2) {
            " + dyncode + @"
        }
    }
";
CompilerResults compiled = provider.CompileAssemblyFromSource(new CompilerParameters(), src);
if (compiled.Errors.Count == 0) {
    Type type = compiled.CompiledAssembly.GetType("CustomTextFunction");
    MessageBox.Show((string)type.GetMethod("f").Invoke(Activator.CreateInstance(type), new string[] { "Hello", "World" }));
} else {
    foreach (object error in compiled.Errors) {
        MessageBox.Show(error.ToString());
    }
}

The code above basically just returns param1 and param2 separated by a space. param1 and param2 is "Hello" "World" respectively.

The issue is that System.Text.RegularExpressions doesnt exist as its part of the .NET Framework. (Yes im aware its not being used for anything) Is there a way to support .Net Framework with this approach?

Im basically looking for a way for users to enter C# code manually (and have them supply the using's used) and then just execute it (always expecting it returning a string).

Upvotes: 0

Views: 92

Answers (2)

Access Denied
Access Denied

Reputation: 9461

You need to add reference to System.dll

System.Text.RegularExpressions is a namespace and not the assembly.

And your C# snippet contains error there should be types for parameters.

    var dyncode = "return param1 + \" \" + param2;";
    CodeDomProvider provider = CodeDomProvider.CreateProvider("C#");

  string src = @"
    using System;
    using System.Text;
    using System.Text.RegularExpressions;
    public class CustomTextFunction {
        public string f(string param1, string param2) {
            " + dyncode + @"
        }
    }
";
            var parameters = new CompilerParameters();
            parameters.ReferencedAssemblies.Add("System.dll");



            CompilerResults compiled = provider.CompileAssemblyFromSource(parameters, src);
            if (compiled.Errors.Count == 0)
            {
                Type type = compiled.CompiledAssembly.GetType("CustomTextFunction");
                Console.WriteLine((string)type.GetMethod("f").Invoke(Activator.CreateInstance(type), new string[] { "Hello", "World" }));
            }
            else
            {
                foreach (object error in compiled.Errors)
                {
                    Console.WriteLine(error.ToString());
                }
            }
            Console.ReadKey();

Upvotes: 1

Serdar
Serdar

Reputation: 797

As you said in your comment you need to add System.Text.RegularExpressions as reference to CodeDomProvider.

You can achieve this with the following code;

provider.ReferencedAssemblies.Add("dll path");

There is also one more work around solution for this case. If you will run the application from just one machine you can add the required library to GAC (Global Application Cache). If you want to apply this approach you can follow this link

Upvotes: 1

Related Questions