Skip to content

Commit

Permalink
升级依赖, 增加兼容代码
Browse files Browse the repository at this point in the history
  • Loading branch information
NMSAzulX committed Oct 11, 2022
1 parent ca90611 commit c83d79b
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/NMS.Leo.Typed/Leo.Typed.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0;</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net5.0;net6.0;</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
</PropertyGroup>
Expand Down
22 changes: 18 additions & 4 deletions src/NMS.Leo/Core/Builder/DictBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using BTFindTree;
using Natasha.CSharp.Compiler;
using NMS.Leo.Metadata;
using System.Reflection;
using System.Text;
using NMS.Leo.Metadata;
using Natasha.CSharp.Compiler;

#if NETCOREAPP3_1_OR_GREATER
using Natasha.CSharp.MultiDomain.Extension;
#endif

// ReSharper disable once CheckNamespace
namespace NMS.Leo.Builder;
Expand Down Expand Up @@ -203,8 +207,14 @@ public static Type InitType(Type type, AlgorithmKind kind = AlgorithmKind.Hash)
}


var classBuilder = NClass.UseDomain(type.GetDomain())
var classBuilder = NClass
#if NETCOREAPP3_1_OR_GREATER
.UseDomain(type.GetDomain())
#else
.DefaultDomain()
#endif
.Public()
.Modifier(ModifierFlags.Sealed)
.Using(type)
.Namespace("NMS.Leo.NCallerDynamic")
.InheritanceAppend(callType)
Expand All @@ -229,7 +239,11 @@ public static Type InitType(Type type, AlgorithmKind kind = AlgorithmKind.Hash)
private static Action<Dictionary<string, LeoMember>> InitMetadataMappingCaller(Type runtimeProxyType)
{
return NDelegate
.UseDomain(runtimeProxyType.GetDomain())
#if NETCOREAPP3_1_OR_GREATER
.UseDomain(runtimeProxyType.GetDomain())
#else
.DefaultDomain()
#endif
.Action<Dictionary<string, LeoMember>>($"{runtimeProxyType.GetDevelopName()}.InitMetadataMapping(obj);");
}
}
17 changes: 12 additions & 5 deletions src/NMS.Leo/Core/Builder/FuzzyDictBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using BTFindTree;
using System.Collections.Concurrent;
#if NETCOREAPP3_1_OR_GREATER
using Natasha.CSharp.MultiDomain.Extension;
#endif

// ReSharper disable once CheckNamespace
namespace NMS.Leo.Builder;
Expand All @@ -14,7 +17,7 @@ static FuzzyDictBuilder()

private static readonly ConcurrentDictionary<Type, string> _type_cache;
private static readonly ConcurrentDictionary<string, string> _str_cache;

public static unsafe DictBase Ctor(Type type)
{
//获得动态生成的类型
Expand All @@ -32,11 +35,15 @@ public static unsafe DictBase Ctor(Type type)

//生成脚本
var newAction = NDelegate
.UseDomain(type.GetDomain())
.ConfigUsing(_type_cache.Keys.ToArray(),"NMS.Leo.NCallerDynamic")
#if NETCOREAPP3_1_OR_GREATER
.UseDomain(type.GetDomain())
#else
.DefaultDomain()
#endif
.ConfigUsing(_type_cache.Keys.ToArray(), "NMS.Leo.NCallerDynamic")
.UnsafeFunc<Type, DictBase>(newFindTree);

FuzzyDictOperator.CreateFromString = (delegate * managed<Type, DictBase>)(newAction.Method.MethodHandle.GetFunctionPointer());
return (DictBase) Activator.CreateInstance(proxyType);
FuzzyDictOperator.CreateFromString = (delegate* managed<Type, DictBase>)(newAction.Method.MethodHandle.GetFunctionPointer());
return (DictBase)Activator.CreateInstance(proxyType);
}
}
10 changes: 9 additions & 1 deletion src/NMS.Leo/Core/Builder/HashDictBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using BTFindTree;
using System.Collections.Concurrent;

#if NETCOREAPP3_1_OR_GREATER
using Natasha.CSharp.MultiDomain.Extension;
#endif

// ReSharper disable once CheckNamespace
namespace NMS.Leo.Builder;

Expand Down Expand Up @@ -32,7 +36,11 @@ public static unsafe DictBase Ctor(Type type)

//生成脚本
var newAction = NDelegate
.UseDomain(type.GetDomain())
#if NETCOREAPP3_1_OR_GREATER
.UseDomain(type.GetDomain())
#else
.DefaultDomain()
#endif
.ConfigUsing(_type_cache.Keys.ToArray(), "NMS.Leo.NCallerDynamic")
.UnsafeFunc<Type, DictBase>(newFindTree);

Expand Down
9 changes: 8 additions & 1 deletion src/NMS.Leo/Core/Builder/PrecisionDictBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using BTFindTree;
using System.Collections.Concurrent;
#if NETCOREAPP3_1_OR_GREATER
using Natasha.CSharp.MultiDomain.Extension;
#endif

// ReSharper disable once CheckNamespace
namespace NMS.Leo.Builder;
Expand Down Expand Up @@ -32,7 +35,11 @@ public static unsafe DictBase Ctor(Type type)

//生成脚本
var newAction = NDelegate
.UseDomain(type.GetDomain())
#if NETCOREAPP3_1_OR_GREATER
.UseDomain(type.GetDomain())
#else
.DefaultDomain()
#endif
.ConfigUsing(_type_cache.Keys.ToArray(), "NMS.Leo.NCallerDynamic")
.UnsafeFunc<Type, DictBase>(newFindTree);

Expand Down
4 changes: 2 additions & 2 deletions src/NMS.Leo/Leo.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net5.0;net6.0;</TargetFrameworks>
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net5.0;net6.0;</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
</PropertyGroup>
Expand Down Expand Up @@ -36,7 +36,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NMS.DynamicDictionary" Version="2.4.2" />
<PackageReference Include="NMS.DynamicDictionary" Version="2.5.0" />
</ItemGroup>

</Project>
5 changes: 4 additions & 1 deletion test/NCallerUT/DictStaticTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,24 @@ static StaticTest2(){
public static string Name;
public static int Age{get;set;}
private static int _j;
}
}";
//根据脚本创建动态类
var oop = new AssemblyCSharpBuilder();
oop.Add(text);
var type = oop.GetTypeFromShortName("StaticTest2");
type.AllowLeoPrivate();
//创建动态类实例代理
var instance = PrecisionDictOperator.CreateFromType(type);
//Get动态调用
Assert.Equal("111", (string) instance["Name"]);
//调用动态委托赋值
instance["Name"] = "222";

instance["_j"] = 222;
Assert.Equal("222", (string) instance["Name"]);
Assert.Equal("222", instance.Get<string>("Name"));
Assert.Equal(222, instance.Get<int>("_j"));
}


Expand Down

0 comments on commit c83d79b

Please sign in to comment.