Skip to content

Commit ea8c8e5

Browse files
committed
Merge branch 'any'
2 parents 2626013 + bc37479 commit ea8c8e5

File tree

5 files changed

+284
-81
lines changed

5 files changed

+284
-81
lines changed

samples/HotReloadSample/Program.cs

+83-62
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System.Reflection.PortableExecutable;
1616
using System.Runtime.Intrinsics;
1717
using Microsoft.CodeAnalysis.Operations;
18+
using Microsoft.Extensions.DependencyModel;
1819

1920
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
2021
namespace HotReloadSample
@@ -23,16 +24,41 @@ internal class Program
2324
{
2425
static unsafe void Main(string[] args)
2526
{
27+
NatashaManagement.RegistDomainCreator<NatashaDomainCreator>();
28+
29+
// AssemblyCSharpBuilder builder = new();
30+
// builder.UseRandomDomain();
31+
// builder.UseSimpleMode();
32+
// builder.WithoutCombineUsingCode();
33+
// builder.ConfigCompilerOption(opt => opt
34+
// .AddSupperess("CS8618")
35+
//// .WithDiagnosticLevel(ReportDiagnostic.Info)
36+
// );
37+
// builder.ConfigLoadContext(opt =>
38+
// opt
39+
// .AddReferenceAndUsingCode<object>()
40+
// .AddReferenceAndUsingCode<Task>()
41+
// );
42+
// builder.Add("using System.Threading.Tasks; public class A{ public string Name; public async Task Delay() {return;} }");
43+
// builder.SetLogEvent((log) =>
44+
// {
45+
// Console.WriteLine(log.ToString());
46+
// });
47+
// var asm = builder.GetAssembly();
48+
// Console.WriteLine(asm.FullName);
49+
50+
51+
2652
Console.WriteLine(MetadataUpdater.IsSupported);
53+
Console.WriteLine(Debugger.IsAttached);
2754
//Console.WriteLine(Vector128<short>.Count);
2855
//var asm = typeof(A).Assembly;
2956
//Stopwatch stopwatch = Stopwatch.StartNew();
3057
//HashSet<string> names = new HashSet<string>();
3158
//HashSet<string> names1 = new HashSet<string>();
3259
//foreach (var item in asm.ExportedTypes)
3360
//{
34-
// if (item.Namespace!=null)
35-
// {
61+
// if (item.Namespace!=null) // {
3662
// names.Add(item.Namespace);
3763
// }
3864
//}
@@ -61,77 +87,73 @@ static unsafe void Main(string[] args)
6187
//Test(typeof(A).Assembly);
6288
//HotReloadService.UpdateApplicationEvent += HotReloadService_UpdateApplicationEvent;
6389
//NatashaManagement.Preheating(null,false,true);
64-
//var domain = NatashaManagement.CreateRandomDomain();
65-
////var asm1 = OldAssembly(domain);
66-
////Show(asm1);
67-
//var asm2 = NewAssembly(domain);
68-
//Show(asm2);
69-
//Update(typeof(A).Assembly, asm2);
70-
//Show(typeof(A).Assembly);
71-
////MetadataUpdater.ApplyUpdate();
72-
Console.ReadKey();
73-
74-
}
75-
76-
public unsafe static void Test(Assembly assembly)
77-
{
78-
if (assembly.TryGetRawMetadata(out var blob, out var length))
90+
var domain = NatashaManagement.CreateRandomDomain();
91+
var asm1 = OldAssembly(domain);
92+
var typeA = asm1.GetTypeFromShortName("A");
93+
var methodInfo = typeA.GetMethod("Show");
94+
var obj = Activator.CreateInstance(typeA);
95+
methodInfo.Invoke(obj, null);
96+
Console.WriteLine();
97+
Assembly asm2 = asm1;
98+
try
7999
{
80-
//HXD04
81-
//var metadataReference = AssemblyMetadata.Create(ModuleMetadata.CreateFromMetadata((IntPtr)blob, length)).GetReference();
82-
var metaReader = new MetadataReader(blob, length);
83-
foreach (var item in metaReader.MethodDefinitions)
84-
{
85-
var methodDefine = metaReader.GetMethodDefinition(item);
86-
var methodName = metaReader.GetString(methodDefine.Name);
87-
if (methodName == "Show2")
88-
{
89-
Console.WriteLine(metaReader.GetString(metaReader.GetTypeDefinition(methodDefine.GetDeclaringType()).Name));
90-
Console.WriteLine(Encoding.UTF8.GetString(metaReader.GetBlobBytes(methodDefine.Signature)));
91-
foreach (var parameter in methodDefine.GetParameters())
92-
{
93-
var para = metaReader.GetParameter(parameter);
94-
foreach (var paraAttr in para.GetCustomAttributes())
95-
{
96-
Console.WriteLine(Encoding.UTF8.GetString(metaReader.GetBlobBytes(metaReader.GetCustomAttribute(paraAttr).Value)));
97-
}
98-
Console.WriteLine(metaReader.GetString(para.Name));
99-
}
100-
Console.WriteLine();
101-
}
102-
103-
}
104-
105-
106-
var asmDefinition = metaReader.GetNamespaceDefinitionRoot();
107-
foreach (var handle in asmDefinition.NamespaceDefinitions)
108-
{
109-
var nameDefinition = metaReader.GetNamespaceDefinition(handle);
110-
Console.WriteLine($"{nameDefinition.Name.IsNil} : {metaReader.GetString(handle)} : {nameDefinition.TypeDefinitions.Length}");
111-
}
100+
asm2 = NewAssembly(domain, asm1);
112101
}
113-
}
114-
private static void HotReloadService_UpdateApplicationEvent(Type[]? obj)
115-
{
116-
Console.WriteLine(obj==null);
102+
catch (Exception ex)
103+
{
104+
var ex1 = ex as InvalidOperationException;
105+
Console.WriteLine(ex1.Message);
106+
Console.WriteLine(ex.Message);
107+
Console.WriteLine(ex.StackTrace);
108+
return;
109+
}
110+
111+
var members = asm2.GetTypeFromShortName("A").GetMembers();
112+
var typeA1 = asm2.GetTypeFromShortName("A");
113+
methodInfo = typeA1.GetMethod("Show");
114+
var obj1 = Activator.CreateInstance(typeA1);
115+
methodInfo.Invoke(obj1, null);
116+
Console.ReadKey();
117117
}
118118

119+
//private static void HotReloadService_UpdateApplicationEvent(Type[]? obj)
120+
//{
121+
// Console.WriteLine(obj==null);
122+
//}
123+
119124
public static Assembly OldAssembly(NatashaLoadContext domain)
120125
{
121-
AssemblyCSharpBuilder builder = new AssemblyCSharpBuilder
126+
AssemblyCSharpBuilder builder = new()
122127
{
123128
LoadContext = domain
124129
};
125-
builder.Add("public class A{ public int Code = 1; public void Show(){ Console.WriteLine(Code); } }", UsingLoadBehavior.WithDefault);
130+
builder.UseSimpleMode();
131+
builder.ConfigLoadContext(opt => opt
132+
.AddReferenceAndUsingCode(typeof(object))
133+
.AddReferenceAndUsingCode(typeof(Console))
134+
);
135+
builder.WithDebugCompile(opt => opt.WriteToAssembly());
136+
builder.Add("public class A{ public int Code = 1; public void Show(){ Console.WriteLine(Code); } }");
126137
return builder.GetAssembly();
127138
}
128-
129-
public static Assembly NewAssembly(NatashaLoadContext domain)
139+
//CTRL+F5
140+
public static Assembly NewAssembly(NatashaLoadContext domain, Assembly oldAssembly)
130141
{
131-
AssemblyCSharpBuilder builder = new AssemblyCSharpBuilder();
142+
AssemblyCSharpBuilder builder = new AssemblyCSharpBuilder(oldAssembly.GetName().Name);
132143
builder.LoadContext = domain;
133-
builder.Add("public class A{ public int Code = 2; public void Show(){ Console.WriteLine(Code); } }", UsingLoadBehavior.WithDefault);
134-
return builder.GetAssembly();
144+
builder.WithFileOutput();
145+
builder.UseSimpleMode();
146+
builder.ConfigLoadContext(opt => opt
147+
.AddReferenceAndUsingCode(typeof(object))
148+
.AddReferenceAndUsingCode(typeof(Console))
149+
);
150+
//builder.WithDebugCompile(opt => opt.WriteToAssembly());
151+
builder.Add("public class A{ public int Code = 2; public int Code1 = 0; public void Show(){ Console.WriteLine(Code); } }");
152+
var result = builder.UpdateAssembly(oldAssembly);
153+
//Console.WriteLine(1);
154+
//MetadataUpdater.ApplyUpdate(result.Item1, result.Item2, result.Item3, result.Item4);
155+
//return result.Item1;
156+
return result.Item1;
135157
}
136158

137159
public static void Show(Assembly assembly)
@@ -140,14 +162,13 @@ public static void Show(Assembly assembly)
140162
var method = type!.GetMethod("Show");
141163
method!.Invoke(Activator.CreateInstance(type), null);
142164
}
143-
144165
public unsafe static void Update(Assembly oldAssembly,Assembly newAssembly)
145166
{
146167
Console.WriteLine(RuntimeFeature.IsDynamicCodeCompiled);
147168
Console.WriteLine(MetadataUpdater.IsSupported);
148169
if (newAssembly.TryGetRawMetadata(out var newBlob, out var newLength))
149170
{
150-
ReadOnlySpan<byte> newMetadataSpan = new ReadOnlySpan<byte>(newBlob, newLength);
171+
ReadOnlySpan<byte> newMetadataSpan = new(newBlob, newLength);
151172
MetadataUpdater.ApplyUpdate(oldAssembly, newMetadataSpan, Encoding.UTF8.GetBytes(File.ReadAllText("1.txt")), ReadOnlySpan<byte>.Empty);
152173
}
153174

samples/ReferenceSample/Program.cs

+12-19
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ static void Main(string[] args)
3434
//Console.WriteLine(typeof(Math).Assembly.FullName);
3535
NatashaManagement.RegistDomainCreator<NatashaDomainCreator>();
3636
NatashaManagement.Preheating(true, true);
37-
TestMini();
37+
TestMini1();
3838
//TestMini();
3939
//var a = Math.Min(1, args.Length);
4040
// NatashaManagement.Preheating<NatashaDomainCreator>(true, true);
@@ -103,8 +103,8 @@ public static void TestMini1()
103103
.UseNewDomain("adasd")
104104
.ConfigCompilerOption(item => item.WithLowerVersionsAssembly())
105105
//.WithFileOutput()
106-
.WithDebugCompile(item => item.WriteToAssembly())
107106
.UseSimpleMode()
107+
.WithDebugCompile(item => item.WriteToAssembly())
108108
.ConfigLoadContext(ldc=> ldc
109109
.AddReferenceAndUsingCode(typeof(Math).Assembly)
110110
.AddReferenceAndUsingCode(typeof(MathF).Assembly)
@@ -148,15 +148,15 @@ public static void TestMini()
148148
.ConfigCompilerOption(item => item.WithLowerVersionsAssembly())
149149
.WithDebugCompile(item => item.WriteToAssembly())
150150
.UseSimpleMode()
151-
.UseSmartMode()
152-
//.ConfigLoadContext(ldc => ldc
153-
// .AddReferenceAndUsingCode(typeof(Math).Assembly)
154-
// .AddReferenceAndUsingCode(typeof(MathF).Assembly)
155-
// .AddReferenceAndUsingCode(typeof(object))
156-
// .AddReferenceAndUsingCode(typeof(Console))
157-
// .AddReferenceAndUsingCode(typeof(Attribute))
158-
// .AddReferenceAndUsingCode(typeof(CodecovMonitor))
159-
// .AddReferenceAndUsingCode(typeof(SuppressMessageAttribute)))
151+
//.UseSmartMode()
152+
.ConfigLoadContext(ldc => ldc
153+
.AddReferenceAndUsingCode(typeof(Math).Assembly)
154+
.AddReferenceAndUsingCode(typeof(MathF).Assembly)
155+
.AddReferenceAndUsingCode(typeof(object))
156+
.AddReferenceAndUsingCode(typeof(Console))
157+
.AddReferenceAndUsingCode(typeof(Attribute))
158+
.AddReferenceAndUsingCode(typeof(CodecovMonitor))
159+
.AddReferenceAndUsingCode(typeof(SuppressMessageAttribute)))
160160
;
161161

162162

@@ -205,14 +205,7 @@ public object Invoke2(){
205205
}
206206
}");
207207

208-
//a09e6bef-ff64-4b5f-8bb8-fc495ebb50ba
209-
DebugDirectoryBuilder debug = new();
210-
debug.AddReproducibleEntry();
211-
debug.AddReproducibleEntry();
212-
213-
214-
215-
builder.WithCodecov();
208+
//builder.WithCodecov();
216209
var asm = builder.GetAssembly();
217210

218211
//执行A.Invoke

0 commit comments

Comments
 (0)