Super-strong-type reflection (1)

Source: Internet
Author: User
Tags reflector
Everyone knows LINQ Introduced standard query Operators To Make the query C # The most important concepts in language. But I wonder if you realize that, LINQ It can also be used for other purposes than query. I will discuss it for the first time below LINQ Used to query other fields. Reflection Problems : Away from type Security Error At least for myself , Use C # For this type of security language CTRL + Shift + B ( Or Shift + F6 ) Key Proceed Compile time , There will be a feeling of ease and peace of mind. I know that running errors that are weird and difficult to debug due to the type of errors used, and prompts such as missing Methods almost never appear. However, when I use reflection, if I'm not careful, the notorious Targetinvocationexception And some Nullreferenceexception Exceptions. Below is what I met : Methodinfo Method =
Typeof ( Mock ) . Getmethod ( "Publicmethodparameters" , Newtype [] { Typeof ( String ), Typeof ( Int )})); If Mock Class Application refactoring to rename this method , What will happen? ? If the parameter type is changed , What will happen? ? There is no doubt that it will happen Runtime exception ! If a flexible plug-in framework with a large amount of reflection is used, This is by no means a small problem. Application refactoring (or expensive) is inevitable because you are afraid of making mistakes.CodeCapabilities. Then, try to replace the magic string ( Magic strings ) And the loose type array, what will happen? Methodinfo Info= Reflector. Method <mock, String, int> ( (X, y, z)=> X. publicmethodparameters (y, z )); Use LINQ for strong type reflection Its working principle is passed as a parameter. λ Expression ( Just like the previous version . Net Same as the delegate in ) It does not have to be executed. The above code basically constructs a method that can call a given method of the type. λ Expression. The type of the object type of the declaration method is Method <> static generic The first type parameter of the method. The optional type parameter you can specify will be the parameter type of the method you want to call (if any ). If you want to obtain Methodinfo , The expression will be: Methodinfo Info = Reflector. Method <mock> (
X => X. publicmethodnoparameters ()); This is better than what you have seen before λ Expressions are typical. In λ Expression. If you want to pass additional parameters, you must put all the content in brackets (in the above example X , Y And Z ). The ing function for attributes and fields is the same: Propertyinfo Property =
Reflector. Property <mock> (x => X. publicproperty ); Fieldinfo Field =
Reflector. Field <mock> (x => X. publicfield ); Use Expression Tree Now let's start to discuss interesting content, that is, how to implement it. In LINQ Medium , Any receipt λ Expression ( Delegate type ) Methods can be converted to receive the same delegate type Expression <t> Method , You do not need to change the client code. For example : Privatestaticvoid Dosomething ( Predicate <mock> predicate) Can be replaced: Privatestaticvoid Dosomething (
Expression <predicate <mock> predicate) In both cases , The call code can be the same λ Expression : Dosomething (x => X. value> 25 ); What happens here is , The compiler will not pass the pointer to the anonymous Delegate of the second method signature. , Instead, it is constructed in the form of an expression tree. AST ( Abstract syntax tree ) Of Il Code. If you open Reflector (The name of my type reflection class also comes from this. It is the greatest tool that any senior developer should always use .) Dosomething You can see that: Parameterexpression Expression1 =
Expression. parameter (typeof ( Mock ), "X "); Program. dosomething (
Expression. Lambda <predicate <mock> (
Expression. gt (expression. Field (
Expression1, fieldof (Mock. Value )), Expression. Constant (0x19, typeof (INT )) ), Newparameterexpression [] { Expression1 }) ) ;

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.