0
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).

1
  • From what im assuming, I either need to use a different Provider or possibly reference/add stuff in CompilerParameters? Commented Oct 3, 2018 at 8:56

2 Answers 2

1

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();
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you! The parameter issue was actually just an error with my example, it isnt present in my code (i was thinking of javascript lmao).
Using System.dll worked perfectly but will this System.dll exist for all systems I could possibly run this on?
@MaDude actually if you use scripts it's better to use jint. github.com/sebastienros/jint
@MaDude As far as I know Compiling code is not a crossplatform feature and requires full .net profile.
But there is crossplatform roslyn: josephwoodward.co.uk/2016/12/…
|
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.