Go to [http://chen1996.blog.163.com/blog/static/4441005220091117102310827/]
Preface
I have seen many of my friends write that the method of calling the DLR script in C # is complete. Now I want to explain it in detail, hoping to help you. This article mainly uses C # And ironpython scripts to demonstrate the process of interoperability between. NET and scripts.
What is DLR?
DLR (Dynamic Language Runtime) is an open-source project led by Microsoft. For. Net ApplicationsProgramProvides script skills. The current version is 0.9. Can you obtain the source from codeplex. For more information, see my blog study DLR.
Overview
There are many ways for an application to execute scripts:
- Get scriptscope through scriptruntime. excutefile
- Get scriptscope through scriptruntime. usefile
- Get scriptscope through scriptruntime. createlist, and then execute scriptings. Execute or scriptid. includefile.
- Use scriptruntime. getengine or scriptruntime. getenginebyfilename to get scriptengine. Call createscriptsourcefromstring, createscriptsourcefromfile, or createscriptsourcefromstream of scriptengine to create scriptsource, and then execute scriptsource. Execute.
- With scriptsource, you do not have to execute. You can call compile to generate compiledcode and then call compiledcode. Execute.
Reference DLR
First, you must obtain the DLL corresponding to the DLR module. You can download it from codeplex. The content is as follows:
I created a DLR directory under the project directory and copied all these files. In the Solution Explorer view, right-click "Reference" and select "add reference"
Click Browse, as shown in figure
Add "Microsoft. Scripting. Core. dll", "Microsoft. Scripting. dll", "Microsoft. Scripting. extensionattribute. dll", and other files to reference. I also don't want to go into details. For more information, see the help documentation of Visual Studio.
The referenced file will be copied to the bin directory during compilation, but the file that is not referenced will not be copied. My approach is to add these files to the project and then set copy. As I only need to use the Python language, I only copy the files "ironpython. dll", "ironpython. modules. dll", and "app. config.
Initialization
After the DLR is referenced, you must initialize the DLR.
- Use namespace
Using Microsoft. Scripting. Hosting;
- Define and create scriptruntime
Scriptruntime Env = scriptruntime. createfromconfiguration ();
Scriptscope scope;
- Load the script. In the form constructor, add the following pornographicCode
Public window1 ()
{
Initializecomponent ();
Assembly = assembly. getassembly (typeof (contact ));
Env. loadassembly (assembly );
Scope = env. executefile ("test. py ");
}
Run scriptruntime. loadassembly in the first two lines of code to enable the script to directly use the class in the current Assembly. Next, execute scriptruntime. executefile and run the test. py script.
Call the Script Function
To call a script function, you must first define a method delegate. It is the most efficient way to call scripts through delegation. Of course, you can also use the dynamicobject tryinvoke method to call the script function. Here I will introduce the delegation method.
- Define Delegation
Delegate void scriptonload (window );
The parameter format of the Delegate, which is consistent with that of the script.
- Call the Script Function
Scriptonload fun;
If (scope. trygetvariable <scriptonload> ("OnLoad ", out fun ))
Fun (this );
The above Code uses the trygetvariable root token function name to obtain the delegate. Then, you can directly call the script through delegation. Of course, you can also save the delegate to efficiently call the function.
Script call C #
Let's take a look at the test. py script.
IMPORT wpfapplication
Def onLoad (window ):
Dataprovider = Window. findresource ("mydatecollectiondatasource ")
DaTa = dataprovider. DaTa;
C = wpfapplication. Contact ("chen1996", "chen1996 is a smart person ")
DaTa. Add (c)
This script is not long, but there are three key points worth attention.
IMP in the first lineThe ORT Statement, which is equivalent to the using of C. Wpfapplication is the namespace of the C # program. I called loadassembly before initialization. Do you still remember? The scriptruntime function is used to load the Assembly for the script.
Next, the third line, window. findresource, is very simple to call the method in C. The native support of. NET is good as provided by DLR. The fourth and sixth rows are the same. It is not dependent here. during initialization, the Assembly loaded by loadassembly is used because all objects are the source parameter window.
Row 4 creates a wpfapplication. Contact object. Wpfapplication is the Assembly loaded during initialization. To use the classes in this Assembly, you must first load them. Then, you can use it like using the script's own type. Thanks to the native. net support provided by DLR, haha.
In short, the interoperability between C # And python is very simple and efficient. Likewise, this example can be applied to all scripting languages and. NET languages.