Skip to content

Commit

Permalink
重命名 API
Browse files Browse the repository at this point in the history
  • Loading branch information
NMSAzulX committed Dec 20, 2023
1 parent 4361c2c commit 03f6321
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 32 deletions.
74 changes: 59 additions & 15 deletions samples/ReferenceSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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<Func<object>>("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)
Expand All @@ -105,6 +144,9 @@ public static object Invoke2(){
{
return new T();
}
/// <summary>
/// 我的动态方法,返回科学计算结果。
/// </summary>
public static object Invoke(){
var type = typeof(HarmonyPatch);
var type2 = typeof(HarmonyPrefix);
Expand Down Expand Up @@ -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];
Expand All @@ -157,17 +200,18 @@ public static void FlushPayload()
}
}
}");
DebugDirectoryBuilder debug = new DebugDirectoryBuilder();
//a09e6bef-ff64-4b5f-8bb8-fc495ebb50ba
DebugDirectoryBuilder debug = new();
debug.AddReproducibleEntry();
debug.AddReproducibleEntry();
var asm = builder.GetAssembly();
var type = asm.GetType("A");
var type1 = asm.GetType("<PrivateImplementationDetails>");
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public AssemblyCSharpBuilder(string assemblyName)
SyntaxTrees = [];
AssemblyName = assemblyName;
DllFilePath = string.Empty;
XmlFilePath = string.Empty;
CommentFilePath = string.Empty;

_semanticAnalysistor = [UsingAnalysistor._usingSemanticDelegate];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
/// <summary>
/// 默认的输出文件夹
Expand Down Expand Up @@ -39,15 +39,15 @@ public AssemblyCSharpBuilder SetAssemblyName(string asmName)
/// </summary>
/// <param name="dllFilePath"></param>
/// <param name="pdbFilePath"></param>
/// <param name="xmlFilePath"></param>
/// <param name="commentFilePath"></param>
/// <returns></returns>
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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using Microsoft.CodeAnalysis;


public static class NatashaAssemblyBuilderExtension
public static class NatashaAssemblyBuilderExtension
{
public static AssemblyCSharpBuilder SetOutputFolder(this AssemblyCSharpBuilder builder, string folder)
{
Expand All @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public AssemblyCSharpBuilder(string assemblyName)
SyntaxTrees = [];
AssemblyName = assemblyName;
DllFilePath = string.Empty;
XmlFilePath = string.Empty;
CommentFilePath = string.Empty;
WithOutput();
if (HasInitialized)
{
Expand Down

0 comments on commit 03f6321

Please sign in to comment.