Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,32 @@ Implementing assemblies include the specific implementation and private fields o

## Preheating Examples

## 链式预热

从 V9 版本起,Natasha 支持链式预热:

```cs
NatashaManagement
//获取链式构造器
.GetInitializer()
//使用引用程序集中的命名空间
.WithRefUsing()
//使用引用程序集中的元数据
.WithRefReference()
//使用内存中的命名空间
.WithMemoryUsing()
//使用内存中的元数据
.WithMemoryReference()
//使用文件来持久化缓存 命名空间
.WithFileUsingCache()
//过滤哪些元数据是不能用的,被排除的
.WithExcludeReferences((asm, asmStr) => { return false; })
//注册域构造器
.Preheating<NatashaDomainCreator>();
```

如果不指定相关 API ,预热将跳过此行为,例如只写 WithXXXReference 不写 using 相关的 API, 那么 Natasha 预热时将只对元数据进行操作,不会缓存 using code. 这样做的好处是实现了高度定制化,按需预热。

### Ordinary Preheating

1. The generic preheating method will automatically create a singleton for the compilation domain creator.
Expand All @@ -22,17 +48,39 @@ Implementing assemblies include the specific implementation and private fields o
4. The second parameter indicates whether to extract 'Metadata' from memory assemblies. Set it to true to extract 'Metadata' from implementing assemblies.

```cs
//Register the compilation domain and preheat method
//注册编译域并预热方法
NatashaManagement.Preheating<NatashaDomainCreator>(true, true);
//或者 V9 版本
NatashaManagement
//获取链式构造器
.GetInitializer()
//使用引用程序集中的命名空间
.WithMemoryUsing()
//使用内存中的元数据
.WithMemoryReference()
//注册域构造器
.Preheating<NatashaDomainCreator>();
```

### Using Cache Preheating

The first generation will write the 'Using Code' into the cache file 'Natasha.Namespace.cache', and subsequent restarts will automatically load it from the file.
指定第三个参数,第一次生成将 Using Code 写入缓存文件 Natasha.Namespace.cache 中,后续重启会自动从文件中加载。

```cs
//Register the compilation domain and preheat method
NatashaManagement.Preheating<NatashaDomainCreator>(true, true, true);
//注册编译域并预热方法
NatashaManagement.Preheating<NatashaDomainCreator>(true, true,true);
//或者 V9 版本
NatashaManagement
//获取链式构造器
.GetInitializer()
//使用引用程序集中的命名空间
.WithMemoryUsing()
//使用内存中的元数据
.WithMemoryReference()
//使用文件来持久化缓存 命名空间
.WithFileUsingCache()
//注册域构造器
.Preheating<NatashaDomainCreator>();
```

### Separate Preheating
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,32 @@ Reference Assembly does not include the implementation and will not cause unexpe

## Preheating Examples

### 链式预热

从 V9 版本起,Natasha 支持链式预热:

```cs
NatashaManagement
//获取链式构造器
.GetInitializer()
//使用引用程序集中的命名空间
.WithRefUsing()
//使用引用程序集中的元数据
.WithRefReference()
//使用内存中的命名空间
.WithMemoryUsing()
//使用内存中的元数据
.WithMemoryReference()
//使用文件来持久化缓存 命名空间
.WithFileUsingCache()
//过滤哪些元数据是不能用的,被排除的
.WithExcludeReferences((asm, asmStr) => { return false; })
//注册域构造器
.Preheating<NatashaDomainCreator>();
```

如果不指定相关 API ,预热将跳过此行为,例如只写 WithXXXReference 不写 using 相关的 API, 那么 Natasha 预热时将只对元数据进行操作,不会缓存 using code. 这样做的好处是实现了高度定制化,按需预热。

### Ordinary Preheating

1. The generic preheating method will automatically create a singleton for the compilation domain creator.
Expand All @@ -23,17 +49,39 @@ Reference Assembly does not include the implementation and will not cause unexpe
4. The second parameter specifies whether to extract the metadata from the memory assembly. Setting it to false will extract the metadata from the reference assembly.

```cs
//Register the compilation domain and preheating method
//注册编译域并预热方法
NatashaManagement.Preheating<NatashaDomainCreator>(false, false);
//或 V9 版本
NatashaManagement
//获取链式构造器
.GetInitializer()
//使用引用程序集中的命名空间
.WithRefUsing()
//使用引用程序集中的元数据
.WithRefReference()
//注册域构造器
.Preheating<NatashaDomainCreator>();
```

### Using Cache Preheating

The first generation will write the 'Using Code' into the cache file 'Natasha.Namespace.cache', and subsequent restarts will automatically load it from the file.

```cs
//Register the compilation domain and preheating method
NatashaManagement.Preheating<NatashaDomainCreator>(false, false, true);
//注册编译域并预热方法
NatashaManagement.Preheating<NatashaDomainCreator>(false, false,true);
//或 V9 版本
NatashaManagement
//获取链式构造器
.GetInitializer()
//使用引用程序集中的命名空间
.WithRefUsing()
//使用引用程序集中的元数据
.WithRefReference()
//使用文件来持久化缓存 命名空间
.WithFileUsingCache()
//注册域构造器
.Preheating<NatashaDomainCreator>();
```

