Review of articles about reflections:
About EMIT Review:
First, create a simple object, instantiate the object, and use emit and expression to read and modify the value of the object, and you can see that the execution result is as expected, as shown in the figure below:
Benchmark using BenchmarkDotNet for benchmarking, reading only object values for testing, running on Net472 and NetCoreApp31, the test results are as follows:
Summary:
Both emit and expression are more efficient than using reflection to get values, and under the .NET 4.7.2 framework, emit is better than expression execution efficiency, but under the .NET Core 3.1 framework, expression is better than emit execution.
If your program only needs to dynamically execute an object once, it is recommended to use reflection (after all, there is no need to encapsulate again or create a delegate), and if your program needs to dynamically execute an object multiple times, it is recommended to use emit or expression, and of course, further encapsulation is required.
The test code is as follows:
When using Expression to obtain object values, the error will be reported as follows:
Expression of type 'System.Int32' cannot be used for return type 'System.Object' I really don't know how these people use it, they only know how to copy and paste!
|