Skip to content

Commit

Permalink
Merge pull request #284 from dotnetcore/any
Browse files Browse the repository at this point in the history
增加 动态方法模板 扩展
  • Loading branch information
NMSAzulX authored Apr 30, 2024
2 parents a051dbb + be47e1b commit 9373db1
Show file tree
Hide file tree
Showing 12 changed files with 235 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .github/project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ samples:
- samples/ReferenceSample/ReferenceSample.csproj
- samples/HotReloadSample/HotReloadSample.csproj
- samples/HotReloadPlugin/HotReloadPlugin.csproj
- samples/ExtensionSample/ExtensionSample.csproj
global_labels:
- name: pr_samples
description: 此次提交包含 [案例工程] 相关的改动.
Expand All @@ -97,6 +98,7 @@ src:
folded_projects:
- src/Natasha.CSharp/Extension/Natasha.CSharp.Extension.Ambiguity/Natasha.CSharp.Extension.Ambiguity.csproj
- src/Natasha.CSharp/Extension/Natasha.CSharp.Extension.Codecov/Natasha.CSharp.Extension.Codecov.csproj
- src/Natasha.CSharp/Extension/Natasha.CSharp.Extension.MethodCreator/Natasha.CSharp.Extension.MethodCreator.csproj
global_labels:
- name: pr_src
description: 此次提交包含 [源码工程] 相关的改动.
Expand Down
9 changes: 8 additions & 1 deletion Natasha.sln
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Natasha.CSharp.Extension.Co
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Natasha.CSharp.Extension.Ambiguity", "src\Natasha.CSharp\Extension\Natasha.CSharp.Extension.Ambiguity\Natasha.CSharp.Extension.Ambiguity.csproj", "{0D60DC94-C8A4-4CE7-9B3D-EB39FDA4EB48}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Natasha.CSharp.Extension.MethodCreator", "src\Natasha.CSharp\Extension\Natasha.CSharp.Extension.MethodCreator\Natasha.CSharp.Extension.MethodCreator.csproj", "{5A9A10B4-5790-4607-850B-A5A6C6C68737}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Natasha.CSharp.Extension.MethodCreator", "src\Natasha.CSharp\Extension\Natasha.CSharp.Extension.MethodCreator\Natasha.CSharp.Extension.MethodCreator.csproj", "{5A9A10B4-5790-4607-850B-A5A6C6C68737}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExtensionSample", "samples\ExtensionSample\ExtensionSample.csproj", "{0C73673D-8514-4058-8DB0-9F03FDE9D9B6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -386,6 +388,10 @@ Global
{5A9A10B4-5790-4607-850B-A5A6C6C68737}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5A9A10B4-5790-4607-850B-A5A6C6C68737}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5A9A10B4-5790-4607-850B-A5A6C6C68737}.Release|Any CPU.Build.0 = Release|Any CPU
{0C73673D-8514-4058-8DB0-9F03FDE9D9B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0C73673D-8514-4058-8DB0-9F03FDE9D9B6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0C73673D-8514-4058-8DB0-9F03FDE9D9B6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0C73673D-8514-4058-8DB0-9F03FDE9D9B6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -460,6 +466,7 @@ Global
{4C7306AB-D54A-44C9-B653-C1E96B4BC419} = {0CB3AF52-50F3-4728-BAC6-389F2E995111}
{0D60DC94-C8A4-4CE7-9B3D-EB39FDA4EB48} = {0CB3AF52-50F3-4728-BAC6-389F2E995111}
{5A9A10B4-5790-4607-850B-A5A6C6C68737} = {0CB3AF52-50F3-4728-BAC6-389F2E995111}
{0C73673D-8514-4058-8DB0-9F03FDE9D9B6} = {B4487ABB-27A2-4353-AB16-1A94B31E0210}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3004E730-B231-40FA-B75C-58D7DDE17679}
Expand Down
15 changes: 15 additions & 0 deletions samples/ExtensionSample/ExtensionSample.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Natasha.CSharp\Component\Core\Natasha.CSharp.Compiler.Domain\Natasha.CSharp.Compiler.Domain.csproj" />
<ProjectReference Include="..\..\src\Natasha.CSharp\Extension\Natasha.CSharp.Extension.MethodCreator\Natasha.CSharp.Extension.MethodCreator.csproj" />
</ItemGroup>