### Separate Preheating
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ loadContext.AddReferenceAndUsingCode(refAssemblyFilePath);
Add [metadata references] separately

```cs
loadContext.AddReferences()
//或
loadContext.ReferenceRecorder.AddReference(
AssemblyName assemblyName,
MetadataReference reference,
Expand All @@ -44,6 +46,8 @@ The purpose of the third parameter is: when the assemblyName already exists in t
### Add [Using Code] separately

```cs
loadContext.AddUsing()
//或
loadContext.UsingRecorder.Using(string? @using);
loadContext.UsingRecorder.Using(IEnumerable<string> @using);
loadContext.UsingRecorder.Using(Assembly assembly);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,18 @@ builder.SetDllFilePath/SetPdbFilePath/SetCommentFilePath();
Natasha has assembly-related configurations

```cs
//Output the full assembly
//输出完整程序集
builder.OutputAsFullAssembly();
//Output the referenced assembly, which by default does not include private members
//输出引用程序集,此时将默认不包含私有成员
builder.OutputAsRefAssembly();
//Include private members when outputting
//输出时包含私有成员
builder.WithPrivateMembers();
//Do not include private members when outputting
//输出时不包含私有成员
builder.WithoutPrivateMembers();

//The compilation result is a referenced assembly, written to a file, and not loaded into the domain.
//编译结果为引用程序集,且写入文件,且不会加载到域。
builder
.OutputAsRefAssembly();
.WithFileOutput()
.WithoutInjectToDomain();
.CompileWithoutAssembly();
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
title: 7. Natasha 的异常
---

## 使用方法

Natasha 在编译时出错会抛出异常,异常 Model 如下:

```cs
public sealed class NatashaException : Exception
{

//格式化后的脚本字符串
public string Formatter;

//错误类型
public NatashaExceptionKind ErrorKind;

//roslyn诊断集合
public List<Diagnostic> Diagnostics;

/// <summary>
/// 详细的编译信息
/// </summary>
public string CompileMessage;

}
```

### 如何监控和获取异常

Natasha 的事件执行流程如下:

1. 添加语法树时:

- 使用 FastAddScriptWithoutCheck,则不会抛出异常。
- 使用 Add 则会进行语法检查,并抛出异常。

2. 编译时:

- 编译后先触发 LogCompilationEvent 事件,用来获取编译后的信息。
- 如果编译成功会继续引发 CompileSucceedEvent 事件。
- 如果编译失败会继续引发 CompileFailedEvent 事件。

3. 编译周期之外:

- 编译过后,可以通过 GetException() 获取异常(可能为空)。
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
title: 8. 使用 Natasha 的类型扩展
---

```cs

Example:


typeof(Dictionary<string,List<int>>[]).GetRuntimeName();
//result: "Dictionary<String,List<Int32>>[]"


typeof(Dictionary<string,List<int>>[]).GetDevelopName();
//result: "System.Collections.Generic.Dictionary<System.String,System.Collections.Generic.List<System.Int32>>[]"

typeof(Dictionary<,>).GetDevelopNameWithoutFlag();
//result: "System.Collections.Generic.Dictionary<,>"


typeof(Dictionary<string,List<int>>[]).GetAvailableName();
//result: "Dictionary_String_List_Int32____"


typeof(Dictionary<string,List<int>>).GetAllGenericTypes();
//result: [string,list<>,int]


typeof(Dictionary<string,List<int>>).IsImplementFrom<IDictionary>();
//result: true


typeof(Dictionary<string,List<int>>).IsSimpleType();
//result: false


typeof(List<>).With(typeof(int));
//result: List<int>

```

<br/>
<br/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
---
title: 9. 使用 Codecov 的扩展
---

## 使用方法

1. 引入 `DotNetCore.Natasha.CSharp.Extension.Codecov` 扩展包。
2. 编码。

```cs
builder.WithCodecov();
Assembly asm = builder.GetAssembly();
List<(string MethodName, bool[] Usage)>? list = asm.GetCodecovCollection();
Show(list);


public static void Show(List<(string MethodName, bool[] Usage)> list)
{
for (int i = 0; i < list!.Count; i++)
{
if (list[i].Usage != null)
{
if (list[i].Usage.Length == 0)
{
Console.WriteLine($"{list[i].MethodName} 执行:100%");
}
else
{
var executeCount = list[i].Usage.Count(item => item);
Console.WriteLine($"{list[i].MethodName} 执行:{((double)executeCount / list[i].Usage.Length).ToString("P")}");
}
}
else
{
Console.WriteLine($"{list[i].MethodName} 未执行!");
}
}
}
```

## Description

CodeCov 将作为 Natasha 扩展库出现

使用方法:

```cs
List<(string MethodName, bool[] Usage)>? result = Assembly.GetCodecovCollection();
```

其中 result 将存放方法以及方法所执行行数的集合。
比如 A 类中有方法 Method , Method 方法体共 6 行代码逻辑,在执行过程中仅执行了前4行。
result 集合中将有:

```cs
"MyNamespace.A.Method":
[0] = true,
[1] = true,
[2] = true,
[3] = true,
[4] = false,
[5] = false,
```

<br/>
<br/>
Loading
Loading