Skip to content

Commit

Permalink
Merge pull request #264 from dotnetcore/any
Browse files Browse the repository at this point in the history
修复其他工程引用以及UT并发问题
  • Loading branch information
NMSAzulX authored Jan 6, 2024
2 parents ca77236 + 07ac0a2 commit abe9f10
Show file tree
Hide file tree
Showing 17 changed files with 141 additions and 31 deletions.
4 changes: 2 additions & 2 deletions samples/HotReloadSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public static Assembly OldAssembly(NatashaLoadContext domain)
{
AssemblyCSharpBuilder builder = new AssemblyCSharpBuilder
{
Domain = domain
LoadContext = domain
};
builder.Add("public class A{ public int Code = 1; public void Show(){ Console.WriteLine(Code); } }", UsingLoadBehavior.WithDefault);
return builder.GetAssembly();
Expand All @@ -129,7 +129,7 @@ public static Assembly OldAssembly(NatashaLoadContext domain)
public static Assembly NewAssembly(NatashaLoadContext domain)
{
AssemblyCSharpBuilder builder = new AssemblyCSharpBuilder();
builder.Domain = domain;
builder.LoadContext = domain;
builder.Add("public class A{ public int Code = 2; public void Show(){ Console.WriteLine(Code); } }", UsingLoadBehavior.WithDefault);
return builder.GetAssembly();
}
Expand Down
35 changes: 25 additions & 10 deletions samples/ReferenceSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,32 @@ static void Main(string[] args)
//TestMini();
//var a = Math.Min(1, args.Length);
NatashaManagement.Preheating<NatashaDomainCreator>(true, true);

//Console.WriteLine("=============================");
//AssemblyCSharpBuilder builder = new();
//var asm = builder
// .UseRandomDomain()
// .ConfigCompilerOption(item=>item.AddSupperess("CS8019"))
// .WithCombineReferences(item => item.UseDefaultReferences())
// .WithCombineUsingCode(UsingLoadBehavior.WithAll)
// .Add("public class A{}",UsingLoadBehavior.WithAll)
// .GetAssembly();
//Console.WriteLine(asm.FullName);
//Console.ReadKey();
AssemblyCSharpBuilder builder = new();
var asm = builder
.UseRandomDomain()
.UseSmartMode()
//.WithoutCombineUsingCode()
.WithFileOutput()
.WithReleaseCompile()
.WithoutInjectToDomain()
.OutputAsRefAssembly()
.Add(@"
/// <summary>
/// 测试类
/// </summary>
public class A{
/// <summary>
/// 输出信息
/// </summary>
public void Show(){
Console.WriteLine(1);
}
}")
.GetAssembly();
Console.WriteLine(asm.FullName);
Console.ReadKey();
//NatashaInitializer.Preheating((asmName, name) => {
// if (name != null)
// {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public AssemblyCSharpBuilder(string assemblyName)
_loadContext = NatashaLoadContext.DefaultContext;
if (_loadContext == default!)
{
throw new NullReferenceException("LoadContext 为空!请检查是否调用 NatashaManagement.Preheating 或 NatashaManagement.RegistDomainCreator, 若调用,请检查 Builder 是否创建了域!");
throw new NullReferenceException("LoadContext 为空!请检查是否调用 NatashaManagement.Preheating<> 或 NatashaManagement.RegistDomainCreator!");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Reflection.PortableExecutable;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace Natasha.CSharp.Compiler.Component
{
Expand All @@ -18,11 +19,11 @@ public static class MetadataHelper
public static unsafe (AssemblyName asmName, MetadataReference metadata, HashSet<string> namespaces)? GetMetadataAndNamespaceFromMemory(Assembly assembly, Func<AssemblyName?, string?, bool>? filter = null)
{
var asmName = assembly.GetName();
if (NatashaLoadContext.Creator.TryGetRawMetadata(assembly ,out var blob, out var length))
if (NatashaLoadContext.Creator.TryGetRawMetadata(assembly, out var blob, out var length))
{
if (filter == null || !filter(asmName, asmName.Name))
{

return (asmName
, AssemblyMetadata.Create(ModuleMetadata.CreateFromMetadata((IntPtr)blob, length)).GetReference()
, GetNamespaceFromMemroy(assembly, filter)
Expand All @@ -44,7 +45,8 @@ public static unsafe (AssemblyName asmName, MetadataReference metadata)? GetMeta
}
return null;
}
public static HashSet<string> GetNamespaceFromMemroy(Assembly assembly, Func<AssemblyName?, string?, bool>? filter = null)

public static HashSet<string> GetNamespaceFromMemroyWithoutInternal(Assembly assembly, Func<AssemblyName?, string?, bool>? filter = null)
{
HashSet<string> tempSets = [];
try
Expand Down Expand Up @@ -130,6 +132,98 @@ public static HashSet<string> GetNamespaceFromMemroy(Assembly assembly, Func<Ass
{
#if DEBUG
Console.WriteLine(assembly.FullName + ex.Message);
#endif
}
return tempSets;
}
public static HashSet<string> GetNamespaceFromMemroy(Assembly assembly, Func<AssemblyName?, string?, bool>? filter = null)
{
if (assembly.FullName.StartsWith("System.Private.CoreLib"))
{
return GetNamespaceFromMemroyWithoutInternal(assembly, filter);
}
HashSet<string> tempSets = [];
try
{
var types = assembly.ExportedTypes;
if (types.Count() > 16)
{
var result = Parallel.ForEach(types, type =>
{

if (type.IsNested && !type.IsNestedPublic)
{
return;
}

var name = type.Namespace;

lock (tempSets)
{
if (tempSets.Contains(name))
{
return;
}
}

if (!string.IsNullOrEmpty(name)
&& name.IndexOf('<') == -1)
{

if (filter == null || !filter(null, name))
{
lock (tempSets)
{
tempSets.Add(name);
}
}
#if DEBUG
else
{
System.Diagnostics.Debug.WriteLine("[排除程序集]:" + name);
}
#endif
}
});
while (!result.IsCompleted)
{
Thread.Sleep(50);
}
}
else
{
foreach (var type in types)
{

if (type.IsNested && !type.IsNestedPublic)
{
continue;
}

var name = type.Namespace;
if (!string.IsNullOrEmpty(name)
&& !tempSets.Contains(name)
&& name.IndexOf('<') == -1)
{

if (filter == null || !filter(null, name))
{
tempSets.Add(name);
}
#if DEBUG
else
{
System.Diagnostics.Debug.WriteLine("[排除程序集]:" + name);
}
#endif
}
}
}
}
catch (System.Exception ex)
{
#if DEBUG
Console.WriteLine(assembly.FullName + ex.Message);
#endif
}
return tempSets;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ public static void Preheating(Func<AssemblyName?, string?, bool>? excludeReferen
stopwatch.RestartAndShowCategoreInfo("[ Metadata ]", "编译缓存初始化", 1);
#endif
AssemblyCSharpBuilder cSharpBuilder = new();
cSharpBuilder.ConfigCompilerOption(item => item.AddSupperess("CS8019"));
cSharpBuilder
.UseRandomDomain()
.UseSmartMode()
Expand Down
2 changes: 1 addition & 1 deletion test/benchmark/NatashaBenchmark/DynamicCallFieldTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class DynamicCallFieldTest

public DynamicCallFieldTest()
{
NatashaInitializer.Preheating();
NatashaManagement.Preheating();
Preheating();
Precache();
}
Expand Down
5 changes: 3 additions & 2 deletions test/benchmark/NatashaBenchmark/NatashaBenchmark.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\Natasha.CSharp\Core\Natasha.CSharp.Template\Natasha.CSharp.Template.csproj" />
</ItemGroup>
<ProjectReference Include="..\..\..\src\Natasha.CSharp\Component\Core\Natasha.CSharp.Template.Core\Natasha.CSharp.Template.Core.csproj" />
</ItemGroup>


</Project>
4 changes: 2 additions & 2 deletions test/monitor/ReferenceTest50/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ static void Main(string[] args)
//Microsoft.CodeAnalysis.Workspaces.ErrorLogger
Check();

NatashaInitializer.Preheating((asm,name)=>name.Contains("IO"));
NatashaManagement.Preheating((asm,name)=>name.Contains("IO"));
Check();
AssemblyCSharpBuilder builder = new();
builder.Domain = DomainManagement.Random();
builder.LoadContext = DomainManagement.Random();
builder.Add("public class Abved{ string Name; public int Age;}");
var asm = builder.GetAssembly();
Check();
Expand Down
3 changes: 2 additions & 1 deletion test/monitor/ReferenceTest50/ReferenceTest50.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\Natasha.CSharp\Core\Natasha.CSharp.Template\Natasha.CSharp.Template.csproj" />
<ProjectReference Include="..\..\..\src\Natasha.CSharp\Component\Core\Natasha.CSharp.Template.Core\Natasha.CSharp.Template.Core.csproj" />
</ItemGroup>


</Project>
2 changes: 1 addition & 1 deletion test/monitor/UnloadTest31/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static void Main(string[] args)
Console.WriteLine($"初始内存占用:{Process.GetCurrentProcess().PrivateMemorySize64 / 1024 / 1024}M");
Console.WriteLine("-----------------------------------------------------------------------------------------");

ShowTaskResoucesInfomation("Natasha预热", ()=> { NatashaInitializer.Preheating(); }, ConsoleColor.Magenta);
ShowTaskResoucesInfomation("Natasha预热", ()=> { NatashaManagement.Preheating(); }, ConsoleColor.Magenta);
Thread.Sleep(1000);
Console.WriteLine("-----------------------------------------------------------------------------------------");
Console.WriteLine($"预热后内存占用:{Process.GetCurrentProcess().PrivateMemorySize64 / 1024 / 1024}M");
Expand Down
2 changes: 1 addition & 1 deletion test/monitor/UnloadTest31/UnloadTest31.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\Natasha.CSharp\Core\Natasha.CSharp.Template\Natasha.CSharp.Template.csproj" />
<ProjectReference Include="..\..\..\src\Natasha.CSharp\Component\Core\Natasha.CSharp.Template.Core\Natasha.CSharp.Template.Core.csproj" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion test/monitor/UnloadTest50/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static void Test()
{
var ad = DomainManagement.Create("test" + i.ToString());
var builder = FastMethodOperator.DefaultDomain();
builder.AssemblyBuilder.Domain = ad;
builder.AssemblyBuilder.LoadContext = ad;
func[i] = builder.Body($@"
int[] a = new int[40960];
for(int i =0;i<40960;i++){{a[i]=i;}}
Expand Down
2 changes: 1 addition & 1 deletion test/monitor/UnloadTest50/UnloadTest50.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\Natasha.CSharp\Core\Natasha.CSharp.Template\Natasha.CSharp.Template.csproj" />
<ProjectReference Include="..\..\..\src\Natasha.CSharp\Component\Core\Natasha.CSharp.Template.Core\Natasha.CSharp.Template.Core.csproj" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion test/monitor/UnloadTest60/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static void Test()
{
var ad = DomainManagement.Create("test" + i.ToString());
var builder = FastMethodOperator.DefaultDomain();
builder.AssemblyBuilder.Domain = ad;
builder.AssemblyBuilder.LoadContext = ad;
func[i] = builder.Body($@"
int[] a = new int[40960];
for(int i =0;i<40960;i++){{a[i]=i;}}
Expand Down
2 changes: 1 addition & 1 deletion test/monitor/UnloadTest60/UnloadTest60.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\Natasha.CSharp\Core\Natasha.CSharp.Template\Natasha.CSharp.Template.csproj" />
<ProjectReference Include="..\..\..\src\Natasha.CSharp\Component\Core\Natasha.CSharp.Template.Core\Natasha.CSharp.Template.Core.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace NatashaFunctionUT.Template
{
[Trait("基础功能测试", "模板")]
public class OopTemplateTest
public class OopTemplateTest : DomainPrepareBase
{

[Fact(DisplayName = "构建类")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace NatashaFunctionUT.Template
{

[Trait("基础功能测试", "模板")]
public class PropertyTemplateTest
public class PropertyTemplateTest : DomainPrepareBase
{
[Fact(DisplayName = "静态属性1")]
public void Test1()
Expand Down

0 comments on commit abe9f10

Please sign in to comment.