9

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?

3
  • 3
    What specifically do you mean by "disadvantages"? To some degree, advantage/disadvantage depends on how you wish to use it! What do you want to use it for? I can think of a handful of disadvantages, but before I answer, I guess I want a bit clearer explanation of the question. Commented Jun 19, 2011 at 20:15
  • I want to use C# for scripting in their applications. I can use the DynamicMethod or CSharpCodeProvider for the purpose. I would like to make an informed choice CSharpCodeProvider vs DynamicMethod. Commented Jun 22, 2011 at 19:38
  • Are you looking for an example of where you can dynamically execute c# code from within c#? (Asking this question because of one of the responses) Commented Jun 25, 2011 at 1:11

2 Answers 2

3
+25

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.

Sign up to request clarification or add additional context in comments.

4 Comments

C# or VB are much more common languages ​​than IronPython. I would like to use C# for their scripting.
They are all .NET languages. not so different as you think
Can you give an example for the DLR and C#?
IronPython is pretty dramatically different from C# or VB.NET. The fact that they all ultimately execute against the same runtime does not in any way mean that someone who is familiar with C# will be able to pickup and use IronPython right away. The article alex mentioned is offering to do exactly that in a scripting form.
1

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.

1 Comment

But with CSahrpCodeProdvider you get an assembly not a DynamicMethod. Four years ago it was not possible to create a DynamicMethod from a string of code, but the article OP linked to provides an option.

Your Answer

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