Reflection practice Accumulation

Source: Internet
Author: User
Background

In a service implementation, there are many similar structures and slightly different functions.

Master Martin told us that we can use the extract method for refactoring.

 

This exercise mainly uses reflection.

Record every day's work in the form of scattered knowledge points.

 

Content 1. Get type

There are two methods to obtain data:

1.1 typeof Method

var entityType = typeof(T);typeof(DataRow);

1.2 type. GetType () method

var daoType = dao.GetType();
2. constructorinfo

Constructorinfo is used to represent the constructor;

Obtained through type. getconstructor;

For example, obtain a constructor with the datarow parameter.

entityType.GetConstructor(new Type[] { typeof(DataRow) });
3. methodinfo

The constructor is represented by methodinfo;

Obtained through type. getmethod;

For example, a function named get with the specified feature parameter type is obtained.

var daoGetMethodInfo = daoType.GetMethod("Get", GetDaoGetMethodParameterTypes(exprBpk).ToArray());
4. properties propertyinfo

This is the most commonly used, so I won't say much about it, for example:

entityType.GetProperty(memberName);
5. Expression expression

In addition, when expression <func <t, Object> is used to pass parameters, the benefit is that all information can be obtained --Code is Data.

For example, expr = ZW => ZW. staffcode

VaR membername = (expr. Body as memberexpression). Member. Name; // indicates staffcodevar memberexprtype = (expr. Body as memberexpression). type; // indicates the type of staffcode, Which is string

 

Make a little progress every day ~~~

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.