Is there a way to dynamically execute a string in .net, similar to eval() in javascript or dynamic sql in sql?

前端 未结 8 1421
心在旅途
心在旅途 2020-12-05 12:39

Is there a way to dynamically execute code contained in a string using .net 2.0, in a similar way to eval() in javascript or using sp_executeSQL in tsql?

I have a st

8条回答
  •  攒了一身酷
    2020-12-05 13:17

    It's not tooo hard ;) I put together a little example. This should help you decide if you want to use dynamic scripts.. or regexes.

    What you can do is create an interface in your assembly, which your dynamic code will implement:

    namespace CompileScriptExample
    {
      public interface IStringManipulator
      {
        string processString(string aString);
      }
    }
    

    Then create a ScriptRunner class:

    namespace CompileScriptExample
    { 
    public class ScriptRunner
    {
    
        public static string RunScript(string scriptCode, string scriptParameter)
        {
    
            CodeDomProvider provider = new Microsoft.CSharp.CSharpCodeProvider();
    
            //configure parameters
            CompilerParameters parameters = new CompilerParameters();
            parameters.GenerateExecutable = false;
            parameters.GenerateInMemory = true;
            parameters.IncludeDebugInformation = false;
            string reference;
            // Set reference to current assembly - this reference is a hack for the example..
            reference = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            parameters.ReferencedAssemblies.Add(reference+"\\CompileScriptExample.exe");
    
            //compile
            CompilerResults results = provider.CompileAssemblyFromSource(parameters, new string[] { scriptCode });
    
            if (results.Errors.Count == 0)
            {
                IStringManipulator compiledScript=(IStringManipulator)FindInterface(results.CompiledAssembly, "IStringManipulator");
                return compiledScript.processString(scriptParameter);//run the script, pass the string param..
            }
            else
            {
                foreach(CompilerError anError in results.Errors)
                {
                    MessageBox.Show(anError.ErrorText);
                }
                //handle compilation errors here
                //..use results.errors collection
                throw new Exception("Compilation error...");
            }
        }
    
        private static object FindInterface(Assembly anAssembly, string interfaceName)
        {
            // find our interface type..
            foreach (Type aType in anAssembly.GetTypes())
            {
                if (aType.GetInterface(interfaceName, true) != null)
                    return anAssembly.CreateInstance(aType.FullName);
            }
            return null;
        }
    }
    

    }

    Now all you have to do is create a script string with code that implements your interface like..

    string myScriptString=@"using CompileScriptExample;
    public class MyStringManipulator : IStringManipulator
    {
      public string processString(string aString)
      {
            return aString+aString;
      }
    };
    

    and then.. in your code, make use of the ScriptRunner to process your string with your dynamic code:

    string processedString = ScriptRunner.RunScript(myScriptString, "hello");
    

提交回复
热议问题