Jump to the main content

Why need Natasha?

Knowledge reserve

Up to now, there are still many people asking what dynamic compilation is, what scenes Emit is used in, how to use the expression tree, it is recommended that if you do not know the students first to find information to learn, such asfood article.

Contemporary pillars

Dynamic compilation is one of the key pillars supporting the contemporary .NET ecosystem, and this technology "serves" a variety of official and unofficial libraries.The core of dynamic compilation is MSIL, and writing IL code officially provides Emit technology, but writing and maintaining Emit is painful, such as syntax sugar reduction, asynchronous methods, instruction compatibility, large amounts of metadata manipulation, error debugging, dynamic automata, dynamic building of complex businesses, and so on. I'm afraid only the author of the open source project knows the expression.

Since this thing is so important, why not wrap it up?Officials offer a relatively human expression tree, but this only improves the ease of writing, but introduces new things, such as expression tree-based configuration and parsing.

The way to solve it

The emergence of Roslyn gave me hope of an eco-flowering that made Emit transparent and allowed us to compile dynamically using C# code.This is Wang Dao, brothers, this is the new era of the great color of the way ah.This scheme is tantamount to a bird gun for a gun, some people may care more about the official attitude, it can be said that the official is recommended to do so, but you can not control the problem, the cost of this technology is reference dependency, error handling, domain loading and other issues.After a series of research, collation, and testing, Natasha has addressed these issues and is officially available to the public in 2019, and now Natasha has released a stable version in the netcore/net5 release, and over the course of the past two years, we have gone from integration to decoupling, from mid-term to new technology applications, natasha is taking a light route from no-> -> -> -> .Next I'll go into more detail about Natasha's application in conjunction with the sample.

Simple scenario

At this point we learned how to use Natasha to build classes, we can try to complete some simple scenarios ourselves, such as implementing an AOP proxy class, if you know enough about AOP proxy classes, I believe that in 5 minutes you can have a small success.

There have been written examples before for reference. Contoller can also be built dynamically to implement dynamic API functionality, but this requires knowledge of MVC/WebAPI. You can also use FreeSql and other CODEFirst-enabled ORMs to create table structures during program initialization, some corporate entities require low intrusiveness, or even zero intrusiveness, at which point you can use Natasha to build temporary classes with labeled intrusions to replace the entity classes to complete some of the functions.

Natasha is a real dynamic practical project, the examples listed above all need to have a certain programming foundation, if there is no need and thinking advice not to be rigid.


This article reveals the tip of the Natasha iceberg, and we'll explore more technology and applications.