Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize dynamic compilation in .net standard

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Net standard how to achieve dynamic compilation, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Background:

First, in the previous article, we realized the transparent invocation of micro-services for consumers through the framework, but we need to write a client proxy for each service, which is extremely tedious. Second, in the project, the front-end site uses the traditional .net Framework framework, and the back-end micro-services we use. Net Core framework transformation, it is not realistic to adjust the front-end site to .net Core framework in a short time, in order to support both frameworks. How to automatically create client-side proxies for micro-services based on. Net Standard framework has become a problem that we must solve.

Problem transformation

Let's take a brief look back at what we now expect the microservice client agent to look like:

According to the above analysis, we only need to pass each method in the service interface to determine whether there is a return value. If there is a return value to call the Invoke method, but there is no return value, call the InvokeWithoutReturn method, and then pass the interface name, method name and method parameters sequentially. If you are familiar with Java, this problem can be easily solved. You can use dynamic proxy to create such an anonymous class, but in the. net world, the implementation of dynamic proxy is very troublesome.

First of all, I think of the realization of Emit through the intermediate language IL, but this is really unfriendly to use. After several twists and turns, I finally choose to give up, and then I think that you can actually dynamically generate this code fragment and load it into the system assembly after dynamic compilation. So under the guidance of this direction, we try to realize this problem step by step.

Solution

How to generate this code snippet? Through the above analysis, we know that we only need to reflect the interface to get the public methods, and copy each method signature of the interface as is, and then call the relevant methods in the RemoteServiceProxy base class according to whether the interface method has a return value. However, we need to pay special attention to the generic method translation. The following is the reference implementation for generating this code snippet.

1. Find out the assembly file for the service interface and process each file

2. Deal with the interface types in each file and find out the dependent assemblies of each assembly for later dynamic compilation

Handle each method in the interface

Get generic type string

How to add dependencies

Since you are compiling the source code, the dependencies in the source code are essential. In the previous step, we found out the dependencies of each assembly, and then we sorted out all these dependencies.

Compile

With code snippets and compiled assembly dependencies, then comes the most important compilation.

Conclusion

After the above processing, it is not perfect, but it has successfully achieved what we expected. In the previous GetService, when we found that it belonged to a remote service, we only needed to return the proxy object in the form similar to the following. At the same time, in order to increase the call more smoothly, we set the timing of this compilation to occur when the program starts, and of course ps may have some other more appropriate timing.

After reading the above, have you mastered the method of dynamic compilation in. Net standard? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report