user595809
user595809

Reputation:

Using the DynamicMethod for scripting

This is described in the article C# scripts using DynamicMethod Pluses I see - the first call will occur much faster than using CSharpCodeProvider.

What are the disadvantages of this method?

Upvotes: 9

Views: 780

Answers (2)

David Nelson
David Nelson

Reputation: 3714

DynamicMethod requires writing IL directly. The author of the article has apparently written his own compiler to translate C# scripts into IL that can be loaded into the DynamicMethod, but that is likely to be very fragile. CSharpCodeProvider uses csc, the same compiler that runs in Visual Studio (almost), so it is likely to be a lot more reliable.

Upvotes: 1

Peter Long
Peter Long

Reputation: 4012

I just finished reading the source code of C# Scripts using DynamicMethod

I think the most intolerant disadvantage is: tooooooooooo complex. In .Net 4.0, we could use DLR and ironpython to do scripting with 5% lines of code as using DynamicMethod. DLR is newer and it is the trend.

some code example for DLR and IronPython:

var scriptEngine = Python.CreateEngine();
var scriptSource = scriptEngine.CreateScriptSourceFromString(@"# coding=utf-8
def execute(command):
    exec(command)
");
scriptScope = scriptEngine.CreateScope();
scriptSource.Execute(scriptScope);
dynamic execute = scriptScope.GetVariable("execute");
execute("print 'hello world'")

pseudo code only, you'd have to modify the above code in order to compile and run. I wrote the above code to show you how easy it will be if you use DLR and Ironpython instead of DynamicMethod.

Upvotes: 3

Related Questions