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 ~~~