Skip to content

Commit

Permalink
Merge pull request #215 from WeihanLi/dev
Browse files Browse the repository at this point in the history
1.0.68
  • Loading branch information
WeihanLi authored Aug 24, 2024
2 parents ff72b7d + 72c0ca9 commit fd3d6df
Show file tree
Hide file tree
Showing 37 changed files with 492 additions and 451 deletions.
14 changes: 7 additions & 7 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ExtensionPackageVersion Condition="'$(TargetFramework)' == 'netstandard2.0' OR '$(TargetFramework)' == 'netstandard2.1' OR '$(TargetFramework)' == 'net6.0'">6.0.0</ExtensionPackageVersion>
<ExtensionPackageVersion Condition="'$(TargetFramework)' == 'net7.0'">7.0.0</ExtensionPackageVersion>
<ExtensionPackageVersion Condition="'$(TargetFramework)' == 'net8.0'">8.0.0</ExtensionPackageVersion>
<ExtensionPackageVersion Condition="'$(TargetFramework)' == 'net9.0'">9.0.0-preview.6.24327.7</ExtensionPackageVersion>
<ExtensionPackageVersion Condition="'$(TargetFramework)' == 'net9.0'">9.0.0-preview.7.24405.7</ExtensionPackageVersion>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="$(ExtensionPackageVersion)" />
Expand All @@ -17,7 +17,7 @@
<PackageVersion Include="System.Reflection.Emit" Version="4.7.0" />
<PackageVersion Include="System.ComponentModel.Annotations" Version="5.0.0" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<PackageVersion Include="Serilog" Version="4.0.0" />
<PackageVersion Include="Serilog" Version="4.0.1" />
</ItemGroup>
<ItemGroup>
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
Expand All @@ -28,17 +28,17 @@
<PackageVersion Include="Xunit.DependencyInjection" Version="8.7.1" />
<PackageVersion Include="Xunit.DependencyInjection.Logging" Version="8.1.0" />
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
<PackageVersion Include="BenchmarkDotNet" Version="0.13.12" />
<PackageVersion Include="BenchmarkDotNet" Version="0.14.0" />
</ItemGroup>
<ItemGroup>
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.7" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.7" />
<PackageVersion Include="Microsoft.Extensions.ObjectPool" Version="8.0.7" />
<PackageVersion Include="Microsoft.EntityFrameworkCore" Version="8.0.8" />
<PackageVersion Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.8" />
<PackageVersion Include="Microsoft.Extensions.ObjectPool" Version="8.0.8" />
<PackageVersion Include="Serilog.Sinks.Console" Version="6.0.0" />
<PackageVersion Include="System.Data.SqlClient" Version="4.8.6" />
<PackageVersion Include="Dapper" Version="2.1.44" />
</ItemGroup>
<ItemGroup>
<GlobalPackageReference Include="PolySharp" Version="1.14.1" />
</ItemGroup>
</Project>
</Project>
8 changes: 4 additions & 4 deletions build/build.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright (c) 2022-2023 Weihan Li. All rights reserved.
// Licensed under the Apache license version 2.0 http://www.apache.org/licenses/LICENSE-2.0

var target = CommandLineParser.Val("target", "Default", args);
var apiKey = CommandLineParser.Val("apiKey", "", args);
var stable = CommandLineParser.BooleanVal("stable", false, args);
var noPush = CommandLineParser.BooleanVal("noPush", false, args);
var target = CommandLineParser.Val("target", args, "Default");
var apiKey = CommandLineParser.Val("apiKey", args);
var stable = CommandLineParser.BooleanVal("stable", args);
var noPush = CommandLineParser.BooleanVal("noPush", args);
var branchName = EnvHelper.Val("BUILD_SOURCEBRANCHNAME", "local");

var solutionPath = "./WeihanLi.Common.sln";
Expand Down
2 changes: 1 addition & 1 deletion build/version.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<VersionMajor>1</VersionMajor>
<VersionMinor>0</VersionMinor>
<VersionPatch>67</VersionPatch>
<VersionPatch>68</VersionPatch>
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
</PropertyGroup>
</Project>
11 changes: 3 additions & 8 deletions samples/DotNetCoreSample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@
// Licensed under the Apache license.

using DotNetCoreSample;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.Net.Mime;
using WeihanLi.Common.Event;
using WeihanLi.Common.Helpers;
using WeihanLi.Extensions;
using WeihanLi.Extensions.Dump;

Console.WriteLine("----------DotNetCoreSample----------");

Expand Down Expand Up @@ -348,7 +341,9 @@
// // registration would be disposed when cts dispose
// }

