LINQ series (8) -- Application Scenario of Expression Tree

Source: Internet
Author: User

Hello everyone, I haven't written a blog here for a long time. Recently, the situation has been very bad, and it has suffered a crush. As a result, I have encountered a great psychological problem recently, so I have never been willing to write blogs. I read the previous blog post before I write it today. The application scenario should be written as planned.

I believe you can use the previous articlesArticleYou have a certain understanding of the Expression Tree. For this series, I may only write two more articles.

You can understand that the expression tree consists of two parts: Body and parameter. Then, all functional expression trees that can be implemented by methods can be implemented. The expression tree can be used to represent a logical process, and allows users to pass in parameter. That is, the expression tree can make the logical process independent.

Therefore, in my usual project, in an application scenario, I will first think of using the expression tree.

Next, I will explain this application scenario for you:

For example, we have a class: aircraft, which has an attribute: airlineid

Public   Class Aircraft
{
Public   String Airlineid
{
Get ;
Set ;
}

PublicAircraft ()
{
}

Public BoolTest ()
{
Return True;
}
}

At the same time, we have another subfleet class, which also has an airlineid attribute.

Public   Class Subfleet
{
Public   String Airlineid
{
Get ;
Set ;
}
Public Subfleet ()
{
}
}

These two classes do not have any inheritance relationships. When we use the instances of these two classes in our solution, we need to determine that airlineid is not null and not empty. This is just an example. In actual projects, the logic of the judgment we need may be far more complex than not null or not empty. Therefore, we hope to write a general method to judge all objects containing the airlineid attribute.

Now, the generic and expression trees are used. We can use the expression tree to write the following method:

Public   Bool Testairlineid < T > (T instance)
{
Parameterexpression aircraftparameter = Expression. parameter ( Typeof (T ), " Testinstance " );
Memberexpression memberairlineidexpression = Expression. Property (aircraftparameter, " Airlineid " );
Constantexpression nullconstant = Expression. Constant ( Null );
Constantexpression emptyconstant = Expression. Constant ( String . Empty );

Expression < Func < T, Bool > Limit null = Expression. Lambda < Func < T, Bool >
(expression. Equal (memberairlineidexpression, nullconstant),
New Parameterexpression [] {aircraftparameter}
);

Expression < Func < T, Bool > Equalempty = Expression. Lambda < Func < T, Bool >
(
Expression. Equal (memberairlineidexpression, emptyconstant ),
New Parameterexpression [] {aircraftparameter}
);

Expression < Func < T, Bool > Expressionstring = Expression. Lambda < Func < T, Bool >
(
Expression. Or (empty null. Body, empty. Body ),
New Parameterexpression [] {aircraftparameter}
);

Func<T,Bool>Method=Optional null. Compile ();

ReturnMethod (instance );
}

Okay. In this way, all objects containing the "airlineid" attribute can use this method to determine whether the attribute is not empty and not empty.

But there is a problem with this. What if the input object does not include the "airlineid" attribute? It is not a runtime exception, can we check errors without this attribute during compilation?

The answer is yes. You only need to pass in a memberexpression. If the input is a memberexpression, you do not need to connect to the generic expression. But in this way, the caller is in trouble. I wrote in the previous blog post that you can weigh against this type of case.

Well, today I will write so much. I will write another summary article to end this series. After this series is over, I will write a series about Silverlight & prism. I hope you will pay attention to it.

If you have any questions, please contact me via my mailbox zxhlelva@126.com and QQ: 244873327.

My name is Edward.

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.