Emit Learning (4), emit Learning
In the previous article, this article mainly demonstrates the process of converting db data to classes in the form of stacks.
I. first look at the first image
When the program runs to 176 rows (the Code posted in the previous article), the first stack appears.
So before that, what did Dapper do? Aside from the OpCodes Implementation Method of Dapper, we use code to convert the data as follows:
1. first, it must be obtained. Which of the columns read from the Database need to be converted? If it is select *, all columns will be read, however, I don't need so many columns, and the classes I receive may not have so many columns. Therefore, first determine which columns need to be converted, and what type of columns are read from the database.
2. after a valid column is determined, you can obtain the constructor in the class, which is used when the class is created. when getting constructors, the simpler the constructors, the better. (In Dapper, the constructor marked with the ExplicitConstructor attribute is checked first, then the constructor parameters are obtained, and then the parameters are initialized), and the class is new.
3. now, we can actually know the source data type and target property/feild type. Now we know the types on both sides, and the db data is ready, target class is new, so you can implement conversion. Different types are used for conversion. it is worth mentioning that, if the target class contains attributes or fields of the custom class, Dapper will not continue to convert the data. Simply give null. this function can also be implemented in Dapper.
Now, back to Dapper. In fact, his work is also like this. The order may be slightly different. The order of 1 and 2 is adjustable. but the implementation method is slightly different. all major routes pass Rome, and the destination is the same. The difference is the scenery on the way.
2. Next, the second figure
The first stack in is a situation that occurs after the execution of 181 lines of code. The meaning of 180 lines and 181 code is to copy the reader [index] To loc2, this loc2 is the local variable with the object type declared in the previous step (row 161). Therefore, from the stack perspective, before row 180 is executed, it is the same as the stack after the execution of Row 3. so I won't draw any images.
Before Unbox_Any is executed, the source type and target type are known, and the source type and target type can be converted. Then, the Unbox_Any operation is performed, unbox_Any is to split the box into the type you need
The above two images are the stack change process under normal circumstances (int, string, datetime, double, etc.). It should be better understood.