IronPython Analysis lambda expression

Source: Internet
Author: User

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);
}
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.