-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. 编译单元增加 UseRandomDomain 、UseNewDomain 、 UseDefaultDomain 方法. 2. 编译单元增加 GetSingleReference 方法支持查询引用. 3. 编译单元增加 AddReference 以支持根据程序集添加引用和 Using. 4. 编译选项增加 AppendCompilerFlag 绑定编译标识.
- Loading branch information
Showing
11 changed files
with
190 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...arp/Natasha.CSharp.Compiler/MultiDomain/Extension/NatashaAssemblyBuilderMultiExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#if NETCOREAPP3_0_OR_GREATER | ||
using System.Reflection; | ||
using System.Xml.Linq; | ||
|
||
|
||
public static class NatashaAssemblyBuilderMultiExtension | ||
{ | ||
/// <summary> | ||
/// 编译单元使用随机域 | ||
/// </summary> | ||
/// <param name="builder">编译单元</param> | ||
/// <returns></returns> | ||
public static AssemblyCSharpBuilder UseRandomDomain(this AssemblyCSharpBuilder builder) | ||
{ | ||
builder.Domain = DomainManagement.Random(); | ||
return builder; | ||
} | ||
|
||
/// <summary> | ||
/// 编译单元使用新的随机域 | ||
/// </summary> | ||
/// <param name="builder">编译单元</param> | ||
/// <param name="domainName">域名字</param> | ||
/// <returns></returns> | ||
public static AssemblyCSharpBuilder UseNewDomain(this AssemblyCSharpBuilder builder,string domainName) | ||
{ | ||
builder.Domain = DomainManagement.Create(domainName); | ||
return builder; | ||
} | ||
|
||
/// <summary> | ||
/// 编译单元使用新的随机域 | ||
/// </summary> | ||
/// <param name="builder">编译单元</param> | ||
/// <returns></returns> | ||
public static AssemblyCSharpBuilder UseDefaultDomain(this AssemblyCSharpBuilder builder) | ||
{ | ||
builder.Domain = NatashaReferenceDomain.DefaultDomain; | ||
return builder; | ||
} | ||
|
||
/// <summary> | ||
/// 增加引用 和 using | ||
/// </summary> | ||
/// <param name="builder">natasha 编译单元</param> | ||
/// <param name="assembly">程序集</param> | ||
/// <param name="loadReferenceBehavior">加载行为</param> | ||
public static AssemblyCSharpBuilder AddReference(this AssemblyCSharpBuilder builder, Assembly assembly, PluginLoadBehavior loadReferenceBehavior = PluginLoadBehavior.None) | ||
{ | ||
var name = assembly.GetName(); | ||
var domain = builder.Domain; | ||
var reference = domain.References.GetSingleReference(name); | ||
if (reference == null) | ||
{ | ||
domain.References.AddReference(assembly); | ||
domain.UsingRecorder.Using(assembly); | ||
} | ||
return builder; | ||
} | ||
/// <summary> | ||
/// 增加引用 和 using | ||
/// </summary> | ||
/// <param name="builder">natasha 编译单元</param> | ||
/// <param name="path">文件路径</param> | ||
/// <param name="loadReferenceBehavior">加载行为</param> | ||
public static AssemblyCSharpBuilder AddReference(this AssemblyCSharpBuilder builder, string path, PluginLoadBehavior loadReferenceBehavior = PluginLoadBehavior.None) | ||
{ | ||
var resolver = new PathAssemblyResolver([path]); | ||
using var mlc = new MetadataLoadContext(resolver); | ||
Assembly assembly = mlc.LoadFromAssemblyPath(path); | ||
var name = assembly.GetName(); | ||
var domain = builder.Domain; | ||
var reference = domain.References.GetSingleReference(name); | ||
if (reference == null) | ||
{ | ||
domain.References.AddReference(assembly.GetName(), path); | ||
domain.UsingRecorder.Using(assembly); | ||
} | ||
return builder; | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters