This article is a mirror article of machine translation, please click here to jump to the original article.

View: 54151|Reply: 3

[Source] .NET/C# Reflection, Emit, Expression performance testing

[Copy link]
Posted on 2021-3-21 13:23:34 | | | |
Review of articles about reflections:

.net/c# takes advantage of the reflection dynamic execution method
https://www.itsvse.com/thread-4771-1-1.html

C# uses reflection to determine whether a property is assigned
https://www.itsvse.com/thread-3045-1-1.html

C# reflection gets the displayname of the object property
https://www.itsvse.com/thread-3219-1-1.html
About EMIT Review:

.NET/C# Emit dynamically generates assemblies
https://www.itsvse.com/thread-9597-1-1.html
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:


.NET/C# uses BenchmarkDotNet to test code performance
https://www.itsvse.com/thread-9576-1-1.html


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!




Previous:.NET/C# Emit dynamically generates assemblies
Next:.NET Core calls gRPC HTTP (non-TLS protected) address error solutions
 Landlord| Posted on 2025-4-28 14:53:41 |
Use Expression to assign values to object properties:


Call:
 Landlord| Posted on 2025-4-28 15:32:51 |
.NET/C# Use UnsafeAccessor to modify read-only field contents
https://www.itsvse.com/thread-10997-1-1.html
 Landlord| Posted on 2025-4-28 15:40:57 |
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com