</Project>
17 changes: 17 additions & 0 deletions samples/ExtensionSample/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace ExtensionSample
{
internal class Program
{
static void Main(string[] args)
{
NatashaManagement.RegistDomainCreator<NatashaDomainCreator>();

var func = "return arg1+arg2;"
.WithAssemblyBuilder(opt=>opt.AddReferenceAndUsingCode<object>())
.ToFunc<int, int, int>();

Console.WriteLine(func(1,2));
Console.ReadKey();
}
}
}
10 changes: 7 additions & 3 deletions samples/HotReloadSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ internal class Program
{
static unsafe void Main(string[] args)
{
//ArgumentNullException.ThrowIfNull(args);

NatashaManagement.RegistDomainCreator<NatashaDomainCreator>();

// AssemblyCSharpBuilder builder = new();
Expand Down Expand Up @@ -139,8 +141,10 @@ public static Assembly OldAssembly(NatashaLoadContext domain)
//CTRL+F5
public static Assembly NewAssembly(NatashaLoadContext domain, Assembly oldAssembly)
{
AssemblyCSharpBuilder builder = new AssemblyCSharpBuilder(oldAssembly.GetName().Name);
builder.LoadContext = domain;
AssemblyCSharpBuilder builder = new(oldAssembly.GetName().Name)
{
LoadContext = domain
};
builder.WithFileOutput();
builder.UseSimpleMode();
builder.ConfigLoadContext(opt => opt
Expand Down Expand Up @@ -169,7 +173,7 @@ public unsafe static void Update(Assembly oldAssembly,Assembly newAssembly)
if (newAssembly.TryGetRawMetadata(out var newBlob, out var newLength))
{
ReadOnlySpan<byte> newMetadataSpan = new(newBlob, newLength);
MetadataUpdater.ApplyUpdate(oldAssembly, newMetadataSpan, Encoding.UTF8.GetBytes(File.ReadAllText("1.txt")), ReadOnlySpan<byte>.Empty);
MetadataUpdater.ApplyUpdate(oldAssembly, newMetadataSpan, Encoding.UTF8.GetBytes(File.ReadAllText("1.txt")), []);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Description>Natasha 的快捷方法构建扩展.</Description>
<PackageId>DotNetCore.Natasha.CSharp.Extension.MethodCreator</PackageId>
<PackageReleaseNotes>升级到最新版.</PackageReleaseNotes>
<PackageTags>Natasha;Compiler;Extension;MethodCreator;</PackageTags>
<Version>8.0.0.1</Version>
<FileVersion>8.0.0.1</FileVersion>
<AssemblyVersion>8.0.0.1</AssemblyVersion>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Natasha.CSharp.Compiler\Natasha.CSharp.Compiler.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,58 @@
public static class StringExtension
{

public static (string script, AssemblyCSharpBuilder builder) WithAssemblyBuilder(this string script, Func<AssemblyCSharpBuilder, AssemblyCSharpBuilder> config)
{
AssemblyCSharpBuilder builder = new();
config?.Invoke(builder);
return (script, builder);
}
public static (string script, AssemblyCSharpBuilder builder) WithAssemblyBuilder(this string script, Func<NatashaLoadContext, NatashaLoadContext> config)
{
AssemblyCSharpBuilder builder = new();
builder.ConfigLoadContext(config);
return (script, builder);
}

public static T? CreateMethod<T>(in this (string script, AssemblyCSharpBuilder builder) buildInfo, string? modifier = null) where T: Delegate
{

var className = $"N{Guid.NewGuid():N}";
var methodInfo = typeof(T).GetMethod("Invoke")!;

var returnTypeScript = methodInfo.ReturnType.GetDevelopName();
var parameterScript = new StringBuilder();

var methodParameters = methodInfo.GetParameters();
for (int i = 0; i < methodParameters.Length; i+=1)
{
parameterScript.Append($"{methodParameters[i].ParameterType.GetDevelopName()} arg{i+1},");
}
if (parameterScript.Length > 0)
{
parameterScript.Length -= 1;
}

buildInfo.builder.Add($"public static class {className} {{ public static {(modifier ?? string.Empty)} {returnTypeScript} Invoke({parameterScript}){{ {buildInfo.script} }} }}");
var asm = buildInfo.builder.GetAssembly();
var type = asm.GetType(className);
if (type != null)
{
return (T)Delegate.CreateDelegate(typeof(T), type.GetMethod("Invoke")!);
}
return null;
}
public static T? CreateAsyncMethod<T>(in this (string script, AssemblyCSharpBuilder builder) buildInfo) where T : Delegate
{
return CreateMethod<T>(buildInfo, "async");
}
public static T? CreateUnsafeMethod<T>(in this (string script, AssemblyCSharpBuilder builder) buildInfo) where T : Delegate
{
return CreateMethod<T>(buildInfo, "unsafe");
}
public static T? CreateUnsafeAsyncMethod<T>(in this (string script, AssemblyCSharpBuilder builder) buildInfo) where T : Delegate
{
return CreateMethod<T>(buildInfo, "unsafe async");
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@

namespace System
{
public static class SystemDelegateExtension
{
#region Action Delegate
public static Action<T1>? ToAction<T1>(in this (string script, AssemblyCSharpBuilder builder) buildInfo)
{
return buildInfo.CreateMethod<Action<T1>>();
}
public static Action<T1, T2>? ToAction<T1, T2>(in this (string script, AssemblyCSharpBuilder builder) buildInfo)
{
return buildInfo.CreateMethod<Action<T1, T2>>();
}
public static Action<T1, T2, T3>? ToAction<T1, T2, T3>(in this (string script, AssemblyCSharpBuilder builder) buildInfo)
{
return buildInfo.CreateMethod<Action<T1, T2, T3>>();
}
public static Action<T1, T2, T3, T4>? ToAction<T1, T2, T3, T4>(in this (string script, AssemblyCSharpBuilder builder) buildInfo)
{
return buildInfo.CreateMethod<Action<T1, T2, T3, T4>>();
}
public static Action<T1, T2, T3, T4, T5>? ToAction<T1, T2, T3, T4, T5>(in this (string script, AssemblyCSharpBuilder builder) buildInfo)
{
return buildInfo.CreateMethod<Action<T1, T2, T3, T4, T5>>();
}
public static Action<T1, T2, T3, T4, T5, T6>? ToAction<T1, T2, T3, T4, T5, T6>(in this (string script, AssemblyCSharpBuilder builder) buildInfo)
{
return buildInfo.CreateMethod<Action<T1, T2, T3, T4, T5, T6>>();
}
public static Action<T1, T2, T3, T4, T5, T6, T7>? ToAction<T1, T2, T3, T4, T5, T6, T7>(in this (string script, AssemblyCSharpBuilder builder) buildInfo)
{
return buildInfo.CreateMethod<Action<T1, T2, T3, T4, T5, T6, T7>>();
}
public static Action<T1, T2, T3, T4, T5, T6, T7, T8>? ToAction<T1, T2, T3, T4, T5, T6, T7, T8>(in this (string script, AssemblyCSharpBuilder builder) buildInfo)
{
return buildInfo.CreateMethod<Action<T1, T2, T3, T4, T5, T6, T7, T8>>();
}
public static Action<T1, T2, T3, T4, T5, T6, T7, T8, T9>? ToAction<T1, T2, T3, T4, T5, T6, T7, T8, T9>(in this (string script, AssemblyCSharpBuilder builder) buildInfo)
{
return buildInfo.CreateMethod<Action<T1, T2, T3, T4, T5, T6, T7, T8, T9>>();
}
public static Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>? ToAction<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(in this (string script, AssemblyCSharpBuilder builder) buildInfo)
{
return buildInfo.CreateMethod<Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>>();
}
public static Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>? ToAction<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>(in this (string script, AssemblyCSharpBuilder builder) buildInfo)
{
return buildInfo.CreateMethod<Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>>();
}
public static Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>? ToAction<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>(in this (string script, AssemblyCSharpBuilder builder) buildInfo)
{
return buildInfo.CreateMethod<Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>>();
}
#endregion


#region Func Delegate
public static Func<T1>? ToFunc<T1>(in this (string script, AssemblyCSharpBuilder builder) buildInfo)
{
return buildInfo.CreateMethod<Func<T1>>();
}
public static Func<T1, T2>? ToFunc<T1, T2>(in this (string script, AssemblyCSharpBuilder builder) buildInfo)
{
return buildInfo.CreateMethod<Func<T1, T2>>();
}
public static Func<T1, T2, T3>? ToFunc<T1, T2, T3>(in this (string script, AssemblyCSharpBuilder builder) buildInfo)
{
return buildInfo.CreateMethod<Func<T1, T2, T3>>();
}
public static Func<T1, T2, T3, T4>? ToFunc<T1, T2, T3, T4>(in this (string script, AssemblyCSharpBuilder builder) buildInfo)
{
return buildInfo.CreateMethod<Func<T1, T2, T3, T4>>();
}
public static Func<T1, T2, T3, T4, T5>? ToFunc<T1, T2, T3, T4, T5>(in this (string script, AssemblyCSharpBuilder builder) buildInfo)
{
return buildInfo.CreateMethod<Func<T1, T2, T3, T4, T5>>();
}
public static Func<T1, T2, T3, T4, T5, T6>? ToFunc<T1, T2, T3, T4, T5, T6>(in this (string script, AssemblyCSharpBuilder builder) buildInfo)
{
return buildInfo.CreateMethod<Func<T1, T2, T3, T4, T5, T6>>();
}
public static Func<T1, T2, T3, T4, T5, T6, T7>? ToFunc<T1, T2, T3, T4, T5, T6, T7>(in this (string script, AssemblyCSharpBuilder builder) buildInfo)
{
return buildInfo.CreateMethod<Func<T1, T2, T3, T4, T5, T6, T7>>();
}
public static Func<T1, T2, T3, T4, T5, T6, T7, T8>? ToFunc<T1, T2, T3, T4, T5, T6, T7, T8>(in this (string script, AssemblyCSharpBuilder builder) buildInfo)
{
return buildInfo.CreateMethod<Func<T1, T2, T3, T4, T5, T6, T7, T8>>();
}
public static Func<T1, T2, T3, T4, T5, T6, T7, T8, T9>? ToFunc<T1, T2, T3, T4, T5, T6, T7, T8, T9>(in this (string script, AssemblyCSharpBuilder builder) buildInfo)
{
return buildInfo.CreateMethod<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9>>();
}
public static Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>? ToFunc<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(in this (string script, AssemblyCSharpBuilder builder) buildInfo)
{
return buildInfo.CreateMethod<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>>();
}
public static Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>? ToFunc<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>(in this (string script, AssemblyCSharpBuilder builder) buildInfo)
{
return buildInfo.CreateMethod<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>>();
}
public static Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>? ToFunc<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>(in this (string script, AssemblyCSharpBuilder builder) buildInfo)
{
return buildInfo.CreateMethod<Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>>();
}
#endregion

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<PackageReleaseNotes>升级到最新版.</PackageReleaseNotes>
<PackageTags>Roslyn;IL;Script;Dynamic;Natasha;NMS;Compiler;</PackageTags>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Version>8.0.0.0</Version>
<FileVersion>8.0.0.0</FileVersion>
<AssemblyVersion>8.0.0.0</AssemblyVersion>
<Version>8.0.0.1</Version>
<FileVersion>8.0.0.1</FileVersion>
<AssemblyVersion>8.0.0.1</AssemblyVersion>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal static string ReverseFullName(Type type, bool ignoreFlag = false)


//后缀
StringBuilder Suffix = new StringBuilder();
StringBuilder Suffix = new();


//数组判别
Expand Down Expand Up @@ -68,7 +68,7 @@ internal static string ReverseFullName(Type type, bool ignoreFlag = false)
if (type.IsGenericType)
{

StringBuilder result = new StringBuilder();
StringBuilder result = new();
if (string.IsNullOrEmpty(fatherString) && !string.IsNullOrEmpty(type.Namespace) && !string.IsNullOrEmpty(type.FullName))
{
result.Append(type.Namespace + ".");
Expand Down Expand Up @@ -161,7 +161,7 @@ public static string ReverseTypeName(Type type)


//后缀
StringBuilder Suffix = new StringBuilder();
StringBuilder Suffix = new();


//数组判别
Expand Down Expand Up @@ -194,7 +194,7 @@ public static string ReverseTypeName(Type type)
if (type.IsGenericType)
{

StringBuilder result = new StringBuilder();
StringBuilder result = new();
result.Append($"{type.Name.Split('`')[0]}<");

if (type.GenericTypeArguments.Length > 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static bool IsImplementFrom<T>(this Type type)

public static HashSet<Type> GetAllTypes(this Type type)
{
HashSet<Type> result = new();
HashSet<Type> result = [];
type.GetAllTypes(result);
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion test/workflow/Workflow.Runner/Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public async Task InitLabels()
if (labels != null && labels.Any())
{
labels.Add(new GithubLabelBase() { Name = "aaa-block-user", Color = "ff0000", Description = "该标签为屏蔽标签,打上该标签的 ISSUE 作者将被屏蔽." });
labels.Add(new GithubLabelBase() { Name = "phasee-done", Color = "020E91", Description = "任务或计划阶段性结束." });
labels.Add(new GithubLabelBase() { Name = "phase-done", Color = "020E91", Description = "任务或计划阶段性结束." });
labels.Add(new GithubLabelBase() { Name = "continuous", Color = "AEB941", Description = "该问题或建议或编码任务将持续进行." });
labels.Add(new GithubLabelBase() { Name = "tasklist", Color = "bfdadc", Description = "计划任务清单." });
var result = await NMSGithubSdk.ExpectLabelsCreateAndUpadateAsync(labels, repoId, ownerName, repoName);
Expand Down

0 comments on commit 9373db1

Please sign in to comment.