Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement Linq Func

2025-03-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you how to achieve Linq Func in Linq, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

In Linq, any method that receives a lambda expression (delegate type) can be converted to a method that receives Expression of the same delegate type without changing the client code. For example:

Privatestaticvoid DoSomething (Predicate predicate)

Can be replaced by:

Privatestaticvoid DoSomething (Expression predicate)

In both cases, the calling code can be the same lambda expression:

DoSomething (x = > x.Value > 25)

What happens here is that instead of passing the pointer to the anonymous delegate of the second method signature, the compiler generates IL code that builds the AST (abstract syntax tree) in the form of an expression tree. If you open Reflector (hence the name of my type reflection class, which is the greatest tool that any advanced developer should use frequently) and cancel the method call to DoSomething, you can see:

ParameterExpression expression1 = Expression.Parameter (typeof (Mock), "x"); Program.DoSomething (Expression.Lambda (Expression.GT (Expression.Field (expression1, fieldof (Mock.Value)), Expression.Constant (0x19, typeof (int)), newParameterExpression [] {expression1})

Here you can see how the compiler builds the entire expression using static methods on the Expression class (my detailed view of API is discussed separately). Of course, in the method implementation, you can check the same tree and do whatever you want. The Linq CTP of * * includes a cool visualization tool that can be used to see what's going on in the expression tree when the runtime reaches your method body. By now, you should understand that I'm implementing a strongly typed reflection: I take an expression tree and search for method call nodes (or, for properties and fields, member access). The following is the implementation of the Method method:

PublicstaticMethodInfo Method (Expression method) {return GetMethodInfo (method);} privatestaticMethodInfo GetMethodInfo (Expression method) {LambdaExpression lambda = method asLambdaExpression; if (lambda = = null) thrownewArgumentNullException ("method"); MethodCallExpression methodExpr = null; / / our Operation returns an object, so you can first name a type conversion (if the method does not return an object) or a direct method call. If (lambda.Body.NodeType = = ExpressionType.Cast) {/ / Type conversion is a unary operation, and the Operand is a method call expression. MethodExpr = (UnaryExpression) lambda.Body). Operand asMethodCallExpression;} elseif (lambda.Body.NodeType = = ExpressionType.MethodCall | | lambda.Body.NodeType = = ExpressionType.MethodCallVirtual) {methodExpr = lambda.Body asMethodCallExpression;} if (methodExpr = = null) thrownewArgumentException ("method"); return methodExpr.Method;}

What I created is the Operation delegate type. Cannot use Linq Func (as well as T, Arg0... Because they return Boolean values I need more flexible objects, simply the object that returns the object, and the delegate "overload" that receives some fixed parameter type (such as Func). So I got the following:

PublicdelegateobjectOperation (); publicdelegateobjectOperation (T declaringType); publicdelegateobjectOperation (T declaringType, A 0 arg0);

Note that users of API never know about the existence of objects of these delegate types, just as users of query operators never know that Func exists. I hope that in the future these commissions will disappear and replace them with something better (maybe publicdelegateobject Operation

< params T>

;). In addition, notice how I add parameters of the new parameter type to the "after" of T, which is an overloaded generic transformation, which is the opposite of the function in Linq Func.

These are all the contents of the article "how to achieve Linq Func in Linq". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report