Skip to content

Commit 8bdb104

Browse files
committed
发布最新扩展库文档
1 parent 5fa2cac commit 8bdb104

File tree

12 files changed

+256
-2
lines changed

12 files changed

+256
-2
lines changed

src/docs/compile/004-Metadata.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ loadContext.AddReferenceAndUsingCode(refAssemblyFilePath);
3030

3131
单独添加 [元数据引用]
3232
```cs
33+
loadContext.AddReferences()
34+
//
3335
loadContext.ReferenceRecorder.AddReference(
3436
AssemblyName assemblyName,
3537
MetadataReference reference,
@@ -39,6 +41,8 @@ loadContext.ReferenceRecorder.AddReference(
3941

4042
### 单独增加 [Using Code]
4143
```cs
44+
loadContext.AddUsing()
45+
//
4246
loadContext.UsingRecorder.Using(string? @using);
4347
loadContext.UsingRecorder.Using(IEnumerable<string> @using);
4448
loadContext.UsingRecorder.Using(Assembly assembly);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
title: "7. Natasha 的异常"
3+
---
4+
5+
## 使用方法
6+
7+
Natasha 在编译时出错会抛出异常,异常 Model 如下:
8+
```cs
9+
public sealed class NatashaException : Exception
10+
{
11+
12+
//格式化后的脚本字符串
13+
public string Formatter;
14+
15+
//错误类型
16+
public NatashaExceptionKind ErrorKind;
17+
18+
//roslyn诊断集合
19+
public List<Diagnostic> Diagnostics;
20+
21+
/// <summary>
22+
/// 详细的编译信息
23+
/// </summary>
24+
public string CompileMessage;
25+
26+
}
27+
```
28+
29+
### 如何监控和获取异常
30+
31+
Natasha 的事件执行流程如下:
32+
1. 添加语法树时:
33+
- 使用 FastAddScriptWithoutCheck,则不会抛出异常。
34+
- 使用 Add 则会进行语法检查,并抛出异常。
35+
2. 编译时:
36+
- 编译后先触发 LogCompilationEvent 事件,用来获取编译后的信息。
37+
- 如果编译成功会继续引发 CompileSucceedEvent 事件。
38+
- 如果编译失败会继续引发 CompileFailedEvent 事件。
39+
3. 编译周期之外:
40+
- 编译过后,可以通过 GetException() 获取异常(可能为空)。
41+
File renamed without changes.

src/docs/compile/007-Codecov.md renamed to src/docs/compile/009-Codecov.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: "7. 使用 Codecov 的扩展"
2+
title: "9. 使用 Codecov 的扩展"
33
---
44

55
## 使用方法
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
title: "10. 使用 CompileDirector 的扩展"
3+
---
4+
## 介绍
5+
6+
这是一个可以让编译单元不断学习和矫正 using code 的库,在重复的编译场景中,几乎每次 using code 和元数据引用都大同小异,甚至不会改变,
7+
该库允编译单元在编译成功时学习有用的 using code 以备后用。
8+
9+
## 使用方法
10+
11+
1. 引入 `DotNetCore.Natasha.CSharp.Extension.CompileDirector` 扩展包。
12+
2. 编码。
13+
14+
### 编译导演
15+
```cs
16+
//传入采样计数阈值,不传默认为 2.
17+
//采样计数会影响 CompileDirector 的“学习”效率.
18+
//每次编译成功,该编译单元成功的 using 计数 + 1.
19+
//当 using 计数大于采样计数阈值时,将被 CompileDirector 内部的 UsingCache 录用为正式 using.
20+
//正式 using 每次会优先覆盖脚本
21+
CompileDirector director = new CompileDirector(3);
22+
23+
//配置每次从该场景中产生的 编译单元。
24+
director.ConfigBuilder(builder => builder
25+
.ConfigSyntaxOptions(opt => opt.WithPreprocessorSymbols("DEBUG"))
26+
.WithDebugCompile()
27+
.ConfigLoadContext(ctx => ctx
28+
.AddReferenceAndUsingCode<object>()
29+
.AddReferenceAndUsingCode(typeof(Math))
30+
.AddReferenceAndUsingCode(typeof(File))
31+
));
32+
33+
//生产编译单元
34+
//由 director 生产的编译单元会自动覆盖已经被录用的 using code.
35+
var builder = director.CreateBuilder();
36+
37+
//编译单元
38+
builder.Add(@"public static class A
39+
{
40+
public static void Show()
41+
{
42+
File.WriteAllText(""1.txt"", ""1"");
43+
Console.WriteLine(Math.Abs(-4));
44+
}
45+
}");
46+
47+
//通过 director 获取程序集
48+
// 成功时继续挑选和计数录用 using code
49+
// 失败时将使用对应的 using 覆盖策略进行重编译
50+
// 通过 ConfigUsingConverSrategy API 配置失败时的覆盖策略
51+
var asm = director.GetAssembly(builder);
52+
asm.GetDelegateFromShortName<Action>("A", "Show")!();
53+
```
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
title: "11. 使用 MethodCreator 的扩展"
3+
---
4+
## 介绍
5+
6+
MethodCreator 扩展库允许开发者快速构建动态委托。
7+
8+
## 使用方法
9+
10+
1. 引入 `DotNetCore.Natasha.CSharp.Extension.MethodCreator` 扩展包。
11+
2. 编码。
12+
13+
### 共分为两种模式
14+
#### 精简构造模式
15+
精简构造模式需要自己管理元数据和using
16+
```cs
17+
var func = "return arg1 + arg2 + 0.1;"
18+
.WithMetadata<double>()
19+
.ToFunc<double, double, double>();
20+
```
21+
#### 智能构造模式
22+
智能构造模式将自动覆盖元数据和 using
23+
```cs
24+
var func = "return arg1 + arg2 + 0.1;"
25+
.ToFunc<double, double, double>();
26+
```
27+
28+
<br/>
29+
<br/>

