Skip to content

Commit

Permalink
New Crowdin translations by GitHub Action
Browse files Browse the repository at this point in the history
  • Loading branch information
crowdin-bot committed Jan 10, 2024
1 parent f73dff3 commit 1996090
Show file tree
Hide file tree
Showing 36 changed files with 2,190 additions and 80 deletions.
40 changes: 0 additions & 40 deletions src/i18n/en-US/docusaurus-plugin-content-docs/current/00-1-Why.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,6 @@ Since this thing is so important, why not wrap it up?Officials offer a relativel

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.

## Project first knowledge

Natasha adopts the MIT open source protocol and has been tested in practice, and can be used by referencing DotNetCore.Natasha.CSharp.

Natasha's most basic compilation unit is AssemblyCSharpBuilder, which has many properties:

- CompileErorBehavior compiles the wrong behavior, defaulting to throwing an exception;
- The behavior of SyntaxError Behavior syntax errors, which defaults to throwing exceptions;
- AssemblyOutputKind assembly output to file or memory stream;
- OutputFold assembly output directory, default to dynamicLibraryFolds directory under current APP;
- CustomUsingShut uses a customer-defined using reference;

Build a Test class in a random domain

```cs
Initialize the Natasha compilation components and environment
NatashaInitializer.Preheating();
// Create a compilation unit and specify the assembly name
AssemblyCSharpBuilder oop = new AssemblyCSharpBuilder("myAssembly");
//Compilation unit uses random domains assigned from domain management
oop. Domain = DomainManagement.Random();
//Add code to the compilation unit
oop. Add(@"namespace HelloWorld{ public class Test{ public Test(){ Name = null; } public string Name; } }");
// gets the class in the assembly according to the short name, and the long name is "HelloWorld.Test"
Type type = oop. GetTypeFromShortName("Test");
```

The above shows the construction of strings, for which Natasha provides a number of templates to simplify operations such as NClass, which can be converted to:
```cs
var type = NClass
. RandomDomain()
. Namespace("HelloWorld")
. Name("Test")
. Ctor(item => item. Public(). Body("Name = null;" ))
. PublicField<string>("Name")
. GetType();
```

Note that this assembly belongs to a certain immediate domain, and you can use type. DisposeDomain() to remove the reference so that the GC can uninstall it. The main domain (the domain where the program starts) is also called the shared domain, the random domain can get the types and data in the main domain, the call between the random domains is not allowed, this feature also makes the later plug-in programming a big hand.

## 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
title: 1.域的概念与使用
---

## 开始

引用包 `DotNetCore.Natasha.Domain``DotNetCore.Natasha.CSharp.Compiler.Domain`;

`DotNetCore.Natasha.Domain` 为 域 的实现包。
`DotNetCore.Natasha.CSharp.Compiler.Domain` 为 Natasha 编译单元与 域的粘合包。

## 创建域

```cs
//如果你想将主域转化为 NatashaDomain,请使用不带参数的初始化方法,并创建后将实例保存起来,不要重复创建。
var domain = new NatashaDomain();
//如果你想创建非主域
var domain = new NatashaDomain(key);
```

## 加载插件

#### 在这之前我不得不说,一个插件如何输出完整依赖,如何屏蔽引用接口,请参照[微软文档](https://learn.microsoft.com/en-us/dotnet/core/tutorials/creating-app-with-plugin-support#simple-plugin-with-no-dependencies)

### 插件加载

`LoadPlugin` 方法允许用户传入插件文件路径,返回程序集。

### 程序集比较器

Natasha.Domain 自实现了程序集比较逻辑,通过 SetAssemblyLoadBehavior(AssemblyCompareInfomation loadBehavior) 方法来指定程序集依赖加载行为,例如 "AssemblyCompareInfomation.UseHighVersion" 枚举传入,将导致插件加载过程中,插件所依赖的程序集与共享域已存在的程序集进行比较,如果程序集名相同,则加载版本较高的程序集。

### 封装 API

Natasha.Domain 合并了插件加载方法和程序集比较器产生了 4 个 API 方法,`LoadPluginWithHighDependency` / `LoadPluginWithLowDependency` / `LoadPluginUseDefaultDependency` / `LoadPluginWithAllDependency`.
就拿第一个 API 来说,如果比对过程中找到的依赖版本有高有低,则选择高版本的依赖,而非加载低版本依赖,在使用过程中记得看注释,有问题要去 Github 提问。

