架构师_程序员_码农网

Retrieve password
Register

QQ登录

Just one step to get started

Search
Views:31322|Responses: 0
打印 上一主题 下一主题

[ NET/C# Reflection, Emit, Expression Performance Tests

[Copy link] .
跳转到指定楼层
owner of the building
发表于 2021-3-21 13:23:34| 看该作者回帖奖励|ReverseBrowse|Read Mode
About Reflections Article Review:

.net/c# Dynamic Execution Methods Using Reflection
h ttps:// www.itsvse.com/thread-4771-1-1.html

c# Determining if a property is assigned a value through reflection
h ttps:// www.itsvse.com/thread-3045-1-1.html

c# Reflection to get the displayname of an object property
h ttps:// www.itsvse.com/thread-3219-1-1.html
About emit Review:

NET/C# Emit Dynamically Generated Assemblies
h ttps:// www.itsvse.com/thread-9597-1-1.html
First, create a new simple object, instantiate the object, use emit and expression to read and modify the value of the object, you can see that the results are as expected, as shown below:



BenchmarkDotNet was used for benchmarking, only object values were read for testing, and the running platforms were Net472 and NetCoreApp31, the test results are as follows:


NET/C# using BenchmarkDotNet to test code performance.
h ttps:// www.itsvse.com/thread-9576-1-1.html


To summarize:

Using both emit and expression is more efficient than using reflection to get a value. emit outperforms expression in the .NET 4.7.2 framework, but expression outperforms emit in the .NET Core 3.1 framework.

If your program only needs to dynamically execute an object once, it is recommended to use reflection (after all, you don't need to encapsulate it again, and you don't need to create a delegate), if your program needs to dynamically execute an object several times, it is recommended to use emit or expression, and of course, it needs to be encapsulated further.

The test code is as follows:

When using Expression to get the value of an object, the online code will report the following error:

Expression of type 'System.Int32' cannot be used for return type 'System.Object'
I really don't know how these people use it, just copy and paste!




Previous: NET/C# Emit Dynamically Generated Assemblies
Next article: .NET Core call gRPC HTTP (non-TLS protected ) address error solution
NET, only published in the process of practice, encountered technical difficulties, do not mislead others.
You need to log in before you can post back Log in | Register

This version of the integral rules

DISCLAIMER:
All software, programming materials or articles published by Code Farmer are limited to be used for learning and research purposes only; the above contents shall not be used for commercial or illegal purposes, otherwise, all the consequences shall be borne by the users themselves. This site information from the network, copyright dispute has nothing to do with this site. You must completely remove the above content from your computer within 24 hours of downloading. If you like the program, please support the genuine software, buy registration and get better genuine service. If there is any infringement, please contact us by email to deal with it.

Mail To:help@itsvse.com

QQ| ( ICP备14021824号-2 )|Sitemap

GMT+8, 2024-9-19 00:59

Quick ReplyBack to topBack to list