From 03f632145873dce1b3ef987ec85e6038e6b3e22a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=9C=A8=E6=A3=AE=20=C2=B7=20=E4=BD=9C=E9=9C=96?= <16236903+NMSAzulX@users.noreply.github.com> Date: Wed, 20 Dec 2023 15:00:29 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- samples/ReferenceSample/Program.cs | 74 +++++++++++++++---- .../AssemblyCSharpBuilder.Ouput.Multi.cs | 2 +- .../CompileUnit/AssemblyCSharpBuilder.cs | 2 +- .../AssemblyCSharpBuilder.Compile.cs | 4 +- .../AssemblyCSharpBuilder.Ouput.cs | 10 +-- .../NatashaAssemblyBuilderExtension.cs | 9 +-- .../AssemblyCSharpBuilder.Ouput.Single.cs | 2 +- .../CompileUnit/AssemblyCSharpBuilder.cs | 2 +- 8 files changed, 73 insertions(+), 32 deletions(-) diff --git a/samples/ReferenceSample/Program.cs b/samples/ReferenceSample/Program.cs index 4f09603c..0d7719cd 100644 --- a/samples/ReferenceSample/Program.cs +++ b/samples/ReferenceSample/Program.cs @@ -17,10 +17,20 @@ namespace ReferenceSample internal class Program { - + public interface B { + string Name { get; set; } + } + public class C : B + { + string B.Name { get => throw new NotImplementedException(); set => throw new NotImplementedException(); } + } static void Main(string[] args) { - NatashaManagement.Preheating(false, false); + //var p = typeof(C).GetProperty("ReferenceSample.Program.B.Name", BindingFlags.NonPublic | BindingFlags.Instance); + //var p1 = typeof(C).GetProperties(BindingFlags.NonPublic | BindingFlags.Instance); + //Console.WriteLine(p.Name); + TestMini(); + //NatashaManagement.Preheating(false, false); //var domain = NatashaManagement.CreateRandomDomain(); //var asm = domain.LoadPluginUseDefaultDependency("I:\\OpenSource\\Natasha\\samples\\ReferenceSample\\bin\\Debug\\net8.0\\DynamicLibraryFolders\\Nc0e9a864079d427680ea239b5a9e525e\\a69937be3d244336a20c46843d51d19b.dll"); @@ -70,20 +80,49 @@ static void Main(string[] args) } + public static void TestMini1() + { + AssemblyCSharpBuilder builder = new(); + builder + .UseRandomDomain() + .WithDebugCompile(item => item.WriteToAssembly()) + .AddReferenceAndUsingCode(typeof(object).Assembly) + .AddReferenceAndUsingCode(typeof(Math).Assembly) + .AddReferenceAndUsingCode(typeof(MathF).Assembly) + .AddReferenceAndUsingCode(typeof(SuppressMessageAttribute)); + + builder.Add(@" +namespace MyNamespace{ + + public class A{ + + public static int N1 = 10; + public static float N2 = 1.2F; + public static double N3 = 3.44; + private static short N4 = 0; + + public static object Invoke(){ + int[] a = [1,2,3]; + return N1 + MathF.Log10((float)Math.Sqrt(MathF.Sqrt(N2) + Math.Tan(N3))); + } + } +} +"); + var method = builder + .GetAssembly() + .GetDelegateFromShortName>("A", "Invoke"); + var result = method(); + result = method(); + Console.WriteLine(result); + } public static void TestMini() { AssemblyCSharpBuilder builder = new(); builder.WithAnalysisAccessibility() .UseRandomDomain() - //.WithOutput() - .WithoutCombineReferences() - .WithoutSemanticCheck() - .WithDebugCompile(item=>item.WriteToAssembly()) - //.WithReleaseCompile(false) - //.OutputAsRefAssembly() - .WithoutPrivateMembers() - .WithCombineUsingCode(UsingLoadBehavior.WithCurrent) + .WithFileOutput() + .WithDebugCompile(item=>item.WriteToFile()) .AddReferenceAndUsingCode(typeof(HarmonyPatch)) .AddReferenceAndUsingCode(typeof(DebuggableAttribute)) .AddReferenceAndUsingCode(typeof(object).Assembly) @@ -105,6 +144,9 @@ public static object Invoke2(){ { return new T(); } + /// + /// 我的动态方法,返回科学计算结果。 + /// public static object Invoke(){ var type = typeof(HarmonyPatch); var type2 = typeof(HarmonyPrefix); @@ -134,7 +176,8 @@ namespace Microsoft.CodeAnalysis.Runtime public static class Instrumentation { public static bool[] CreatePayload(System.Guid mvid, int methodToken, int fileIndex, ref bool[] payload, int payloadLength) - { + { + Console.WriteLine(mvid.ToString()); if (payload == null) { payload = new bool[payloadLength]; @@ -157,7 +200,8 @@ public static void FlushPayload() } } }"); - DebugDirectoryBuilder debug = new DebugDirectoryBuilder(); + //a09e6bef-ff64-4b5f-8bb8-fc495ebb50ba + DebugDirectoryBuilder debug = new(); debug.AddReproducibleEntry(); debug.AddReproducibleEntry(); var asm = builder.GetAssembly(); @@ -165,9 +209,9 @@ public static void FlushPayload() var type1 = asm.GetType(""); var method = type.GetMethod("Invoke"); var result = method.Invoke(null, null); - var method2 = type.GetMethod("Invoke2"); - var result2 = method2.Invoke(Activator.CreateInstance(type), null); - + //var method2 = type.GetMethod("Invoke2"); + //var result2 = method2.Invoke(Activator.CreateInstance(type), null); + result = method.Invoke(null, null); Console.WriteLine(result); } diff --git a/src/Natasha.CSharp/Natasha.CSharp.Compiler/MultiDomain/CompileUnit/AssemblyCSharpBuilder.Ouput.Multi.cs b/src/Natasha.CSharp/Natasha.CSharp.Compiler/MultiDomain/CompileUnit/AssemblyCSharpBuilder.Ouput.Multi.cs index 2f17f4fb..25803c6f 100644 --- a/src/Natasha.CSharp/Natasha.CSharp.Compiler/MultiDomain/CompileUnit/AssemblyCSharpBuilder.Ouput.Multi.cs +++ b/src/Natasha.CSharp/Natasha.CSharp.Compiler/MultiDomain/CompileUnit/AssemblyCSharpBuilder.Ouput.Multi.cs @@ -31,7 +31,7 @@ public AssemblyCSharpBuilder WithFileOutput(string? folder = null) } DllFilePath = Path.Combine(OutputFolder, $"{AssemblyName}.dll"); PdbFilePath = Path.Combine(OutputFolder, $"{AssemblyName}.pdb"); - XmlFilePath = Path.Combine(OutputFolder, $"{AssemblyName}.xml"); + CommentFilePath = Path.Combine(OutputFolder, $"{AssemblyName}.xml"); return this; } } diff --git a/src/Natasha.CSharp/Natasha.CSharp.Compiler/MultiDomain/CompileUnit/AssemblyCSharpBuilder.cs b/src/Natasha.CSharp/Natasha.CSharp.Compiler/MultiDomain/CompileUnit/AssemblyCSharpBuilder.cs index 02732538..e96bfa69 100644 --- a/src/Natasha.CSharp/Natasha.CSharp.Compiler/MultiDomain/CompileUnit/AssemblyCSharpBuilder.cs +++ b/src/Natasha.CSharp/Natasha.CSharp.Compiler/MultiDomain/CompileUnit/AssemblyCSharpBuilder.cs @@ -25,7 +25,7 @@ public AssemblyCSharpBuilder(string assemblyName) SyntaxTrees = []; AssemblyName = assemblyName; DllFilePath = string.Empty; - XmlFilePath = string.Empty; + CommentFilePath = string.Empty; _semanticAnalysistor = [UsingAnalysistor._usingSemanticDelegate]; diff --git a/src/Natasha.CSharp/Natasha.CSharp.Compiler/Public/CompileUnit/AssemblyCSharpBuilder.Compile.cs b/src/Natasha.CSharp/Natasha.CSharp.Compiler/Public/CompileUnit/AssemblyCSharpBuilder.Compile.cs index c74da1b5..9dd92682 100644 --- a/src/Natasha.CSharp/Natasha.CSharp.Compiler/Public/CompileUnit/AssemblyCSharpBuilder.Compile.cs +++ b/src/Natasha.CSharp/Natasha.CSharp.Compiler/Public/CompileUnit/AssemblyCSharpBuilder.Compile.cs @@ -90,9 +90,9 @@ public Assembly GetAssembly() dllStream = new MemoryStream(); } - if (XmlFilePath != string.Empty) + if (CommentFilePath != string.Empty) { - xmlStream = File.Create(XmlFilePath); + xmlStream = File.Create(CommentFilePath); } var debugInfoFormat = _debugConfiguration._informationFormat; diff --git a/src/Natasha.CSharp/Natasha.CSharp.Compiler/Public/CompileUnit/AssemblyCSharpBuilder.Ouput.cs b/src/Natasha.CSharp/Natasha.CSharp.Compiler/Public/CompileUnit/AssemblyCSharpBuilder.Ouput.cs index bba5d41a..36eb805c 100644 --- a/src/Natasha.CSharp/Natasha.CSharp.Compiler/Public/CompileUnit/AssemblyCSharpBuilder.Ouput.cs +++ b/src/Natasha.CSharp/Natasha.CSharp.Compiler/Public/CompileUnit/AssemblyCSharpBuilder.Ouput.cs @@ -11,7 +11,7 @@ public sealed partial class AssemblyCSharpBuilder public string AssemblyName; public string DllFilePath; public string? PdbFilePath; - public string XmlFilePath; + public string CommentFilePath; public string OutputFolder; /// /// 默认的输出文件夹 @@ -39,15 +39,15 @@ public AssemblyCSharpBuilder SetAssemblyName(string asmName) /// /// /// - /// + /// /// - public AssemblyCSharpBuilder WithFileOutput(string dllFilePath, string? pdbFilePath = null, string? xmlFilePath = null) + public AssemblyCSharpBuilder WithFileOutput(string dllFilePath, string? pdbFilePath = null, string? commentFilePath = null) { DllFilePath = dllFilePath; PdbFilePath = pdbFilePath; - if (xmlFilePath != null) + if (commentFilePath != null) { - XmlFilePath = xmlFilePath; + CommentFilePath = commentFilePath; } return this; } diff --git a/src/Natasha.CSharp/Natasha.CSharp.Compiler/Public/Extension/NatashaAssemblyBuilderExtension.cs b/src/Natasha.CSharp/Natasha.CSharp.Compiler/Public/Extension/NatashaAssemblyBuilderExtension.cs index 40dc2c12..453d940b 100644 --- a/src/Natasha.CSharp/Natasha.CSharp.Compiler/Public/Extension/NatashaAssemblyBuilderExtension.cs +++ b/src/Natasha.CSharp/Natasha.CSharp.Compiler/Public/Extension/NatashaAssemblyBuilderExtension.cs @@ -1,7 +1,4 @@ -using Microsoft.CodeAnalysis; - - -public static class NatashaAssemblyBuilderExtension +public static class NatashaAssemblyBuilderExtension { public static AssemblyCSharpBuilder SetOutputFolder(this AssemblyCSharpBuilder builder, string folder) { @@ -18,9 +15,9 @@ public static AssemblyCSharpBuilder SetPdbFilePath(this AssemblyCSharpBuilder bu builder.PdbFilePath = pdbFilePath; return builder; } - public static AssemblyCSharpBuilder SetXmlFilePath(this AssemblyCSharpBuilder builder, string xmlFilePath) + public static AssemblyCSharpBuilder SetCommentFilePath(this AssemblyCSharpBuilder builder, string commentFilePath) { - builder.XmlFilePath = xmlFilePath; + builder.CommentFilePath = commentFilePath; return builder; } } diff --git a/src/Natasha.CSharp/Natasha.CSharp.Compiler/SingleDomain/CompileUnit/AssemblyCSharpBuilder.Ouput.Single.cs b/src/Natasha.CSharp/Natasha.CSharp.Compiler/SingleDomain/CompileUnit/AssemblyCSharpBuilder.Ouput.Single.cs index 6c4a764a..ff203caa 100644 --- a/src/Natasha.CSharp/Natasha.CSharp.Compiler/SingleDomain/CompileUnit/AssemblyCSharpBuilder.Ouput.Single.cs +++ b/src/Natasha.CSharp/Natasha.CSharp.Compiler/SingleDomain/CompileUnit/AssemblyCSharpBuilder.Ouput.Single.cs @@ -33,7 +33,7 @@ public AssemblyCSharpBuilder WithOutput(string? folder = null) } DllFilePath = Path.Combine(OutputFolder, $"{AssemblyName}.dll"); PdbFilePath = Path.Combine(OutputFolder, $"{AssemblyName}.pdb"); - XmlFilePath = Path.Combine(OutputFolder, $"{AssemblyName}.xml"); + CommentFilePath = Path.Combine(OutputFolder, $"{AssemblyName}.xml"); return this; } diff --git a/src/Natasha.CSharp/Natasha.CSharp.Compiler/SingleDomain/CompileUnit/AssemblyCSharpBuilder.cs b/src/Natasha.CSharp/Natasha.CSharp.Compiler/SingleDomain/CompileUnit/AssemblyCSharpBuilder.cs index 2a92fe07..ab9e71b5 100644 --- a/src/Natasha.CSharp/Natasha.CSharp.Compiler/SingleDomain/CompileUnit/AssemblyCSharpBuilder.cs +++ b/src/Natasha.CSharp/Natasha.CSharp.Compiler/SingleDomain/CompileUnit/AssemblyCSharpBuilder.cs @@ -26,7 +26,7 @@ public AssemblyCSharpBuilder(string assemblyName) SyntaxTrees = []; AssemblyName = assemblyName; DllFilePath = string.Empty; - XmlFilePath = string.Empty; + CommentFilePath = string.Empty; WithOutput(); if (HasInitialized) {