### 注意事项

ALC 不允许加载引用程序集,因为引用程序集不可执行。
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
---
title: 2.1 智能编译模式 - 实现程序集
---

## 为何使用实现程序集

实现程序集包括逻辑的具体实现和私有字段,预热内存涨幅较少,但实现程序集具有敏感的依赖关系,需要小心使用。

## 前提条件

1. 引入 `DotNetCore.Natasha.CSharp.Compiler.Domain` Core 版本的编译域包。
2. 使用预热方法为智能编译做准备。
3. 智能编译。

## 预热案例

### 普通预热

1. 泛型预热方法将自动创建 编译域创建者 单例。
2. 传入 true 参数以达到禁用运行时程序集的作用,Natasha 将会选择内存程序集预热。
3. 第一个参数指是否从内存程序集中提取 Using Code, 设置为 true 将从实现程序集中提取 Using。
4. 第二个参数指是否从内存程序集中提取 元数据,设置为 true 将从实现程序集中提取 元数据。

```cs
//注册编译域并预热方法
NatashaManagement.Preheating<NatashaDomainCreator>(true, true);
```

### Using缓存预热

第一次生成将 Using Code 写入缓存文件 Natasha.Namespace.cache 中,后续重启会自动从文件中加载。

```cs
//注册编译域并预热方法
NatashaManagement.Preheating<NatashaDomainCreator>(true, truetrue);
```

### 分开预热

```cs
//注册编译域
NatashaManagement.RegistDomainCreator<NatashaDomainCreator>();
//预热方法
NatashaManagement.Preheating(true, true);
```

### 过滤预热

```cs
//如果存在 Dapper 主版本高于 12 的程序集,则不要将它加入缓存。
//传入排除方法
NatashaManagement.Preheating<NatashaDomainCreator>((asmName, name) => {
if (asmName.Name != null)
{
if (asmName.Name.Contains("Dapper") && asmName.Version!.Major > 12)
{
return true;
}
}
return false;
},true, truetrue);
```

> 也许您的环境过于复杂,从而遇到一些意外,请您精简代码之后,确保异常能够重现,提交 issue 给 Natasha。
## 智能编译

在预热后请参考以下代码

```cs
AssemblyCSharpBuilder builder = new();
var myAssembly = builder
.UseRandomDomain()
.UseSmartMode() //启用智能模式
.Add("public class A{ }")
.GetAssembly();
```

智能模式将合并 共享域与当前域的 元数据以及 Using, 并启用语义检查.
智能模式的 API 逻辑为:

```cs
.WithCombineReferences(item => item.UseAllReferences())
.WithCombineUsingCode(UsingLoadBehavior.WithAll)
.WithReleaseCompile()
.WithSemanticCheck();
```

可以参考[元数据管理与微调] 对 元数据 的合并行为进行微调。
可以参考[微调Using覆盖] 对 UsingCode 的合并行为进行微调。

## 其他案例

#### 批量排除程序集

```cs
NatashaInitializer.Preheating((asmName, name) => {
if (name != null)
{
if (name.Contains("System"))
{
if (
name.Contains("Net") ||
name.Contains("Xml") ||
name.Contains("IO") ||
name.Contains("Reflection") ||
name.Contains("Threading") ||
name.Contains("Security") ||
name.Contains("Diagnostics") ||
name.Contains("Data") ||
name.Contains("Resources.") ||
name.Contains("Drawing") ||
name.Contains("Text") ||
name.Contains("Globalization") ||
name.Contains("Service") ||
name.Contains("Web") ||
name.Contains("Window") ||
name.Contains("ComponentModel")
)
{
//排除
return true;
}
return false;
}
if (name.Contains("Natasha"))
{
//加载
return false;
}
if (name.Contains("ConsoleApp3"))
{
//加载
return false;
}
}
return true;
},true, true);
```

#### 根据版本排除程序集

该示例使用 AssemblyName 进行判断程序集名称及版本, 以下代码排除了 dapper 主版本号为 12 的程序集引用文件;

```cs
NatashaInitializer.Preheating((asmName, name) => {
if (asmName.Name != null)
{
if (asmName.Name.Contains("Dapper") && asmName.Version!.Major > 12)
{
return true;
}
}
return false;
});
```

> 减少程序集引用文件的加载,可以有效的控制内存涨幅.
Loading

0 comments on commit 1996090

Please sign in to comment.