Skip to content

Commit 7df57fe

Browse files
New Crowdin translations by GitHub Action (#169)
Co-authored-by: Crowdin Bot <[email protected]>
1 parent ce03e5a commit 7df57fe

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+4324
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
title: "Introduction to the project"
3+
description: "Natasha helps you easily and dynamically invoke code"
4+
slug: /
5+
sidebar_position: 1
6+
---
7+
8+
# Natasha
9+
10+
[![Member project of .NET Core Community](https://img.shields.io/badge/member%20project%20of-NCC-9e20c9.svg)](https://github.com/dotnetcore) [![Badge](https://img.shields.io/badge/link-996.icu-red.svg)](https://996.icu/#/zh_CN) [![GitHub license](https://img.shields.io/github/license/dotnetcore/natasha.svg)](https://github.com/dotnetcore/Natasha/blob/master/LICENSE)
11+
12+
A library of C-dynamic assembly buildings based on [Roslyn](https://github.com/dotnet/roslyn) , which allows developers to add assemblies to running programs by using the dynamic build domain/assembly/class/structure/enumeration/interface/method, etc. of the C? code dynamically in the run of the program.Natasha integrates domain management/plug-in management for domain isolation, domain offloading, hot-swapping and more. The library follows a complete compilation process, provides complete error tips, automatically adds references, and a well-developed data structure building template that allows developers to focus only on assembly scripting, compatible with netcoreapp2.0 plus / netcoreapp3.0 plus, providing a cross-platform, unified, simple chain API. And we will fix your problem and reply to your [](https://github.com/dotnetcore/Natasha/issues/new)as soon as possible .
13+
14+
![Console dynamic build type](/images/console.gif)
15+
16+
![Create a Controller and delete it](/images/create_controller_deletion.gif)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
title: "Why need Natasha?"
3+
sidebar_position: 2
4+
---
5+
6+
## Knowledge reserve
7+
8+
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 as[food article](https://www.cnblogs.com/whuanle/category/1548012.html).
9+
10+
## Contemporary pillars
11+
12+
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.
13+
14+
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.
15+
16+
## The way to solve it
17+
18+
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.
19+
20+
## Simple scenario
21+
22+
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.
23+
24+
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.
25+
26+
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.
27+
28+
## plan
29+
30+
This article reveals the tip of the Natasha iceberg, and we'll explore more technology and applications.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
title: 1.域的概念与使用
3+
---
4+
5+
## Getting started
6+
7+
引用包 `DotNetCore.Natasha.Domain``DotNetCore.Natasha.CSharp.Compiler.Domain`;
8+
9+
`DotNetCore.Natasha.Domain` 为 域 的实现包。
10+
`DotNetCore.Natasha.CSharp.Compiler.Domain` 为 Natasha 编译单元与 域的粘合包。
11+
12+
## 创建域
13+
14+
```cs
15+
//如果你想将主域转化为 NatashaDomain,请使用不带参数的初始化方法,并创建后将实例保存起来,不要重复创建。
16+
var domain = new NatashaDomain();
17+
//如果你想创建非主域
18+
var domain = new NatashaDomain(key);
19+
```
20+
21+
## 加载插件
22+
23+
#### 在这之前我不得不说,一个插件如何输出完整依赖,如何屏蔽引用接口,请参照[微软文档](https://learn.microsoft.com/en-us/dotnet/core/tutorials/creating-app-with-plugin-support#simple-plugin-with-no-dependencies)
24+
25+
### 插件加载
26+
27+
`LoadPlugin` 方法允许用户传入插件文件路径,返回程序集。
28+
29+
### 程序集比较器
30+
31+
Natasha.Domain 自实现了程序集比较逻辑,通过 SetAssemblyLoadBehavior(AssemblyCompareInfomation loadBehavior) 方法来指定程序集依赖加载行为,例如 "AssemblyCompareInfomation.UseHighVersion" 枚举传入,将导致插件加载过程中,插件所依赖的程序集与共享域已存在的程序集进行比较,如果程序集名相同,则加载版本较高的程序集。
32+
33+
### 封装 API
34+
35+
Natasha.Domain 合并了插件加载方法和程序集比较器产生了 4 个 API 方法,`LoadPluginWithHighDependency` / `LoadPluginWithLowDependency` / `LoadPluginUseDefaultDependency` / `LoadPluginWithAllDependency`.
36+
就拿第一个 API 来说,如果比对过程中找到的依赖版本有高有低,则选择高版本的依赖,而非加载低版本依赖,在使用过程中记得看注释,有问题要去 Github 提问。
37+
38+
### 注意事项
39+
40+
ALC 不允许加载引用程序集,因为引用程序集不可执行。
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
---
2+
title: 10. 模板初始化操作
3+
---
4+
5+
Natasha 的所有模板均继承自 ComplierTemplate ,ComplierTemplate 本身会提供静态构造方法。因此上层 API 也会被支持。Therefore, the upper-level API is also supported.因此上层 API 也会被支持。
6+
7+
<br/>
8+
9+
## 使用
10+
11+
NDelegate / NAssembly / NClass.. / xxx_Oerator 等等以下称为 “Handler”.
12+
13+
<br/>
14+
15+
#### 静态初始化代码:
16+
17+
```cs
18+
19+
//使用 domain 域
20+
Handler.UseDomain(domian, compiler => { 编译器配置 });
21+
22+
23+
//使用某编译器的域
24+
Handler.UseCompiler(assemblyCSharpCompiler, compiler => { 编译器配置 }));
25+
26+
27+
//创建一个 "domainJim" 域
28+
Handler.CreateDomain("domianJim", compiler => { 编译器配置 });
29+
30+
31+
//使用默认域
32+
Handler.DefaultDomain(compiler => { 编译器配置 });
33+
34+
35+
//使用随机域
36+
Handler.RandomDomain(compiler => { 编译器配置 });
37+
38+
```
39+
40+
<br/>
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
---
2+
title: 2.1 智能编译模式 - 实现程序集
3+
---
4+
5+
## 为何使用实现程序集
6+
7+
实现程序集包括逻辑的具体实现和私有字段,预热内存涨幅较少,但实现程序集具有敏感的依赖关系,需要小心使用。
8+
9+
## Prerequisite
10+
11+
1. 引入 `DotNetCore.Natasha.CSharp.Compiler.Domain` Core 版本的编译域包。
12+
2. 使用预热方法为智能编译做准备。
13+
3. 智能编译。
14+
15+
## 预热案例
16+
17+
### 普通预热
18+
19+
1. 泛型预热方法将自动创建 编译域创建者 单例。
20+
2. 传入 true 参数以达到禁用运行时程序集的作用,Natasha 将会选择内存程序集预热。
21+
3. 第一个参数指是否从内存程序集中提取 Using Code, 设置为 true 将从实现程序集中提取 Using。
22+
4. 第二个参数指是否从内存程序集中提取 元数据,设置为 true 将从实现程序集中提取 元数据。
23+
24+
```cs
25+
//注册编译域并预热方法
26+
NatashaManagement.Preheating<NatashaDomainCreator>(true, true);
27+
```
28+
29+
### Using缓存预热
30+
31+
第一次生成将 Using Code 写入缓存文件 Natasha.Namespace.cache 中,后续重启会自动从文件中加载。
32+
33+
```cs
34+
//注册编译域并预热方法
35+
NatashaManagement.Preheating<NatashaDomainCreator>(true, truetrue);
36+
```
37+
38+
### 分开预热
39+
40+
```cs
41+
//注册编译域
42+
NatashaManagement.RegistDomainCreator<NatashaDomainCreator>();
43+
//预热方法
44+
NatashaManagement.Preheating(true, true);
45+
```
46+
47+
### 过滤预热
48+
49+
```cs
50+
//如果存在 Dapper 主版本高于 12 的程序集,则不要将它加入缓存。
51+
//传入排除方法
52+
NatashaManagement.Preheating<NatashaDomainCreator>((asmName, name) => {
53+
if (asmName.Name != null)
54+
{
55+
if (asmName.Name.Contains("Dapper") && asmName.Version!.Major > 12)
56+
{
57+
return true;
58+
}
59+
}
60+
return false;
61+
},true, truetrue);
62+
```
63+
64+
> 也许您的环境过于复杂,从而遇到一些意外,请您精简代码之后,确保异常能够重现,提交 issue 给 Natasha。
65+
66+
## 智能编译
67+
68+
在预热后请参考以下代码
69+
70+
```cs
71+
AssemblyCSharpBuilder builder = new();
72+
var myAssembly = builder
73+
.UseRandomDomain()
74+
.UseSmartMode() //启用智能模式
75+
.Add("public class A{ }")
76+
.GetAssembly();
77+
```
78+
79+
智能模式将合并 共享域与当前域的 元数据以及 Using, 并启用语义检查.
80+
智能模式的 API 逻辑为:
81+
82+
```cs
83+
.WithCombineReferences(item => item.UseAllReferences())
84+
.WithCombineUsingCode(UsingLoadBehavior.WithAll)
85+
.WithReleaseCompile()
86+
.WithSemanticCheck();
87+
```
88+
89+
可以参考[元数据管理与微调] 对 元数据 的合并行为进行微调。
90+
可以参考[微调Using覆盖] 对 UsingCode 的合并行为进行微调。
91+
92+
## 其他案例
93+
94+
#### 批量排除程序集
95+
96+
```cs
97+
NatashaInitializer.Preheating((asmName, name) => {
98+
if (name != null)
99+
{
100+
if (name.Contains("System"))
101+
{
102+
if (
103+
name.Contains("Net") ||
104+
name.Contains("Xml") ||
105+
name.Contains("IO") ||
106+
name.Contains("Reflection") ||
107+
name.Contains("Threading") ||
108+
name.Contains("Security") ||
109+
name.Contains("Diagnostics") ||
110+
name.Contains("Data") ||
111+
name.Contains("Resources.") ||
112+
name.Contains("Drawing") ||
113+
name.Contains("Text") ||
114+
name.Contains("Globalization") ||
115+
name.Contains("Service") ||
116+
name.Contains("Web") ||
117+
name.Contains("Window") ||
118+
name.Contains("ComponentModel")
119+
)
120+
{
121+
//排除
122+
return true;
123+
}
124+
return false;
125+
}
126+
if (name.Contains("Natasha"))
127+
{
128+
//加载
129+
return false;
130+
}
131+
if (name.Contains("ConsoleApp3"))
132+
{
133+
//加载
134+
return false;
135+
}
136+
}
137+
return true;
138+
},true, true);
139+
```
140+
141+
#### 根据版本排除程序集
142+
143+
该示例使用 AssemblyName 进行判断程序集名称及版本, 以下代码排除了 dapper 主版本号为 12 的程序集引用文件;
144+
145+
```cs
146+
NatashaInitializer.Preheating((asmName, name) => {
147+
if (asmName.Name != null)
148+
{
149+
if (asmName.Name.Contains("Dapper") && asmName.Version!.Major > 12)
150+
{
151+
return true;
152+
}
153+
}
154+
return false;
155+
});
156+
```
157+
158+
> 减少程序集引用文件的加载,可以有效的控制内存涨幅.

0 commit comments

Comments
 (0)