await InvokeHelper.TryInvokeAsync(EventTest.MainTest);
// await InvokeHelper.TryInvokeAsync(EventTest.MainTest);

InvokeHelper.TryInvoke(CommandExecutorTest.MainTest);

ConsoleHelper.ReadKeyWithPrompt("Press any key to exit");

Expand Down
2 changes: 2 additions & 0 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
<PackageIcon>icon.jpg</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<LangVersion>Preview</LangVersion>
</PropertyGroup>
<ItemGroup>
<Using Include="System.Object" Alias="Lock" Condition="!$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net9.0'))" />
<None Include="README.md" Pack="true" PackagePath="\" />
<None Include="$([MSBuild]::GetPathOfFileAbove('icon.jpg', '$(MSBuildThisFileDirectory)../'))" Pack="true" Visible="false" PackagePath=""/>
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/WeihanLi.Common.Logging.Serilog/SerilogHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace WeihanLi.Common.Logging.Serilog;

public static class SerilogHelper
{
private static readonly object Locker = new();
private static readonly Lock Locker = new();

public static void LogInit(Action<LoggerConfiguration> configureAction)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net6.0</TargetFrameworks>
<TargetFrameworks>netstandard2.0;net6.0;net9.0</TargetFrameworks>
<PackageProjectUrl>https://github.com/WeihanLi/WeihanLi.Common/tree/dev/src/WeihanLi.Common.Logging.Serilog</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageTags>$(PackageTags);logging;serilog</PackageTags>
Expand Down
9 changes: 5 additions & 4 deletions src/WeihanLi.Common/Aspect/AspectDelegate.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using WeihanLi.Common.Helpers;
using WeihanLi.Common.Helpers;
using WeihanLi.Extensions;

namespace WeihanLi.Common.Aspect;
Expand All @@ -24,7 +23,8 @@ public static void InvokeWithCompleteFunc(IInvocation invocation, Func<IInvocati
InvokeInternal(invocation, null, completeFunc);
}

[RequiresUnreferencedCode("Unreferenced code may be used.")]
[RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")]
[RequiresUnreferencedCode("Unreferenced code may be used")]
public static void InvokeInternal(IInvocation invocation, IReadOnlyList<IInterceptor>? interceptors, Func<IInvocation, Task>? completeFunc)
{
// enrich
Expand Down Expand Up @@ -87,7 +87,8 @@ public static void InvokeInternal(IInvocation invocation, IReadOnlyList<IInterce
}
}

[RequiresUnreferencedCode("Unreferenced code may be used.")]
[RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")]
[RequiresUnreferencedCode("Unreferenced code may be used")]
private static Func<IInvocation, Task> GetAspectDelegate(IInvocation invocation, IReadOnlyList<IInterceptor>? interceptors, Func<IInvocation, Task>? completeFunc)
{
// ReSharper disable once ConvertToLocalFunction
Expand Down
10 changes: 4 additions & 6 deletions src/WeihanLi.Common/Aspect/DefaultProxyFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ public sealed class DefaultProxyFactory
(IProxyTypeFactory proxyTypeFactory, IServiceProvider? serviceProvider = null) : IProxyFactory
{
public static readonly IProxyFactory Instance = new DefaultProxyFactory(DefaultProxyTypeFactory.Instance);

private readonly IProxyTypeFactory _proxyTypeFactory = proxyTypeFactory;
private readonly IServiceProvider _serviceProvider = serviceProvider ?? DependencyResolver.Current;

[RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")]
Expand All @@ -17,7 +15,7 @@ public object CreateProxy(Type serviceType, object?[] arguments)
{
Guard.NotNull(serviceType);

var proxyType = _proxyTypeFactory.CreateProxyType(serviceType);
var proxyType = proxyTypeFactory.CreateProxyType(serviceType);
var proxy = _serviceProvider.CreateInstance(proxyType, arguments);
return proxy;
}
Expand All @@ -29,7 +27,7 @@ public object CreateProxy(Type serviceType, Type implementType, params object?[]
Guard.NotNull(serviceType);
Guard.NotNull(implementType);

var proxyType = _proxyTypeFactory.CreateProxyType(serviceType, implementType);
var proxyType = proxyTypeFactory.CreateProxyType(serviceType, implementType);
if (serviceType.IsInterface)
{
var implement = _serviceProvider.CreateInstance(implementType, arguments);
Expand All @@ -51,8 +49,8 @@ public object CreateProxyWithTarget(Type serviceType, object implement, object?[
var implementType = implement.GetType();

var proxyType = serviceType.IsClass
? _proxyTypeFactory.CreateProxyType(serviceType)
: _proxyTypeFactory.CreateProxyType(serviceType, implementType)
? proxyTypeFactory.CreateProxyType(serviceType)
: proxyTypeFactory.CreateProxyType(serviceType, implementType)
;
var proxy = _serviceProvider.CreateInstance(proxyType, arguments);
ProxyUtils.SetProxyTarget(proxy, implement);
Expand Down
4 changes: 1 addition & 3 deletions src/WeihanLi.Common/Aspect/DefaultProxyTypeFactory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Diagnostics.CodeAnalysis;

namespace WeihanLi.Common.Aspect;
namespace WeihanLi.Common.Aspect;

public sealed class DefaultProxyTypeFactory : IProxyTypeFactory
{
Expand Down
1 change: 1 addition & 0 deletions src/WeihanLi.Common/Aspect/IInterceptorResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

public interface IInterceptorResolver
{
[RequiresUnreferencedCode("Unreferenced code may be used")]
IReadOnlyList<IInterceptor> ResolveInterceptors(IInvocation invocation);
}
10 changes: 9 additions & 1 deletion src/WeihanLi.Common/Aspect/IProxyFactory.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
namespace WeihanLi.Common.Aspect;
using System.Diagnostics.CodeAnalysis;

namespace WeihanLi.Common.Aspect;

public interface IProxyFactory
{
[RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")]
[RequiresUnreferencedCode("Unreferenced code may be used")]
object CreateProxy(Type serviceType, object?[] arguments);

[RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")]
[RequiresUnreferencedCode("Unreferenced code may be used")]
object CreateProxy(Type serviceType, Type implementType, params object?[] arguments);

[RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")]
[RequiresUnreferencedCode("Unreferenced code may be used")]
object CreateProxyWithTarget(Type serviceType, object implement, object?[] arguments);
}
4 changes: 4 additions & 0 deletions src/WeihanLi.Common/Aspect/IProxyTypeFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

public interface IProxyTypeFactory
{
[RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")]
[RequiresUnreferencedCode("Unreferenced code may be used")]
Type CreateProxyType(Type serviceType);

[RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")]
[RequiresUnreferencedCode("Unreferenced code may be used")]
Type CreateProxyType(Type serviceType, Type implementType);
}
9 changes: 6 additions & 3 deletions src/WeihanLi.Common/Aspect/InternalAspectHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ internal static class MethodInvokeHelper
{
public static readonly MethodInfo GetInvocationReturnValueMethod =
typeof(AspectInvocation).GetProperty("ReturnValue")!.GetGetMethod()!;


[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "<Pending>")]
public static readonly MethodInfo InvokeAspectDelegateMethod =
typeof(AspectDelegate).GetMethod(nameof(AspectDelegate.Invoke))!;


[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "<Pending>")]
public static readonly MethodInfo GetCurrentMethod =
typeof(MethodBase).GetMethod(nameof(MethodBase.GetCurrentMethod))!;


[UnconditionalSuppressMessage("Trimming", "IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code", Justification = "<Pending>")]
public static readonly ConstructorInfo AspectInvocationConstructor =
typeof(AspectInvocation).GetConstructors()[0];
}
30 changes: 29 additions & 1 deletion src/WeihanLi.Common/Aspect/ProxyFactoryExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
namespace WeihanLi.Common.Aspect;
using System.Diagnostics.CodeAnalysis;

namespace WeihanLi.Common.Aspect;

public static class ProxyFactoryExtensions
{
[RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")]
[RequiresUnreferencedCode("Unreferenced code may be used")]
public static TInterface CreateInterfaceProxy<TInterface>(this IProxyFactory proxyGenerator)
where TInterface : class
{
Expand All @@ -13,6 +17,8 @@ public static TInterface CreateInterfaceProxy<TInterface>(this IProxyFactory pro
return proxyGenerator.CreateProxy<TInterface>();
}

[RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")]
[RequiresUnreferencedCode("Unreferenced code may be used")]
public static TInterface CreateInterfaceProxy<TInterface, TImplement>(this IProxyFactory proxyGenerator, params object?[] arguments) where TImplement : TInterface
where TInterface : class
{
Expand All @@ -24,6 +30,8 @@ public static TInterface CreateInterfaceProxy<TInterface, TImplement>(this IProx
return proxyGenerator.CreateProxy<TInterface, TImplement>(arguments);
}

[RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")]
[RequiresUnreferencedCode("Unreferenced code may be used")]
public static TInterface CreateInterfaceProxy<TInterface, TImplement>(this IProxyFactory proxyGenerator, TImplement implement) where TImplement : TInterface
where TInterface : class
{
Expand All @@ -35,6 +43,8 @@ public static TInterface CreateInterfaceProxy<TInterface, TImplement>(this IProx
return proxyGenerator.CreateProxyWithTarget<TInterface>(implement);
}

[RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")]
[RequiresUnreferencedCode("Unreferenced code may be used")]
public static TClass CreateClassProxy<TClass>(this IProxyFactory proxyGenerator, params object?[] arguments) where TClass : class
{
var type = typeof(TClass);
Expand All @@ -45,6 +55,8 @@ public static TClass CreateClassProxy<TClass>(this IProxyFactory proxyGenerator,
return proxyGenerator.CreateProxy<TClass>(arguments);
}

[RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")]
[RequiresUnreferencedCode("Unreferenced code may be used")]
public static TClass CreateClassProxy<TClass, TImplement>(this IProxyFactory proxyGenerator, params object?[] arguments) where TImplement : TClass
where TClass : class
{
Expand All @@ -56,6 +68,8 @@ public static TClass CreateClassProxy<TClass, TImplement>(this IProxyFactory pro
return proxyGenerator.CreateProxy<TClass, TImplement>(arguments);
}

[RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")]
[RequiresUnreferencedCode("Unreferenced code may be used")]
public static TClass CreateClassProxy<TClass, TImplement>(this IProxyFactory proxyGenerator, TImplement implement) where TImplement : TClass
where TClass : class
{
Expand All @@ -69,16 +83,22 @@ public static TClass CreateClassProxy<TClass, TImplement>(this IProxyFactory pro

#region CreateProxy

[RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")]
[RequiresUnreferencedCode("Unreferenced code may be used")]
public static object CreateProxy(this IProxyFactory proxyFactory, Type serviceType, params object?[] arguments)
{
return proxyFactory.CreateProxy(serviceType, arguments);
}

[RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")]
[RequiresUnreferencedCode("Unreferenced code may be used")]
public static TService CreateProxy<TService>(this IProxyFactory proxyFactory, params object?[] arguments) where TService : class
{
return (TService)proxyFactory.CreateProxy(typeof(TService), arguments);
}

[RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")]
[RequiresUnreferencedCode("Unreferenced code may be used")]
public static TService CreateProxy<TService, TImplement>(this IProxyFactory proxyFactory, params object?[] arguments)
where TImplement : TService
where TService : class
Expand All @@ -90,25 +110,33 @@ public static TService CreateProxy<TService, TImplement>(this IProxyFactory prox

#region CreateProxyWithTarget

[RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")]
[RequiresUnreferencedCode("Unreferenced code may be used")]
public static TService CreateProxyWithTarget<TService, TImplement>(this IProxyFactory proxyFactory, TImplement target)
where TImplement : TService
where TService : class
{
return (TService)proxyFactory.CreateProxyWithTarget(typeof(TService), target);
}

[RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")]
[RequiresUnreferencedCode("Unreferenced code may be used")]
public static TService CreateProxyWithTarget<TService>(this IProxyFactory proxyFactory, object target)
where TService : class
{
return (TService)proxyFactory.CreateProxyWithTarget(typeof(TService), target);
}

[RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")]
[RequiresUnreferencedCode("Unreferenced code may be used")]
public static TService CreateProxyWithTarget<TService>(this IProxyFactory proxyFactory, object target, object[] arguments)
where TService : class
{
return (TService)proxyFactory.CreateProxyWithTarget(typeof(TService), target, arguments);
}

[RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")]
[RequiresUnreferencedCode("Unreferenced code may be used")]
public static object CreateProxyWithTarget(this IProxyFactory proxyFactory, Type serviceType, object target)
{
return proxyFactory.CreateProxyWithTarget(serviceType, target, []);
Expand Down
4 changes: 3 additions & 1 deletion src/WeihanLi.Common/Aspect/ProxyUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal static class ProxyUtils
private static readonly Dictionary<string, Type> _proxyTypes = [];

private const string TargetFieldName = "__target";
private static readonly object _typeLock = new();
private static readonly Lock _typeLock = new();

private static readonly Func<Type, Type?, string> _proxyTypeNameResolver;

Expand Down Expand Up @@ -426,6 +426,8 @@ public static Type CreateClassProxy(Type serviceType, Type? implementType)
}
}

[RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")]
[RequiresUnreferencedCode("Unreferenced code may be used")]
public static void SetProxyTarget(object? proxyService, object? target)
{
if (null != proxyService && null != target)
Expand Down
Loading

0 comments on commit fd3d6df

Please sign in to comment.