In our. NET project, to use the custom formula function, we take advantage of the IronPython lambda function, we can easily calculate the value, but we found that if the expression involved in the property changes, the formula must be recalculated, how to analyze the expression to know exactly which properties to access?
In our. NET project, to use the custom formula function, we take advantage of the IronPython lambda function, we can easily calculate the value, but we found that if the expression involved in the property changes, the formula must be recalculated, how to analyze the expression to know exactly which properties to access?
A careful study found that IronPython provides such a feature:
|
The following are the referenced contents: systemstate state = new systemstate ();
Compilercontext context = new Compilercontext ();
Parser p = parser.fromstring (state, Context, "ActiveObject.Contact.Address + ' ds");
IronPython.Compiler.Ast.Expression ex = P.parsetestlistasexpression (); |
The code above analyzes the expression: ActiveObject.Contact.Address + ' ds ', where a parser class can be used to parse the expression, using the VS Object viewer to understand the result:
The problem has not been completely solved, I think the result has, I can not be recursive all the structure? Wow, there are more than 20 types, look, um, and this:
|
The following are the referenced contents: Class Program {
static void Main (string[] args) {
systemstate state = new systemstate ();
Compilercontext context = new Compilercontext ();
Parser p = parser.fromstring (state, Context, "ActiveObject.Contact.Address + ' ds");
IronPython.Compiler.Ast.Expression ex = P.parsetestlistasexpression ();
Mywalker w = new Mywalker ();
Ex. Walk (w);
}
}
Class Mywalker:astwalker {
public override bool Walk (fieldexpression node) {
Console.WriteLine ("Walk:{0},{1}", node. Name, node. Target);
Return base. Walk (node);
}
} |
cool!! The virtual method I overloaded was invoked two times, telling me that I had access to 2 fields.
The structure description class of the lambda was provided in. NET 3.5, but I did not find a class for dynamic compilation analysis. A friend of the blog Park said: "There was a dynamicexpression class that provided dynamic compilation, but now the. NET 3.5 has been chopped off, but it has the source code in LINQ samples.
Note: This program uses the 1.0 version of IronPython, the 2.0 version of the method has been different. Here is the code for 2.0:
class Program {
static void Main (string[] args) {
//engine
IronPython.Hosting.PythonEngine engine = IronPython.Hosting.PythonEngine.Current Engine;
//Code units
Sourcecodeunit unit = new Sourcecodeunit (engine, "ActiveObject.Contact.Address + ' ds '");
//context and options
Compilercontext contexts = new Compilercontext (unit);
ironpython.pythonengineoptions option = new Ironpython.pythonengineoptions ();
//Parse expression
Parser p = parser.createparser (context,option);
IronPython.Compiler.Ast.Expression ex = P.parseexpression ();
//recursively find
Mywalker w = new Mywalker ();
Ex. Walk (w);
}
}
class Mywalker:pythonwalker {
public override bool Walk (memberexpression node) {
Console.WriteLine ("Walk:{0},{1}", node. Name, node. Target);
return base. Walk (node);
}
} |