src/i18n/zh-Hans/docusaurus-plugin-content-docs/current/compile/004-Metadata.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ loadContext.AddReferenceAndUsingCode(refAssemblyFilePath);
3030

3131
单独添加 [元数据引用]
3232
```cs
33+
loadContext.AddReferences()
34+
//
3335
loadContext.ReferenceRecorder.AddReference(
3436
AssemblyName assemblyName,
3537
MetadataReference reference,
@@ -39,6 +41,8 @@ loadContext.ReferenceRecorder.AddReference(
3941

4042
### 单独增加 [Using Code]
4143
```cs
44+
loadContext.AddUsing()
45+
//
4246
loadContext.UsingRecorder.Using(string? @using);
4347
loadContext.UsingRecorder.Using(IEnumerable<string> @using);
4448
loadContext.UsingRecorder.Using(Assembly assembly);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
---
2+
title: "7. Natasha 的异常"
3+
---
4+
5+
## 使用方法
6+
7+
Natasha 在编译时出错会抛出异常,异常 Model 如下:
8+
```cs
9+
public sealed class NatashaException : Exception
10+
{
11+
12+
//格式化后的脚本字符串
13+
public string Formatter;
14+
15+
//错误类型
16+
public NatashaExceptionKind ErrorKind;
17+
18+
//roslyn诊断集合
19+
public List<Diagnostic> Diagnostics;
20+
21+
/// <summary>
22+
/// 详细的编译信息
23+
/// </summary>
24+
public string CompileMessage;
25+
26+
}
27+
```
28+
29+
### 如何监控和获取异常
30+
31+
Natasha 的事件执行流程如下:
32+
1. 添加语法树时:
33+
- 使用 FastAddScriptWithoutCheck,则不会抛出异常。
34+
- 使用 Add 则会进行语法检查,并抛出异常。
35+
2. 编译时:
36+
- 编译后先触发 LogCompilationEvent 事件,用来获取编译后的信息。
37+
- 如果编译成功会继续引发 CompileSucceedEvent 事件。
38+
- 如果编译失败会继续引发 CompileFailedEvent 事件。
39+
3. 编译周期之外:
40+
- 编译过后,可以通过 GetException() 获取异常(可能为空)。
41+
File renamed without changes.

src/i18n/zh-Hans/docusaurus-plugin-content-docs/current/compile/007-Codecov.md renamed to src/i18n/zh-Hans/docusaurus-plugin-content-docs/current/compile/009-Codecov.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: "7. 使用 Codecov 的扩展"
2+
title: "9. 使用 Codecov 的扩展"
33
---
44

55
## 使用方法

0 commit comments

Comments
 (0)