background:
I used to develop Java in the past and used ORM frameworks such as Mybatis and Hiberante. Recently, I need to start a C# project. Since it is not very difficult, I don’t want to learn the ORM framework of C# anymore, so I thought about using reflection to simply implement the content of the ORM framework, simple addition, deletion, modification and search, and no connection between multiple tables is used.
reflection:
Reflection in Java and C# is roughly the same, mainly refers to a ability that a program can access, detect and modify its own state or behavior, and can adjust or modify the state and related semantics of the behavior described by the application based on the state and results of its own behavior. My understanding is that you can dynamically obtain the properties and methods of objects when the program is running, and you can make related calls.
First, let’s take a look at the reflection implementation in C#:
To obtain Type objects, reflection operations need to be performed through Type objects.
Get it with a fully qualified name Type tp = (""); Get it through class Type tp = typeof(Int)
After obtaining the Type object, we can create the object through its constructor method.
Calling parameterless construct
// Get the initialization construction information of the class ConstructorInfo ct = (); // Call the constructor without parametersT newObj = (T)(null);
Call parameter construct
//Define parameter type array Type[] tps = new Type[2]; tps[0] = typeof(int); tps[1] = typeof(string); //Get the initialization parameter information of the class ConstructorInfo ct2 = (tps); //Define parameter array object[] obj = new object[2]; obj[0] = (object)100; obj[1] = (object)"Param Example"; //Calling the constructor with parameters ExampleClass Ex2 = (ExampleClass)(obj);
Get all public fields
// Get all public fieldsFieldInfo[] arr = (); // Assign a value to the specified field. An object needs to be passed in newObj(newObj, r[name]);
Here are the methods that can be introduced. All information in the class can be obtained through reflection, and can be called, and can also break the encapsulation (unsafe)
practise
The following is to automatically encapsulate the result set obtained from the database into the bean through reflection. No manual packaging required
public static T dataToObj(String str) { String strSql = str; DataSet ds = (strSql); Type t = typeof(T); DataRow r = [0].Rows[0]; // Find a line FieldInfo[] arr = (); // Return all public fields (public) ConstructorInfo ct = (); T newObj = (T)(null); if (r != null) { foreach (FieldInfo f in arr)// traverse all fields { string name = ; Type type2 = ; if (r[name].GetType() != typeof(DBNull)) { string typeName = ; (newObj, r[name]); } } } else { newObj = default(T); } (); return newObj; }
Encapsulate to List
public static List<T> dataToList(String str) { List<T> list = new List<T>(); String strSql = str; DataSet ds = (strSql); Type t = typeof(T); FieldInfo[] arr = (); // Return all public fields (public) ConstructorInfo ct = (); foreach (DataRow dr in [0].Rows) { T newObj = (T)(null); foreach (FieldInfo f in arr)// traverse all fields { string name = ; Type type2 = ; string typeName = ; if (dr[name].GetType() != typeof(DBNull)) { (newObj, dr[name]); } } (newObj); } (); return list; }
Splice strings for insert operation
public static void inserByBean(string tableName, T target) { StringBuilder sql = new StringBuilder(); // Spliced sql ("insert into "+tableName+"("); Type t = (); PropertyInfo[] ps = (); for (int i = 0; i < ; i++) { object obj = ps[i].GetValue(target, null); if (obj != null) { string name = ps[i].Name; if (i != - 1) { (" " + name + ","); } else { (" " + name + ""); } } } (") values("); for (int i = 0; i < ; i++) { object obj = ps[i].GetValue(target, null); if (obj != null) { if (i != - 1) { if (ps[i].PropertyType == typeof(string) || ps[i].PropertyType == typeof(DateTime)) { ("'" + obj + "',"); } else { ("" + obj + ","); } } else { if (ps[i].PropertyType == typeof(string) || ps[i].PropertyType == typeof(DateTime)) { ("'" + obj + "')"); } else { ("" + obj + ")"); } } } } string resultSql = (); (resultSql); }
The above article C# preliminary exploration of the implementation principle of the ORM framework through reflection (detailed explanation) is all the content I have shared with you. I hope you can give you a reference and I hope you can support me more.