From 10eb3df0a93c8c372f7ffd43118bca1d41888043 Mon Sep 17 00:00:00 2001 From: Weihan Li Date: Thu, 4 Jan 2024 01:33:39 +0800 Subject: [PATCH] refactor: apply dotnet-format --- samples/DotNetCoreSample/AppHostTest.cs | 4 +- .../DotNetCoreSample/CommandExecutorTest.cs | 4 +- samples/DotNetCoreSample/DisposeTest.cs | 10 ++-- .../DotNetCoreSample/NewtonJsonFormatter.cs | 10 ++-- samples/DotNetCoreSample/Program.cs | 4 +- src/WeihanLi.Common/Aspect/AspectDelegate.cs | 2 +- .../Aspect/FluentAspectOptionsExtensions.cs | 14 +++--- .../Aspect/IInterceptionConfiguration.cs | 2 +- src/WeihanLi.Common/Aspect/ProxyUtils.cs | 10 ++-- src/WeihanLi.Common/CacheUtil.cs | 8 +-- src/WeihanLi.Common/Data/Repository.cs | 2 +- .../DependencyInjection/ServiceContainer.cs | 2 +- .../ServiceContainerBuilderExtensions.cs | 6 +-- .../DependencyInjection/ServiceDefinition.cs | 14 +++--- src/WeihanLi.Common/DependencyResolver.cs | 3 +- .../Event/EventBusExtensions.cs | 6 +-- .../Extensions/CoreExtension.cs | 12 +++-- .../Extensions/DataExtension.cs | 3 +- .../Extensions/ExpressionExtension.cs | 2 +- .../Extensions/ILGeneratorExtensions.cs | 50 +++++++++---------- .../Extensions/ProcessExtension.cs | 2 +- .../Extensions/ReflectionExtension.cs | 4 +- .../Extensions/ServiceCollectionExtension.cs | 14 +++--- .../Extensions/TypeExtension.cs | 9 ++-- .../Helpers/ActivatorHelper.cs | 20 ++++---- .../Helpers/ApplicationHelper.cs | 2 +- .../Helpers/BoundedConcurrentQueue.cs | 8 +-- .../Helpers/CommandExecutor.cs | 28 +++++------ src/WeihanLi.Common/Helpers/ConsoleHelper.cs | 6 +-- src/WeihanLi.Common/Helpers/DataSerializer.cs | 8 +-- .../Helpers/DiagnosticHelper.cs | 4 +- src/WeihanLi.Common/Helpers/EnumHelper.cs | 12 ++--- .../Hosting/AppHostBuilderExtensions.cs | 2 +- src/WeihanLi.Common/Helpers/MapHelper.cs | 6 +-- .../Helpers/PipelineBuilder.cs | 2 +- .../Pipelines/PipelineBuilderExtensions.cs | 4 +- src/WeihanLi.Common/Helpers/ReflectHelper.cs | 2 +- src/WeihanLi.Common/Helpers/TaskHelper.cs | 6 +-- src/WeihanLi.Common/Http/MockHttpHandler.cs | 2 +- .../Logging/LogHelperExtensions.cs | 6 +-- .../ConfigurationRenderMiddleare.cs | 2 +- .../Templating/DefaultRenderMiddleware.cs | 4 +- .../DependencyInjectionExtensions.cs | 8 +-- .../Templating/TemplateEngine.cs | 2 +- .../Templating/TemplateRendererBuilder.cs | 4 +- .../HostingExtensions.cs | 2 +- .../TimerBaseBackgroundService.cs | 2 +- .../ConfigurationExtensionTest.cs | 2 +- .../ExtensionsTest/StringExtensionTest.cs | 12 ++--- .../HelpersTest/BoundedConcurrentQueue.cs | 2 +- 50 files changed, 180 insertions(+), 175 deletions(-) diff --git a/samples/DotNetCoreSample/AppHostTest.cs b/samples/DotNetCoreSample/AppHostTest.cs index 8547c87d..98d9741f 100644 --- a/samples/DotNetCoreSample/AppHostTest.cs +++ b/samples/DotNetCoreSample/AppHostTest.cs @@ -99,7 +99,7 @@ public async Task StartAsync(CancellationToken cancellationToken) _listener.Start(); var logger = _serviceProvider.GetRequiredService>(); logger.LogInformation("WebServer started"); - + while (!cancellationToken.IsCancellationRequested) { var listenerContext = await _listener.GetContextAsync(); @@ -135,7 +135,7 @@ public WebServerHostedService(IWebServer server) { _server = server; } - + public override async Task StopAsync(CancellationToken cancellationToken) { await _server.StopAsync(cancellationToken); diff --git a/samples/DotNetCoreSample/CommandExecutorTest.cs b/samples/DotNetCoreSample/CommandExecutorTest.cs index c9a130f3..6b4cddff 100644 --- a/samples/DotNetCoreSample/CommandExecutorTest.cs +++ b/samples/DotNetCoreSample/CommandExecutorTest.cs @@ -13,11 +13,11 @@ public static void MainTest() ExecuteAndCapture("hostname") .PrintOutputToConsole() .EnsureSuccessExitCode(); - + ExecuteAndOutput("hostname").EnsureSuccessExitCode(); ExecuteAndOutputAsync("hostname").Wait(); - + ExecuteCommandAndOutput("hostname").EnsureSuccessExitCode(); } } diff --git a/samples/DotNetCoreSample/DisposeTest.cs b/samples/DotNetCoreSample/DisposeTest.cs index 0340a652..82f008bf 100644 --- a/samples/DotNetCoreSample/DisposeTest.cs +++ b/samples/DotNetCoreSample/DisposeTest.cs @@ -30,7 +30,7 @@ public static void MainTest() Name = "MainTest2" }; Console.WriteLine(service.GetType()); - + service = null; Console.WriteLine(service is null); } @@ -39,7 +39,7 @@ public static void MainTest() Console.WriteLine(@$"---- {nameof(MainTest)} end"); } - + public static async ValueTask MainTestAsync() { Console.WriteLine(@$"---- {nameof(MainTestAsync)} start"); @@ -63,7 +63,7 @@ public static async ValueTask MainTestAsync() Name = "MainTestAsync2" }; Console.WriteLine(service.GetType()); - + service = null; Console.WriteLine(service is null); } @@ -77,12 +77,12 @@ public static async ValueTask MainTestAsync() file sealed class TestService : DisposableBase { public required string Name { get; init; } - + protected override void Dispose(bool disposing) { if (disposing) { - Console.WriteLine($@"<<{Name}>> disposes managed resources"); + Console.WriteLine($@"<<{Name}>> disposes managed resources"); } Console.WriteLine($@"<<{Name}>> disposes unmanaged resources"); base.Dispose(disposing); diff --git a/samples/DotNetCoreSample/NewtonJsonFormatter.cs b/samples/DotNetCoreSample/NewtonJsonFormatter.cs index a9108242..685f1455 100644 --- a/samples/DotNetCoreSample/NewtonJsonFormatter.cs +++ b/samples/DotNetCoreSample/NewtonJsonFormatter.cs @@ -11,14 +11,14 @@ namespace DotNetCoreSample; -public sealed class NewtonJsonFormatterOptions: ConsoleFormatterOptions +public sealed class NewtonJsonFormatterOptions : ConsoleFormatterOptions { } -public sealed class NewtonJsonFormatter: ConsoleFormatter +public sealed class NewtonJsonFormatter : ConsoleFormatter { public const string FormatterName = "NewtonJson"; - + private readonly NewtonJsonFormatterOptions _options; public NewtonJsonFormatter(IOptions options) : base(FormatterName) @@ -73,7 +73,7 @@ public override void Write(in LogEntry logEntry, IExternalScopeP } writer.WriteEndObject(); } - + writer.WriteEndObject(); writer.Flush(); textWriter.WriteLine(); @@ -82,7 +82,7 @@ public override void Write(in LogEntry logEntry, IExternalScopeP public static partial class LoggingBuilderExtensions { - public static ILoggingBuilder AddNewtonJsonConsole(this ILoggingBuilder loggingBuilder, + public static ILoggingBuilder AddNewtonJsonConsole(this ILoggingBuilder loggingBuilder, Action? optionsConfigure = null) { loggingBuilder.AddConsole(options => options.FormatterName = NewtonJsonFormatter.FormatterName); diff --git a/samples/DotNetCoreSample/Program.cs b/samples/DotNetCoreSample/Program.cs index 1f272eda..f3a56926 100644 --- a/samples/DotNetCoreSample/Program.cs +++ b/samples/DotNetCoreSample/Program.cs @@ -1,11 +1,11 @@ -using System.Net.Mime; -// Copyright (c) Weihan Li. All rights reserved. +// Copyright (c) Weihan Li. All rights reserved. // 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; using WeihanLi.Common.Aspect; using WeihanLi.Common.Event; diff --git a/src/WeihanLi.Common/Aspect/AspectDelegate.cs b/src/WeihanLi.Common/Aspect/AspectDelegate.cs index 7a4af19d..e6f37179 100644 --- a/src/WeihanLi.Common/Aspect/AspectDelegate.cs +++ b/src/WeihanLi.Common/Aspect/AspectDelegate.cs @@ -86,7 +86,7 @@ public static void InvokeInternal(IInvocation invocation, IReadOnlyList GetAspectDelegate(IInvocation invocation, IReadOnlyList? interceptors, Func? completeFunc) { diff --git a/src/WeihanLi.Common/Aspect/FluentAspectOptionsExtensions.cs b/src/WeihanLi.Common/Aspect/FluentAspectOptionsExtensions.cs index 05e73794..010e4927 100644 --- a/src/WeihanLi.Common/Aspect/FluentAspectOptionsExtensions.cs +++ b/src/WeihanLi.Common/Aspect/FluentAspectOptionsExtensions.cs @@ -67,7 +67,7 @@ public static IInterceptionConfiguration InterceptMethod(this FluentAspectOption return options.InterceptMethod(m => m.GetSignature().Equals(methodSignature)); } - public static IInterceptionConfiguration InterceptPropertyGetter<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)]T>(this FluentAspectOptions options, + public static IInterceptionConfiguration InterceptPropertyGetter<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(this FluentAspectOptions options, Expression> expression) { var prop = expression.GetProperty(); @@ -84,7 +84,7 @@ public static IInterceptionConfiguration InterceptMethod(this FluentAspectOption return options.InterceptMethod(prop.GetMethod); } - public static IInterceptionConfiguration InterceptPropertySetter<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)]T>(this FluentAspectOptions options, + public static IInterceptionConfiguration InterceptPropertySetter<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(this FluentAspectOptions options, Expression> expression) { var prop = expression.GetProperty(); @@ -234,7 +234,7 @@ public static FluentAspectOptions NoInterceptMethod(this FluentAspectOptions opt return options.NoInterceptMethod(m => m.GetSignature().Equals(methodSignature)); } - public static FluentAspectOptions NoInterceptProperty<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)]T>(this FluentAspectOptions options, + public static FluentAspectOptions NoInterceptProperty<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(this FluentAspectOptions options, Expression> expression) { var prop = expression.GetProperty(); @@ -254,7 +254,7 @@ public static FluentAspectOptions NoInterceptMethod(this FluentAspectOptions opt return options; } - public static FluentAspectOptions NoInterceptPropertyGetter<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)]T>(this FluentAspectOptions options, + public static FluentAspectOptions NoInterceptPropertyGetter<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(this FluentAspectOptions options, Expression> expression) { var prop = expression.GetProperty(); @@ -271,7 +271,7 @@ public static FluentAspectOptions NoInterceptMethod(this FluentAspectOptions opt return options.NoInterceptMethod(prop.GetMethod); } - public static FluentAspectOptions NoInterceptPropertySetter<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)]T>(this FluentAspectOptions options, + public static FluentAspectOptions NoInterceptPropertySetter<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(this FluentAspectOptions options, Expression> expression) { var prop = expression.GetProperty(); @@ -323,8 +323,8 @@ public static FluentAspectOptions UseProxyFactory(this FluentAspe options.ProxyFactory = new TProxyFactory(); return options; } - - public static FluentAspectOptions UseProxyFactory<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]TProxyFactory>(this FluentAspectOptions options, + + public static FluentAspectOptions UseProxyFactory<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TProxyFactory>(this FluentAspectOptions options, params object[] parameters) where TProxyFactory : class, IProxyFactory { options.ProxyFactory = ActivatorHelper.CreateInstance(parameters); diff --git a/src/WeihanLi.Common/Aspect/IInterceptionConfiguration.cs b/src/WeihanLi.Common/Aspect/IInterceptionConfiguration.cs index 7afa154d..c184ebb4 100644 --- a/src/WeihanLi.Common/Aspect/IInterceptionConfiguration.cs +++ b/src/WeihanLi.Common/Aspect/IInterceptionConfiguration.cs @@ -40,7 +40,7 @@ public static IInterceptionConfiguration With(this IInterceptionConfiguration in return interceptionConfiguration; } - public static IInterceptionConfiguration With<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]TInterceptor>(this IInterceptionConfiguration interceptionConfiguration, params object?[] parameters) where TInterceptor : IInterceptor + public static IInterceptionConfiguration With<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TInterceptor>(this IInterceptionConfiguration interceptionConfiguration, params object?[] parameters) where TInterceptor : IInterceptor { if (Guard.NotNull(parameters, nameof(parameters)).Length == 0) { diff --git a/src/WeihanLi.Common/Aspect/ProxyUtils.cs b/src/WeihanLi.Common/Aspect/ProxyUtils.cs index e134227e..67d9dd2e 100644 --- a/src/WeihanLi.Common/Aspect/ProxyUtils.cs +++ b/src/WeihanLi.Common/Aspect/ProxyUtils.cs @@ -29,7 +29,7 @@ internal static class ProxyUtils "GetType", "Finalize", }; - + static ProxyUtils() { var asmBuilder = AssemblyBuilder.DefineDynamicAssembly(new AssemblyName(ProxyAssemblyName), AssemblyBuilderAccess.Run); @@ -70,7 +70,7 @@ private static string GetFriendlyTypeName(this Type? type) return type.IsBasicType() ? type.Name : type.FullName ?? type.Name; } - + [RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")] [RequiresUnreferencedCode("Unreferenced code may be used")] public static Type CreateInterfaceProxy(Type interfaceType) @@ -315,7 +315,7 @@ public static Type CreateInterfaceProxy(Type interfaceType, Type? implementType) public static Type CreateClassProxy(Type serviceType, Type? implementType) { Guard.NotNull(serviceType); - + if (implementType is null) { implementType = serviceType; @@ -767,7 +767,7 @@ private static GenericParameterAttributes ToClassGenericParameterAttributes(Gene return GenericParameterAttributes.None; } } - + [RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")] [RequiresUnreferencedCode("Unreferenced code may be used")] private static CustomAttributeBuilder DefineCustomAttribute(CustomAttributeData customAttributeData) @@ -776,7 +776,7 @@ private static CustomAttributeBuilder DefineCustomAttribute(CustomAttributeData if (customAttributeData.NamedArguments is null) return new CustomAttributeBuilder(customAttributeData.Constructor, customAttributeData.ConstructorArguments.Select(c => c.Value).ToArray()); - + var attributeTypeInfo = customAttributeData.AttributeType.GetTypeInfo(); var constructorArgs = customAttributeData.ConstructorArguments .Select(ReadAttributeValue) diff --git a/src/WeihanLi.Common/CacheUtil.cs b/src/WeihanLi.Common/CacheUtil.cs index a0d6d0c0..2c8a241a 100644 --- a/src/WeihanLi.Common/CacheUtil.cs +++ b/src/WeihanLi.Common/CacheUtil.cs @@ -9,14 +9,14 @@ public static class CacheUtil { private static readonly ConcurrentDictionary TypePropertyCache = new(); private static readonly ConcurrentDictionary TypeFieldCache = new(); - - public static PropertyInfo[] GetTypeProperties([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)]Type type) + + public static PropertyInfo[] GetTypeProperties([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] Type type) { Guard.NotNull(type); return TypePropertyCache.GetOrAdd(type, _ => type.GetProperties()); } - - public static FieldInfo[] GetTypeFields([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)]Type type) + + public static FieldInfo[] GetTypeFields([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] Type type) { Guard.NotNull(type); return TypeFieldCache.GetOrAdd(type, _ => type.GetFields()); diff --git a/src/WeihanLi.Common/Data/Repository.cs b/src/WeihanLi.Common/Data/Repository.cs index a4f89284..734e6abe 100644 --- a/src/WeihanLi.Common/Data/Repository.cs +++ b/src/WeihanLi.Common/Data/Repository.cs @@ -11,7 +11,7 @@ namespace WeihanLi.Common.Data; [CLSCompliant(false)] -public class Repository<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors|DynamicallyAccessedMemberTypes.PublicProperties)]TEntity> : IRepository where TEntity : new() +public class Repository<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.PublicProperties)] TEntity> : IRepository where TEntity : new() { #region TODO: Cache External diff --git a/src/WeihanLi.Common/DependencyInjection/ServiceContainer.cs b/src/WeihanLi.Common/DependencyInjection/ServiceContainer.cs index d975e264..5cd999de 100644 --- a/src/WeihanLi.Common/DependencyInjection/ServiceContainer.cs +++ b/src/WeihanLi.Common/DependencyInjection/ServiceContainer.cs @@ -155,7 +155,7 @@ public void Dispose() [RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")] private object? GetServiceInstance(Type serviceType, ServiceDefinition serviceDefinition) => EnrichObject(GetServiceInstanceInternal(serviceType, serviceDefinition)); - + [RequiresDynamicCode("The native code for this instantiation might not be available at runtime.")] [RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")] private object? GetServiceInstanceInternal(Type serviceType, ServiceDefinition serviceDefinition) diff --git a/src/WeihanLi.Common/DependencyInjection/ServiceContainerBuilderExtensions.cs b/src/WeihanLi.Common/DependencyInjection/ServiceContainerBuilderExtensions.cs index 2c551b05..537996f5 100644 --- a/src/WeihanLi.Common/DependencyInjection/ServiceContainerBuilderExtensions.cs +++ b/src/WeihanLi.Common/DependencyInjection/ServiceContainerBuilderExtensions.cs @@ -7,7 +7,7 @@ namespace WeihanLi.Common.DependencyInjection; public static partial class ServiceContainerBuilderExtensions { - public static IServiceContainerBuilder AddSingleton<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]TService>(this IServiceContainerBuilder serviceContainerBuilder, TService service) + public static IServiceContainerBuilder AddSingleton<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TService>(this IServiceContainerBuilder serviceContainerBuilder, TService service) { Guard.NotNull(service, nameof(service)); serviceContainerBuilder.Add(new ServiceDefinition(service!, typeof(TService))); @@ -182,7 +182,7 @@ public static IServiceContainerBuilder RegisterAssemblyTypesAsImplementedInterfa /// service lifetime /// services public static IServiceContainerBuilder RegisterTypeAsImplementedInterfaces( - this IServiceContainerBuilder services, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]Type type, + this IServiceContainerBuilder services, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] Type type, ServiceLifetime serviceLifetime = ServiceLifetime.Singleton) => RegisterTypeAsImplementedInterfaces(services, type, null, serviceLifetime); @@ -194,7 +194,7 @@ public static IServiceContainerBuilder RegisterTypeAsImplementedInterfaces( /// interfaceTypeFilter /// service lifetime /// services - public static IServiceContainerBuilder RegisterTypeAsImplementedInterfaces(this IServiceContainerBuilder services, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]Type type, Func? interfaceTypeFilter, ServiceLifetime serviceLifetime = ServiceLifetime.Singleton) + public static IServiceContainerBuilder RegisterTypeAsImplementedInterfaces(this IServiceContainerBuilder services, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] Type type, Func? interfaceTypeFilter, ServiceLifetime serviceLifetime = ServiceLifetime.Singleton) { Guard.NotNull(type); foreach (var interfaceType in type.GetImplementedInterfaces()) diff --git a/src/WeihanLi.Common/DependencyInjection/ServiceDefinition.cs b/src/WeihanLi.Common/DependencyInjection/ServiceDefinition.cs index 5f2b6e4c..c9a4720c 100644 --- a/src/WeihanLi.Common/DependencyInjection/ServiceDefinition.cs +++ b/src/WeihanLi.Common/DependencyInjection/ServiceDefinition.cs @@ -58,27 +58,27 @@ public static ServiceDefinition Singleton(Func() where TServiceImplement : TService + public static ServiceDefinition Singleton() where TServiceImplement : TService { return new(typeof(TService), typeof(TServiceImplement), ServiceLifetime.Singleton); } - public static ServiceDefinition Singleton<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]TService>() + public static ServiceDefinition Singleton<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TService>() { return new(typeof(TService), ServiceLifetime.Singleton); } - public static ServiceDefinition Scoped<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]TService>(Func factory) + public static ServiceDefinition Scoped<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TService>(Func factory) { return new(typeof(TService), factory, ServiceLifetime.Scoped); } - public static ServiceDefinition Scoped() where TServiceImplement : TService + public static ServiceDefinition Scoped() where TServiceImplement : TService { return new(typeof(TService), typeof(TServiceImplement), ServiceLifetime.Scoped); } - public static ServiceDefinition Scoped<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]TService>() + public static ServiceDefinition Scoped<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TService>() { return new(typeof(TService), ServiceLifetime.Scoped); } @@ -88,12 +88,12 @@ public static ServiceDefinition Transient(Func() + public static ServiceDefinition Transient<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TService>() { return new(typeof(TService), ServiceLifetime.Transient); } - public static ServiceDefinition Transient() where TServiceImplement : TService + public static ServiceDefinition Transient() where TServiceImplement : TService { return new(typeof(TService), typeof(TServiceImplement), ServiceLifetime.Transient); } diff --git a/src/WeihanLi.Common/DependencyResolver.cs b/src/WeihanLi.Common/DependencyResolver.cs index e6e2932f..1acb744b 100644 --- a/src/WeihanLi.Common/DependencyResolver.cs +++ b/src/WeihanLi.Common/DependencyResolver.cs @@ -62,6 +62,7 @@ public ServiceProviderDependencyResolver(ServiceProvider serviceProvider) return _serviceProvider.GetService(serviceType); } + [RequiresUnreferencedCode("Calls WeihanLi.Common.DependencyInjectionExtensions.GetServices(Type)")] public IEnumerable GetServices(Type serviceType) { return _serviceProvider.GetServices(serviceType); @@ -92,7 +93,7 @@ public async Task TryInvokeServiceAsync(Func act private sealed class DefaultDependencyResolver : IDependencyResolver { - public object? GetService([DynamicallyAccessedMembers((DynamicallyAccessedMemberTypes.PublicParameterlessConstructor))]Type serviceType) + public object? GetService([DynamicallyAccessedMembers((DynamicallyAccessedMemberTypes.PublicParameterlessConstructor))] Type serviceType) { // Since attempting to create an instance of an interface or an abstract type results in an exception, immediately return null // to improve performance and the debugging experience with first-chance exceptions enabled. diff --git a/src/WeihanLi.Common/Event/EventBusExtensions.cs b/src/WeihanLi.Common/Event/EventBusExtensions.cs index aef1ab4e..85c4913b 100644 --- a/src/WeihanLi.Common/Event/EventBusExtensions.cs +++ b/src/WeihanLi.Common/Event/EventBusExtensions.cs @@ -39,8 +39,8 @@ public static IEventBuilder AddEvents(this IServiceCollection services) return new EventBuilder(services); } - - public static IEventBuilder AddEventHandler(this IEventBuilder eventBuilder, ServiceLifetime serviceLifetime = ServiceLifetime.Transient) + + public static IEventBuilder AddEventHandler(this IEventBuilder eventBuilder, ServiceLifetime serviceLifetime = ServiceLifetime.Transient) where TEvent : class, IEventBase where TEventHandler : class, IEventHandler { @@ -54,7 +54,7 @@ public static IEventBuilder AddEventHandler(this IEventBuilder eventBuil eventBuilder.Services.TryAddEnumerable(new ServiceDescriptor(typeof(IEventHandler), eventHandler)); return eventBuilder; } - + [RequiresUnreferencedCode("Assembly.GetTypes() requires unreferenced code")] public static IEventBuilder RegisterEventHandlers(this IEventBuilder builder, Func? filter = null, ServiceLifetime serviceLifetime = ServiceLifetime.Singleton, params Assembly[] assemblies) { diff --git a/src/WeihanLi.Common/Extensions/CoreExtension.cs b/src/WeihanLi.Common/Extensions/CoreExtension.cs index 6b8e4bb6..68ddb335 100644 --- a/src/WeihanLi.Common/Extensions/CoreExtension.cs +++ b/src/WeihanLi.Common/Extensions/CoreExtension.cs @@ -1564,9 +1564,9 @@ public static int Sign(this int value) /// Exception when exitCode not match the successCode public static int EnsureSuccessExitCode(this int exitCode, int successCode = 0) { - if (exitCode != 0) - throw new InvalidOperationException($"Unexpected exit code:{ExitCode}"); - + if (exitCode != 0) + throw new InvalidOperationException($"Unexpected exit code:{exitCode}"); + return exitCode; } @@ -1682,6 +1682,7 @@ public static T AsOrDefault(this object? @this, Func defaultValueFactory) /// Generic type parameter. /// this. /// A T. + [RequiresUnreferencedCode()] public static T To(this object? @this) { #nullable disable @@ -1719,6 +1720,7 @@ public static T To(this object? @this) /// this. /// The type. /// An object. + [RequiresUnreferencedCode()] public static object? To(this object? @this, Type type) { if (@this == null || @this == DBNull.Value) @@ -2614,14 +2616,14 @@ public static StringBuilder AppendLineIf(this StringBuilder builder, FuncThe @this to act on. /// The arguments. /// The new instance. - public static T? CreateInstance<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]T>([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]this Type @this, params object?[]? args) => (T?)Activator.CreateInstance(@this, args); + public static T? CreateInstance<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T>([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] this Type @this, params object?[]? args) => (T?)Activator.CreateInstance(@this, args); /// /// if a type has empty constructor /// /// type /// - public static bool HasEmptyConstructor([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]this Type type) + public static bool HasEmptyConstructor([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] this Type type) => Guard.NotNull(type, nameof(type)).GetConstructors(BindingFlags.Instance).Any(c => c.GetParameters().Length == 0); public static bool IsNullableType(this Type type) diff --git a/src/WeihanLi.Common/Extensions/DataExtension.cs b/src/WeihanLi.Common/Extensions/DataExtension.cs index e625cf73..730ffea0 100644 --- a/src/WeihanLi.Common/Extensions/DataExtension.cs +++ b/src/WeihanLi.Common/Extensions/DataExtension.cs @@ -45,7 +45,7 @@ IEnumerator IEnumerable.GetEnumerator() #region DataTable - public static DataTable ToDataTable<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)]T>(this IEnumerable entities) + public static DataTable ToDataTable<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T>(this IEnumerable entities) { if (null == entities) { @@ -212,6 +212,7 @@ public static dynamic ToExpandoObject(this DataRow @this) /// /// The @this to act on. /// @this as a DataTable. + [RequiresUnreferencedCode()] public static DataTable ToDataTable(this IDataReader @this) { var dt = new DataTable(); diff --git a/src/WeihanLi.Common/Extensions/ExpressionExtension.cs b/src/WeihanLi.Common/Extensions/ExpressionExtension.cs index a31c18d8..096c1b76 100644 --- a/src/WeihanLi.Common/Extensions/ExpressionExtension.cs +++ b/src/WeihanLi.Common/Extensions/ExpressionExtension.cs @@ -154,7 +154,7 @@ public static MemberInfo GetMemberInfo(this Expression /// /// - public static PropertyInfo GetProperty<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)]TEntity, TProperty>( + public static PropertyInfo GetProperty<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] TEntity, TProperty>( this Expression> expression) { var member = GetMemberInfo(expression); diff --git a/src/WeihanLi.Common/Extensions/ILGeneratorExtensions.cs b/src/WeihanLi.Common/Extensions/ILGeneratorExtensions.cs index 2330af48..e1a74cee 100644 --- a/src/WeihanLi.Common/Extensions/ILGeneratorExtensions.cs +++ b/src/WeihanLi.Common/Extensions/ILGeneratorExtensions.cs @@ -18,7 +18,7 @@ public static void EmitMethod(this ILGenerator ilGenerator, MethodInfo method) { Guard.NotNull(ilGenerator); Guard.NotNull(method); - + EmitMethod(ilGenerator, method, method.DeclaringType!); } @@ -38,7 +38,7 @@ public static void EmitMethod(this ILGenerator ilGenerator, MethodInfo method, T [RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")] [RequiresUnreferencedCode("Unreferenced code may be used")] - public static void EmitConvertToObject(this ILGenerator ilGenerator, + public static void EmitConvertToObject(this ILGenerator ilGenerator, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type typeFrom) { Guard.NotNull(ilGenerator); @@ -56,7 +56,7 @@ public static void EmitConvertToObject(this ILGenerator ilGenerator, [RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")] [RequiresUnreferencedCode("Unreferenced code may be used")] - public static void EmitConvertFromObject(this ILGenerator ilGenerator, + public static void EmitConvertFromObject(this ILGenerator ilGenerator, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.PublicConstructors)] Type typeTo) { Guard.NotNull(ilGenerator); @@ -71,12 +71,12 @@ public static void EmitConvertFromObject(this ILGenerator ilGenerator, ilGenerator.EmitConvertToType(typeof(object), typeTo); } } - + [RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")] [RequiresUnreferencedCode("Unreferenced code may be used")] public static void EmitConvertToType(this ILGenerator ilGenerator, - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]Type typeFrom, - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.PublicConstructors)]Type typeTo, + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type typeFrom, + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.PublicConstructors)] Type typeTo, bool isChecked = true) { Guard.NotNull(ilGenerator); @@ -125,9 +125,9 @@ public static void EmitConvertToType(this ILGenerator ilGenerator, [RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")] [RequiresUnreferencedCode("Unreferenced code may be used")] - private static void EmitNullableConversion(this ILGenerator ilGenerator, - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]Type typeFrom, - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.PublicConstructors)]Type typeTo, + private static void EmitNullableConversion(this ILGenerator ilGenerator, + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type typeFrom, + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.PublicConstructors)] Type typeTo, bool isChecked) { var isTypeFromNullable = typeFrom.IsNullableType(); @@ -142,9 +142,9 @@ private static void EmitNullableConversion(this ILGenerator ilGenerator, [RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")] [RequiresUnreferencedCode("Unreferenced code may be used")] - private static void EmitNullableToNullableConversion(this ILGenerator ilGenerator, - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]Type typeFrom, - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.PublicConstructors)]Type typeTo, + private static void EmitNullableToNullableConversion(this ILGenerator ilGenerator, + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type typeFrom, + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.PublicConstructors)] Type typeTo, bool isChecked) { var locFrom = ilGenerator.DeclareLocal(typeFrom); @@ -176,8 +176,8 @@ private static void EmitNullableToNullableConversion(this ILGenerator ilGenerato [RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")] [RequiresUnreferencedCode("Unreferenced code may be used")] - private static void EmitNullableToNonNullableConversion(this ILGenerator ilGenerator, - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]Type typeFrom, + private static void EmitNullableToNonNullableConversion(this ILGenerator ilGenerator, + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type typeFrom, Type typeTo, bool isChecked) { if (typeTo.IsValueType) @@ -188,8 +188,8 @@ private static void EmitNullableToNonNullableConversion(this ILGenerator ilGener [RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")] [RequiresUnreferencedCode("Unreferenced code may be used")] - private static void EmitNullableToNonNullableStructConversion(this ILGenerator ilGenerator, - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]Type typeFrom, + private static void EmitNullableToNonNullableStructConversion(this ILGenerator ilGenerator, + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type typeFrom, Type typeTo, bool isChecked) { var locFrom = ilGenerator.DeclareLocal(typeFrom); @@ -209,9 +209,9 @@ private static void EmitNullableToReferenceConversion(this ILGenerator ilGenerat [RequiresDynamicCode("Defining a dynamic assembly requires dynamic code.")] [RequiresUnreferencedCode("Unreferenced code may be used")] - private static void EmitNonNullableToNullableConversion(this ILGenerator ilGenerator, - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]Type typeFrom, - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.PublicConstructors)]Type typeTo, + private static void EmitNonNullableToNullableConversion(this ILGenerator ilGenerator, + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type typeFrom, + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.PublicConstructors)] Type typeTo, bool isChecked) { var locTo = ilGenerator.DeclareLocal(typeTo); @@ -389,7 +389,7 @@ private static void EmitNumericConversion(this ILGenerator ilGenerator, Type typ public static void EmitCastToType(this ILGenerator ilGenerator, Type typeFrom, Type typeTo) { Guard.NotNull(ilGenerator); - + if (!typeFrom.IsValueType && typeTo.IsValueType) { ilGenerator.Emit(OpCodes.Unbox_Any, typeTo); @@ -534,21 +534,21 @@ public static void EmitBoolean(this ILGenerator ilGenerator, bool value) } } - public static void EmitHasValue(this ILGenerator ilGenerator, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]Type nullableType) + public static void EmitHasValue(this ILGenerator ilGenerator, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type nullableType) { Guard.NotNull(ilGenerator); - + var mi = nullableType.GetTypeInfo().GetMethod("get_HasValue", BindingFlags.Instance | BindingFlags.Public)!; ilGenerator.Emit(OpCodes.Call, mi); } - public static void EmitGetValueOrDefault(this ILGenerator ilGenerator, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]Type nullableType) + public static void EmitGetValueOrDefault(this ILGenerator ilGenerator, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type nullableType) { var mi = nullableType.GetTypeInfo().GetMethod("GetValueOrDefault", Type.EmptyTypes)!; ilGenerator.Emit(OpCodes.Call, mi); } - public static void EmitGetValue(this ILGenerator ilGenerator, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]Type nullableType) + public static void EmitGetValue(this ILGenerator ilGenerator, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)] Type nullableType) { var mi = nullableType.GetTypeInfo().GetMethod("get_Value", BindingFlags.Instance | BindingFlags.Public)!; ilGenerator.Emit(OpCodes.Call, mi); @@ -558,7 +558,7 @@ public static void EmitDefaultValue(this ILGenerator ilGenerator, Type type) { Guard.NotNull(ilGenerator); Guard.NotNull(type); - + switch (Type.GetTypeCode(type)) { case TypeCode.Object: diff --git a/src/WeihanLi.Common/Extensions/ProcessExtension.cs b/src/WeihanLi.Common/Extensions/ProcessExtension.cs index cfd0d1e0..5f9f1695 100644 --- a/src/WeihanLi.Common/Extensions/ProcessExtension.cs +++ b/src/WeihanLi.Common/Extensions/ProcessExtension.cs @@ -136,7 +136,7 @@ public static int GetExitCode(this ProcessStartInfo psi, TextWriter? stdOut = nu if (e.Data != null) stdErr?.WriteLine(e.Data); }; - + try { process.Start(); diff --git a/src/WeihanLi.Common/Extensions/ReflectionExtension.cs b/src/WeihanLi.Common/Extensions/ReflectionExtension.cs index 3e5c97fe..e3fc119e 100644 --- a/src/WeihanLi.Common/Extensions/ReflectionExtension.cs +++ b/src/WeihanLi.Common/Extensions/ReflectionExtension.cs @@ -11,8 +11,8 @@ namespace WeihanLi.Extensions; public static class ReflectionExtension -{ - public static MethodInfo? GetMethodBySignature([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]this Type type, MethodInfo method) +{ + public static MethodInfo? GetMethodBySignature([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)] this Type type, MethodInfo method) { Guard.NotNull(type); Guard.NotNull(method); diff --git a/src/WeihanLi.Common/Extensions/ServiceCollectionExtension.cs b/src/WeihanLi.Common/Extensions/ServiceCollectionExtension.cs index 2bb09647..1d521351 100644 --- a/src/WeihanLi.Common/Extensions/ServiceCollectionExtension.cs +++ b/src/WeihanLi.Common/Extensions/ServiceCollectionExtension.cs @@ -182,8 +182,8 @@ public static IServiceCollection RegisterAssemblyTypesAsImplementedInterfaces(th /// type /// service lifetime /// services - public static IServiceCollection RegisterTypeAsImplementedInterfaces(this IServiceCollection services, - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces | DynamicallyAccessedMemberTypes.PublicConstructors)]Type type, + public static IServiceCollection RegisterTypeAsImplementedInterfaces(this IServiceCollection services, + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces | DynamicallyAccessedMemberTypes.PublicConstructors)] Type type, ServiceLifetime serviceLifetime = ServiceLifetime.Singleton) => RegisterTypeAsImplementedInterfaces(services, type, null, serviceLifetime); @@ -195,8 +195,8 @@ public static IServiceCollection RegisterTypeAsImplementedInterfaces(this IServi /// interfaceTypeFilter /// service lifetime /// services - public static IServiceCollection RegisterTypeAsImplementedInterfaces(this IServiceCollection services, - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces | DynamicallyAccessedMemberTypes.PublicConstructors)]Type type, + public static IServiceCollection RegisterTypeAsImplementedInterfaces(this IServiceCollection services, + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces | DynamicallyAccessedMemberTypes.PublicConstructors)] Type type, Func? interfaceTypeFilter, ServiceLifetime serviceLifetime = ServiceLifetime.Singleton) { Guard.NotNull(type); @@ -266,7 +266,7 @@ public static IServiceCollection RegisterAssemblyModules( /// decorator type /// services /// services - public static IServiceCollection Decorate(this IServiceCollection services) + public static IServiceCollection Decorate(this IServiceCollection services) where TService : class where TDecorator : class, TService { @@ -281,8 +281,8 @@ public static IServiceCollection RegisterAssemblyModules( /// decoratorType /// services /// throw exception when serviceType not registered - public static IServiceCollection Decorate(this IServiceCollection services, Type serviceType, - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]Type decoratorType) + public static IServiceCollection Decorate(this IServiceCollection services, Type serviceType, + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] Type decoratorType) { var service = services.LastOrDefault(x => x.ServiceType == serviceType); if (service == null) diff --git a/src/WeihanLi.Common/Extensions/TypeExtension.cs b/src/WeihanLi.Common/Extensions/TypeExtension.cs index 18df5ff8..165a2b28 100644 --- a/src/WeihanLi.Common/Extensions/TypeExtension.cs +++ b/src/WeihanLi.Common/Extensions/TypeExtension.cs @@ -82,7 +82,8 @@ public static bool IsBasicType(this Type type) /// type /// /// Matching constructor or default one - public static ConstructorInfo? GetConstructor([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]this Type type, params Type[]? parameterTypes) + [RequiresUnreferencedCode()] + public static ConstructorInfo? GetConstructor([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] this Type type, params Type[]? parameterTypes) { if (parameterTypes == null || parameterTypes.Length == 0) return GetEmptyConstructor(type); @@ -91,7 +92,7 @@ public static bool IsBasicType(this Type type) return ctor; } - public static ConstructorInfo? GetEmptyConstructor([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]this Type type) + public static ConstructorInfo? GetEmptyConstructor([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] this Type type) { var constructors = type.GetConstructors(); @@ -120,7 +121,7 @@ public static bool IsAssignableTo(this Type @this) /// The type being tested. /// The types of the contractor to find. /// The is a match is found; otherwise, null. - public static ConstructorInfo? GetMatchingConstructor([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]this Type type, Type[]? constructorParameterTypes) + public static ConstructorInfo? GetMatchingConstructor([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] this Type type, Type[]? constructorParameterTypes) { if (constructorParameterTypes == null || constructorParameterTypes.Length == 0) return GetEmptyConstructor(type); @@ -137,7 +138,7 @@ public static bool IsAssignableTo(this Type @this) /// /// type /// - public static IEnumerable GetImplementedInterfaces([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)]this Type type) + public static IEnumerable GetImplementedInterfaces([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Interfaces)] this Type type) { return type.GetTypeInfo().ImplementedInterfaces; } diff --git a/src/WeihanLi.Common/Helpers/ActivatorHelper.cs b/src/WeihanLi.Common/Helpers/ActivatorHelper.cs index cb5ff17d..3dab28db 100644 --- a/src/WeihanLi.Common/Helpers/ActivatorHelper.cs +++ b/src/WeihanLi.Common/Helpers/ActivatorHelper.cs @@ -13,7 +13,7 @@ namespace WeihanLi.Common.Helpers; internal static class ParameterDefaultValue { private static readonly Type NullableOpenGenericType = typeof(Nullable<>); - + [RequiresUnreferencedCode("Unreferenced code may be used")] public static bool TryGetDefaultValue(ParameterInfo parameter, out object? defaultValue) { @@ -79,7 +79,7 @@ public static class ActivatorHelper /// type /// parameters /// T instance - public static T CreateInstance<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]T>(params object?[] parameters) + public static T CreateInstance<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T>(params object?[] parameters) { return (T)(Activator.CreateInstance(typeof(T), parameters) ?? throw new InvalidOperationException()); } @@ -92,8 +92,8 @@ public static class ActivatorHelper /// Constructor arguments not provided by the . /// An activated object of type instanceType [RequiresUnreferencedCode("Unreferenced code may be used")] - public static object CreateInstance(this IServiceProvider provider, - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]Type instanceType, + public static object CreateInstance(this IServiceProvider provider, + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type instanceType, params object?[] parameters) { return MatchConstructor(instanceType, parameters).CreateInstance(provider); @@ -151,13 +151,13 @@ private static ConstructorMatcher MatchConstructor( /// Constructor arguments not provided by di sys /// Best Constructor Matched [RequiresUnreferencedCode("Unreferenced code may be used")] - public static ConstructorInfo MatchBestConstructor([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]Type instanceType, params object[] parameters) + public static ConstructorInfo MatchBestConstructor([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type instanceType, params object[] parameters) { return MatchConstructor(instanceType, parameters).Constructor; } [RequiresUnreferencedCode("Unreferenced code may be used")] - public static object?[] GetBestConstructorArguments(IServiceProvider serviceProvider, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]Type instanceType, params object?[] parameters) + public static object?[] GetBestConstructorArguments(IServiceProvider serviceProvider, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type instanceType, params object?[] parameters) { return MatchConstructor(instanceType, parameters).GetConstructorArguments(serviceProvider); } @@ -198,7 +198,7 @@ public static ObjectFactory CreateFactory(Type instanceType, Type[] argumentType /// Constructor arguments not provided by the . /// An activated object of type T [RequiresUnreferencedCode("Unreferenced code may be used")] - public static T CreateInstance<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]T>(this IServiceProvider provider, params object[] parameters) + public static T CreateInstance<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] T>(this IServiceProvider provider, params object[] parameters) { return (T)CreateInstance(provider, typeof(T), parameters); } @@ -210,7 +210,7 @@ public static ObjectFactory CreateFactory(Type instanceType, Type[] argumentType /// The service provider used to resolve dependencies /// The resolved service or created instance [RequiresUnreferencedCode("Unreferenced code may be used")] - public static T GetServiceOrCreateInstance<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]T>(this IServiceProvider provider) + public static T GetServiceOrCreateInstance<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] T>(this IServiceProvider provider) { return (T)GetServiceOrCreateInstance(provider, typeof(T)); } @@ -222,7 +222,7 @@ public static ObjectFactory CreateFactory(Type instanceType, Type[] argumentType /// The type of the service /// The resolved service or created instance [RequiresUnreferencedCode("Unreferenced code may be used")] - public static object GetServiceOrCreateInstance(this IServiceProvider provider, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]Type type) + public static object GetServiceOrCreateInstance(this IServiceProvider provider, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type type) { return provider.GetService(type) ?? CreateInstance(provider, type); } @@ -307,7 +307,7 @@ public static void FindApplicableConstructor( // Tries to find constructor based on provided argument types [RequiresUnreferencedCode("Unreferenced code may be used")] private static bool TryFindMatchingConstructor( - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]Type instanceType, + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)] Type instanceType, Type[] argumentTypes, ref ConstructorInfo? matchingConstructor, ref int?[]? parameterMap) diff --git a/src/WeihanLi.Common/Helpers/ApplicationHelper.cs b/src/WeihanLi.Common/Helpers/ApplicationHelper.cs index bf3fab5e..587cf52c 100644 --- a/src/WeihanLi.Common/Helpers/ApplicationHelper.cs +++ b/src/WeihanLi.Common/Helpers/ApplicationHelper.cs @@ -94,7 +94,7 @@ public static string GetDotnetDirectory() public static string? ResolvePath(string execName) { var executableName = execName; - if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && !Path.HasExtension(execName)) { executableName += ".exe"; diff --git a/src/WeihanLi.Common/Helpers/BoundedConcurrentQueue.cs b/src/WeihanLi.Common/Helpers/BoundedConcurrentQueue.cs index b0988855..1eff5ab9 100644 --- a/src/WeihanLi.Common/Helpers/BoundedConcurrentQueue.cs +++ b/src/WeihanLi.Common/Helpers/BoundedConcurrentQueue.cs @@ -28,13 +28,13 @@ public BoundedConcurrentQueue(int queueLimit, BoundedQueueFullMode mode = Bounde public int Count => _queue.Count; - public bool TryDequeue([MaybeNullWhen(false)]out T item) + public bool TryDequeue([MaybeNullWhen(false)] out T item) { if (_queueLimit == NonBounded) return _queue.TryDequeue(out item); var result = false; - + if (_queue.TryDequeue(out item)) { result = true; @@ -53,7 +53,7 @@ public bool TryEnqueue(T item) } var result = true; - + if (Interlocked.Increment(ref _counter) <= _queueLimit) { _queue.Enqueue(item); @@ -72,7 +72,7 @@ public bool TryEnqueue(T item) else { Interlocked.Decrement(ref _counter); - result = false; + result = false; } } diff --git a/src/WeihanLi.Common/Helpers/CommandExecutor.cs b/src/WeihanLi.Common/Helpers/CommandExecutor.cs index 63e80ba4..d23340a7 100644 --- a/src/WeihanLi.Common/Helpers/CommandExecutor.cs +++ b/src/WeihanLi.Common/Helpers/CommandExecutor.cs @@ -9,7 +9,7 @@ namespace WeihanLi.Common.Helpers; public static class CommandExecutor { private static readonly char[] SpaceSeparator = [' ']; - + /// /// Execute command /// @@ -23,7 +23,7 @@ public static int ExecuteCommand(string command, string? workingDirectory = null var cmd = command.Split(SpaceSeparator, 2); return Execute(cmd[0], cmd.Length > 1 ? cmd[1] : null, workingDirectory, configure); } - + /// /// Execute command and capture output /// @@ -37,7 +37,7 @@ public static CommandResult ExecuteCommandAndCapture(string command, string? wor var cmd = command.Split(SpaceSeparator, 2); return ExecuteAndCapture(cmd[0], cmd.Length > 1 ? cmd[1] : null, workingDirectory, configure); } - + /// /// Execute command with a process /// @@ -47,15 +47,15 @@ public static CommandResult ExecuteCommandAndCapture(string command, string? wor /// working directory /// configure the ProcessStartInfo /// exit code - public static int ExecuteCommandAndOutput(string command, - TextWriter? stdout = null, TextWriter? stderr = null, + public static int ExecuteCommandAndOutput(string command, + TextWriter? stdout = null, TextWriter? stderr = null, string? workingDirectory = null, Action? configure = null) { Guard.NotNullOrEmpty(command); var cmd = command.Split(SpaceSeparator, 2); return ExecuteAndOutput(cmd[0], cmd.Length > 1 ? cmd[1] : null, stdout, stderr, workingDirectory, configure); } - + /// /// Execute command async /// @@ -70,7 +70,7 @@ public static Task ExecuteCommandAsync(string command, string? workingDirec var cmd = command.Split(SpaceSeparator, 2); return ExecuteAsync(cmd[0], cmd.Length > 1 ? cmd[1] : null, workingDirectory, configure, cancellationToken); } - + /// /// Execute command and capture output async /// @@ -105,7 +105,7 @@ public static Task ExecuteCommandAndOutputAsync(string command, string? arg var cmd = command.Split(SpaceSeparator, 2); return ExecuteAndOutputAsync(cmd[0], cmd.Length > 1 ? cmd[1] : null, stdout, stderr, workingDirectory, configure, cancellationToken); } - + /// /// Execute command with a process /// @@ -146,7 +146,7 @@ public static async Task ExecuteAsync(string commandPath, string? arguments configure?.Invoke(processStartInfo); return await processStartInfo.ExecuteAsync(cancellationToken); } - + /// /// Execute command with a process /// @@ -157,8 +157,8 @@ public static async Task ExecuteAsync(string commandPath, string? arguments /// working directory /// configure the ProcessStartInfo /// exit code - public static int ExecuteAndOutput(string commandPath, string? arguments = null, - TextWriter? stdout = null, TextWriter? stderr = null, + public static int ExecuteAndOutput(string commandPath, string? arguments = null, + TextWriter? stdout = null, TextWriter? stderr = null, string? workingDirectory = null, Action? configure = null) { var processStartInfo = new ProcessStartInfo(commandPath, arguments ?? string.Empty) @@ -170,7 +170,7 @@ public static int ExecuteAndOutput(string commandPath, string? arguments = null, configure?.Invoke(processStartInfo); return processStartInfo.GetExitCode(stdout ?? Console.Out, stderr ?? Console.Error); } - + /// /// Execute command with a process /// @@ -193,7 +193,7 @@ public static async Task ExecuteAndOutputAsync(string commandPath, string? WorkingDirectory = workingDirectory ?? Environment.CurrentDirectory }; configure?.Invoke(processStartInfo); - return await processStartInfo.GetExitCodeAsync(stdout ?? Console.Out, stderr ?? Console.Error,cancellationToken); + return await processStartInfo.GetExitCodeAsync(stdout ?? Console.Out, stderr ?? Console.Error, cancellationToken); } /// @@ -267,7 +267,7 @@ public sealed class CommandResult(int exitCode, string standardOut, string stand [Obsolete("Please use EnsureSuccessExitCode() instead", true)] public CommandResult EnsureSuccessfulExitCode(int successCode = 0) => EnsureSuccessExitCode(successCode); - + public CommandResult EnsureSuccessExitCode(int successCode = 0) { ExitCode.EnsureSuccessExitCode(successCode); diff --git a/src/WeihanLi.Common/Helpers/ConsoleHelper.cs b/src/WeihanLi.Common/Helpers/ConsoleHelper.cs index 879e77a0..5800715c 100644 --- a/src/WeihanLi.Common/Helpers/ConsoleHelper.cs +++ b/src/WeihanLi.Common/Helpers/ConsoleHelper.cs @@ -176,7 +176,7 @@ public static void WriteIf(string? output, bool condition) { if (condition) Console.Write(output); } - + public static void ErrorWriteLineIf(string? output, bool condition) { if (condition) Console.Error.WriteLine(output); @@ -190,9 +190,9 @@ public static void ErrorWriteIf(string? output, bool condition) public static CommandResult PrintOutputToConsole(this CommandResult commandResult) { Guard.NotNull(commandResult); - + Console.WriteLine(commandResult.StandardOut); - + if (!string.IsNullOrEmpty(commandResult.StandardError)) { Console.WriteLine(); diff --git a/src/WeihanLi.Common/Helpers/DataSerializer.cs b/src/WeihanLi.Common/Helpers/DataSerializer.cs index 0b6be0b3..8db1519f 100644 --- a/src/WeihanLi.Common/Helpers/DataSerializer.cs +++ b/src/WeihanLi.Common/Helpers/DataSerializer.cs @@ -29,7 +29,7 @@ public interface IDataSerializer public class XmlDataSerializer : IDataSerializer { internal static readonly Lazy Instance = new(); - + [RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")] public virtual byte[] Serialize(T obj) { @@ -46,7 +46,7 @@ public virtual byte[] Serialize(T obj) serializer.Serialize(ms, obj); return ms.ToArray(); } - + [RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")] public virtual T Deserialize(byte[] bytes) { @@ -69,11 +69,11 @@ public virtual byte[] Serialize(T obj) { if (typeof(Task).IsAssignableFrom(typeof(T))) { - throw new ArgumentException(Resource.TaskCanNotBeSerialized); + throw new ArgumentException(Resource.TaskCanNotBeSerialized); } return obj.ToJson().GetBytes(); } - + [RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")] public virtual T Deserialize(byte[] bytes) { diff --git a/src/WeihanLi.Common/Helpers/DiagnosticHelper.cs b/src/WeihanLi.Common/Helpers/DiagnosticHelper.cs index c1165ec9..bce2d685 100644 --- a/src/WeihanLi.Common/Helpers/DiagnosticHelper.cs +++ b/src/WeihanLi.Common/Helpers/DiagnosticHelper.cs @@ -9,12 +9,12 @@ namespace WeihanLi.Common.Helpers; public static class DiagnosticHelper { private const string DiagnosticSourceName = "WeihanLi.Common"; - + [CLSCompliant(false)] public static readonly ActivitySource ActivitySource; [CLSCompliant(false)] public static readonly Meter Meter; - + static DiagnosticHelper() { var version = typeof(DiagnosticHelper).Assembly.GetName().Version?.ToString(3); diff --git a/src/WeihanLi.Common/Helpers/EnumHelper.cs b/src/WeihanLi.Common/Helpers/EnumHelper.cs index 4dd95e31..406589d1 100644 --- a/src/WeihanLi.Common/Helpers/EnumHelper.cs +++ b/src/WeihanLi.Common/Helpers/EnumHelper.cs @@ -15,8 +15,8 @@ public static IReadOnlyList ToIdNameList() where TEnum : Enu Id = Convert.ToInt32(Enum.Parse(enumType, name)) }); } - - public static IReadOnlyList> ToIdNameList<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)]TEnum, TValue>() where TEnum : Enum + + public static IReadOnlyList> ToIdNameList<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] TEnum, TValue>() where TEnum : Enum { var enumType = typeof(TEnum); return Array.ConvertAll(Enum.GetNames(enumType), name => new IdNameModel() @@ -25,8 +25,8 @@ public static IReadOnlyList ToIdNameList() where TEnum : Enu Name = name, }); } - - public static IReadOnlyList ToIdNameDescList<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)]TEnum>() where TEnum : Enum + + public static IReadOnlyList ToIdNameDescList<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] TEnum>() where TEnum : Enum { var enumType = typeof(TEnum); return Array.ConvertAll(Enum.GetNames(enumType), converter: name => new IdNameDescModel() @@ -36,8 +36,8 @@ public static IReadOnlyList ToIdNameList() where TEnum : Enu Description = enumType.GetField(name)?.GetDescription() }); } - - public static IReadOnlyList> ToIdNameDescList<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)]TEnum, TValue>() where TEnum : Enum + + public static IReadOnlyList> ToIdNameDescList<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)] TEnum, TValue>() where TEnum : Enum { var enumType = typeof(TEnum); return Array.ConvertAll(Enum.GetNames(enumType), converter: name => new IdNameDescModel() diff --git a/src/WeihanLi.Common/Helpers/Hosting/AppHostBuilderExtensions.cs b/src/WeihanLi.Common/Helpers/Hosting/AppHostBuilderExtensions.cs index 0f504b66..f14e1b5a 100644 --- a/src/WeihanLi.Common/Helpers/Hosting/AppHostBuilderExtensions.cs +++ b/src/WeihanLi.Common/Helpers/Hosting/AppHostBuilderExtensions.cs @@ -10,7 +10,7 @@ namespace WeihanLi.Common.Helpers.Hosting; public static class AppHostBuilderExtensions { - public static IAppHostBuilder AddHostedService<[DynamicallyAccessedMembers((DynamicallyAccessedMemberTypes.PublicConstructors))]TService>(this IAppHostBuilder appHostBuilder) + public static IAppHostBuilder AddHostedService<[DynamicallyAccessedMembers((DynamicallyAccessedMemberTypes.PublicConstructors))] TService>(this IAppHostBuilder appHostBuilder) where TService : class, IHostedService { Guard.NotNull(appHostBuilder); diff --git a/src/WeihanLi.Common/Helpers/MapHelper.cs b/src/WeihanLi.Common/Helpers/MapHelper.cs index 761c1ad3..aa4a6962 100644 --- a/src/WeihanLi.Common/Helpers/MapHelper.cs +++ b/src/WeihanLi.Common/Helpers/MapHelper.cs @@ -5,7 +5,7 @@ namespace WeihanLi.Common.Helpers; public static class MapHelper { - public static TTarget Map<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)]TSource, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)]TTarget>(TSource source) where TTarget : new() + public static TTarget Map<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] TSource, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] TTarget>(TSource source) where TTarget : new() { if (source is null) { @@ -41,7 +41,7 @@ public static class MapHelper return result; } - public static TTarget MapWith<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)]TSource, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)]TTarget>(TSource source, params string[] propertiesToMap) where TTarget : new() + public static TTarget MapWith<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] TSource, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] TTarget>(TSource source, params string[] propertiesToMap) where TTarget : new() { if (source is null) { @@ -78,7 +78,7 @@ public static class MapHelper return result; } - public static TTarget MapWithout<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)]TSource, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)]TTarget>(TSource source, params string[] propertiesNoMap) where TTarget : new() + public static TTarget MapWithout<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] TSource, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] TTarget>(TSource source, params string[] propertiesNoMap) where TTarget : new() { if (source is null) { diff --git a/src/WeihanLi.Common/Helpers/PipelineBuilder.cs b/src/WeihanLi.Common/Helpers/PipelineBuilder.cs index dd62f3ac..60cc8b90 100644 --- a/src/WeihanLi.Common/Helpers/PipelineBuilder.cs +++ b/src/WeihanLi.Common/Helpers/PipelineBuilder.cs @@ -27,7 +27,7 @@ public static IAsyncPipelineBuilder CreateAsync(Func CreateValueAsync() { - return new ValueAsyncPipelineBuilder(c => + return new ValueAsyncPipelineBuilder(c => #if NET6_0_OR_GREATER ValueTask.CompletedTask #else diff --git a/src/WeihanLi.Common/Helpers/Pipelines/PipelineBuilderExtensions.cs b/src/WeihanLi.Common/Helpers/Pipelines/PipelineBuilderExtensions.cs index 680246e5..bf7be0fc 100644 --- a/src/WeihanLi.Common/Helpers/Pipelines/PipelineBuilderExtensions.cs +++ b/src/WeihanLi.Common/Helpers/Pipelines/PipelineBuilderExtensions.cs @@ -191,7 +191,7 @@ public static IValueAsyncPipelineBuilder Use(this IValueAsyn return builder.Use(next => context => func(context, next)); } - + public static IValueAsyncPipelineBuilder UseMiddleware(this IValueAsyncPipelineBuilder builder, IValueAsyncPipelineMiddleware middleware) { Guard.NotNull(middleware); @@ -205,7 +205,7 @@ public static IValueAsyncPipelineBuilder UseMiddleware(this [RequiresUnreferencedCode("Unreferenced code may be used")] public static IValueAsyncPipelineBuilder UseMiddleware(this IValueAsyncPipelineBuilder builder) - where TMiddleware : class, IValueAsyncPipelineMiddleware + where TMiddleware : class, IValueAsyncPipelineMiddleware { return builder.UseMiddleware(DependencyResolver.Current.GetServiceOrCreateInstance()); } diff --git a/src/WeihanLi.Common/Helpers/ReflectHelper.cs b/src/WeihanLi.Common/Helpers/ReflectHelper.cs index 2487e4a9..4e0c0bf6 100644 --- a/src/WeihanLi.Common/Helpers/ReflectHelper.cs +++ b/src/WeihanLi.Common/Helpers/ReflectHelper.cs @@ -51,7 +51,7 @@ public AwaitableInfo( } [RequiresUnreferencedCode("Members from serialized types may be trimmed if not referenced directly")] - public static bool IsTypeAwaitable([DynamicallyAccessedMembers((DynamicallyAccessedMemberTypes.PublicMethods))]Type type, out AwaitableInfo? awaitableInfo) + public static bool IsTypeAwaitable([DynamicallyAccessedMembers((DynamicallyAccessedMemberTypes.PublicMethods))] Type type, out AwaitableInfo? awaitableInfo) { // Based on Roslyn code: http://source.roslyn.io/#Microsoft.CodeAnalysis.Workspaces/Shared/Extensions/ISymbolExtensions.cs,db4d48ba694b9347 diff --git a/src/WeihanLi.Common/Helpers/TaskHelper.cs b/src/WeihanLi.Common/Helpers/TaskHelper.cs index ed2bbddc..2ae2893b 100644 --- a/src/WeihanLi.Common/Helpers/TaskHelper.cs +++ b/src/WeihanLi.Common/Helpers/TaskHelper.cs @@ -4,14 +4,14 @@ namespace WeihanLi.Common.Helpers; public static class TaskHelper -{ +{ public static ValueTask ToTask(object? obj) { - var task = obj switch + var task = obj switch { ValueTask vt => vt, Task t => new ValueTask(t), - _ => + _ => #if NET6_0_OR_GREATER ValueTask.CompletedTask #else diff --git a/src/WeihanLi.Common/Http/MockHttpHandler.cs b/src/WeihanLi.Common/Http/MockHttpHandler.cs index ffb573ec..3ae744e8 100644 --- a/src/WeihanLi.Common/Http/MockHttpHandler.cs +++ b/src/WeihanLi.Common/Http/MockHttpHandler.cs @@ -42,7 +42,7 @@ protected override Task SendAsync(HttpRequestMessage reques { return _responseFactory(request); } - + public HttpClient GetHttpClient() => new(this) { BaseAddress = new Uri("http://localhost/") diff --git a/src/WeihanLi.Common/Logging/LogHelperExtensions.cs b/src/WeihanLi.Common/Logging/LogHelperExtensions.cs index 7514f3ba..647a76f5 100644 --- a/src/WeihanLi.Common/Logging/LogHelperExtensions.cs +++ b/src/WeihanLi.Common/Logging/LogHelperExtensions.cs @@ -165,7 +165,7 @@ public static ILogHelperLoggingBuilder WithProvider(this ILogHelperLoggingBuilde return loggingBuilder; } - public static ILogHelperLoggingBuilder WithProvider<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]TLogProvider>(this ILogHelperLoggingBuilder loggingBuilder, params object[] ctorParams) where TLogProvider : ILogHelperProvider + public static ILogHelperLoggingBuilder WithProvider<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TLogProvider>(this ILogHelperLoggingBuilder loggingBuilder, params object[] ctorParams) where TLogProvider : ILogHelperProvider { Guard.NotNull(loggingBuilder, nameof(loggingBuilder)); @@ -173,7 +173,7 @@ public static ILogHelperLoggingBuilder WithProvider(this ILogHelperLoggingBuilde return loggingBuilder; } - public static ILogHelperLoggingBuilder WithEnricher<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]TEnricher>(this ILogHelperLoggingBuilder loggingBuilder, + public static ILogHelperLoggingBuilder WithEnricher<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TEnricher>(this ILogHelperLoggingBuilder loggingBuilder, TEnricher enricher) where TEnricher : ILogHelperLoggingEnricher { Guard.NotNull(loggingBuilder, nameof(loggingBuilder)); @@ -190,7 +190,7 @@ public static ILogHelperLoggingBuilder WithProvider(this ILogHelperLoggingBuilde return loggingBuilder; } - public static ILogHelperLoggingBuilder WithEnricher<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]TEnricher>(this ILogHelperLoggingBuilder loggingBuilder, params object[] ctorParams) where TEnricher : ILogHelperLoggingEnricher + public static ILogHelperLoggingBuilder WithEnricher<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] TEnricher>(this ILogHelperLoggingBuilder loggingBuilder, params object[] ctorParams) where TEnricher : ILogHelperLoggingEnricher { loggingBuilder.AddEnricher(ActivatorHelper.CreateInstance(ctorParams)); return loggingBuilder; diff --git a/src/WeihanLi.Common/Templating/ConfigurationRenderMiddleare.cs b/src/WeihanLi.Common/Templating/ConfigurationRenderMiddleare.cs index c39561fc..06a2f3e4 100644 --- a/src/WeihanLi.Common/Templating/ConfigurationRenderMiddleare.cs +++ b/src/WeihanLi.Common/Templating/ConfigurationRenderMiddleare.cs @@ -17,7 +17,7 @@ public Task InvokeAsync(TemplateRenderContext context, Func next) { if (context.Text.IsNullOrWhiteSpace()) return; - + await next(context); - + context.RenderedText = context.Text; foreach (var parameter in context.Parameters) { diff --git a/src/WeihanLi.Common/Templating/DependencyInjectionExtensions.cs b/src/WeihanLi.Common/Templating/DependencyInjectionExtensions.cs index 3aede718..4dd952a7 100644 --- a/src/WeihanLi.Common/Templating/DependencyInjectionExtensions.cs +++ b/src/WeihanLi.Common/Templating/DependencyInjectionExtensions.cs @@ -16,14 +16,14 @@ public static IServiceCollection AddTemplating(this IServiceCollection services, Guard.NotNull(services); if (services.Any(x => x.ServiceType == typeof(ITemplateEngine))) throw new InvalidOperationException("Templating services had been registered"); - + if (optionsConfigure != null) services.AddOptions().Configure(optionsConfigure); services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); - + services.TryAddEnumerable(ServiceDescriptor.Singleton()); services.TryAddEnumerable(ServiceDescriptor.Singleton()); services.AddSingleton(sp => @@ -32,7 +32,7 @@ public static IServiceCollection AddTemplating(this IServiceCollection services, ?? sp.GetService(); return new ConfigurationRenderMiddleware(configuration); }); - + services.TryAddSingleton(sp => { var pipelineBuilder = PipelineBuilder.CreateAsync(); @@ -42,7 +42,7 @@ public static IServiceCollection AddTemplating(this IServiceCollection services, } return pipelineBuilder.Build(); }); - + return services; } } diff --git a/src/WeihanLi.Common/Templating/TemplateEngine.cs b/src/WeihanLi.Common/Templating/TemplateEngine.cs index e99efd33..ec49cb2d 100644 --- a/src/WeihanLi.Common/Templating/TemplateEngine.cs +++ b/src/WeihanLi.Common/Templating/TemplateEngine.cs @@ -23,7 +23,7 @@ public async Task RenderAsync(string text, object? parameters = null) var result = await _templateRenderer.RenderAsync(context, parameters); return result; } - + public static TemplateEngine CreateDefault(Action? builderConfigure = null) { var builder = new TemplateEngineBuilder(); diff --git a/src/WeihanLi.Common/Templating/TemplateRendererBuilder.cs b/src/WeihanLi.Common/Templating/TemplateRendererBuilder.cs index 3703bed8..ca3e403e 100644 --- a/src/WeihanLi.Common/Templating/TemplateRendererBuilder.cs +++ b/src/WeihanLi.Common/Templating/TemplateRendererBuilder.cs @@ -10,8 +10,8 @@ internal sealed class TemplateEngineBuilder : ITemplateEngineBuilder private readonly IAsyncPipelineBuilder _pipelineBuilder = PipelineBuilder.CreateAsync(); private Action? _optionsConfigure; - - public ITemplateRendererBuilder UseRenderMiddleware(TMiddleware middleware) where TMiddleware: class, IRenderMiddleware + + public ITemplateRendererBuilder UseRenderMiddleware(TMiddleware middleware) where TMiddleware : class, IRenderMiddleware { _pipelineBuilder.UseMiddleware(Guard.NotNull(middleware)); return this; diff --git a/src/WeihanLi.Extensions.Hosting/HostingExtensions.cs b/src/WeihanLi.Extensions.Hosting/HostingExtensions.cs index 791f2b58..2d1ab21f 100644 --- a/src/WeihanLi.Extensions.Hosting/HostingExtensions.cs +++ b/src/WeihanLi.Extensions.Hosting/HostingExtensions.cs @@ -14,7 +14,7 @@ public static IHostApplicationBuilder ConfigureHostOptions(this IHostApplication ArgumentNullException.ThrowIfNull(optionsConfigure); hostApplicationBuilder.Services.Configure(optionsConfigure); - + return hostApplicationBuilder; } } diff --git a/src/WeihanLi.Extensions.Hosting/TimerBaseBackgroundService.cs b/src/WeihanLi.Extensions.Hosting/TimerBaseBackgroundService.cs index ae12e75a..fc8ef4ef 100644 --- a/src/WeihanLi.Extensions.Hosting/TimerBaseBackgroundService.cs +++ b/src/WeihanLi.Extensions.Hosting/TimerBaseBackgroundService.cs @@ -12,7 +12,7 @@ public abstract class TimerBaseBackgroundService : BackgroundService, IMyHostedS { protected abstract TimeSpan Period { get; } protected abstract Task ExecuteTaskAsync(CancellationToken stoppingToken); - + protected override async Task ExecuteAsync(CancellationToken stoppingToken) { using var timer = new PeriodicTimer(Period); diff --git a/test/WeihanLi.Common.Test/ExtensionsTest/ConfigurationExtensionTest.cs b/test/WeihanLi.Common.Test/ExtensionsTest/ConfigurationExtensionTest.cs index 2a864dbe..f0067a8f 100644 --- a/test/WeihanLi.Common.Test/ExtensionsTest/ConfigurationExtensionTest.cs +++ b/test/WeihanLi.Common.Test/ExtensionsTest/ConfigurationExtensionTest.cs @@ -10,7 +10,7 @@ public class ConfigurationExtensionTest [InlineData("false")] [InlineData("false-value")] [InlineData(null)] - public void FeatureFlagTest(string value) + public void FeatureFlagTest(string? value) { var featureName = "TestFeature"; var configuration = new ConfigurationBuilder() diff --git a/test/WeihanLi.Common.Test/ExtensionsTest/StringExtensionTest.cs b/test/WeihanLi.Common.Test/ExtensionsTest/StringExtensionTest.cs index 50ba29c2..d67da244 100644 --- a/test/WeihanLi.Common.Test/ExtensionsTest/StringExtensionTest.cs +++ b/test/WeihanLi.Common.Test/ExtensionsTest/StringExtensionTest.cs @@ -32,7 +32,7 @@ public void GetTypeByTypeName(Type type, string name) [InlineData("", "123")] [InlineData(" ", "123")] [InlineData(null, "123")] - public void GetNotEmptyValue(string value, string defaultValue) + public void GetNotEmptyValue(string? value, string defaultValue) { var expected = string.IsNullOrEmpty(value) ? defaultValue : value; Assert.Equal(expected, value.GetNotEmptyValueOrDefault(defaultValue)); @@ -43,7 +43,7 @@ public void GetNotEmptyValue(string value, string defaultValue) [InlineData("", "123")] [InlineData(" ", "123")] [InlineData(null, "123")] - public void StringGetValue(string value, string defaultValue) + public void StringGetValue(string? value, string defaultValue) { var expected = string.IsNullOrWhiteSpace(value) ? defaultValue : value; Assert.Equal(expected, value.GetValueOrDefault(defaultValue)); @@ -68,7 +68,7 @@ public void TrimStart(string? value, string? start) [InlineData(" ")] [InlineData("")] [InlineData(null)] - public void IsNullOrEmpty(string value) + public void IsNullOrEmpty(string? value) { Assert.Equal(string.IsNullOrEmpty(value), value.IsNullOrEmpty()); } @@ -79,7 +79,7 @@ public void IsNullOrEmpty(string value) [InlineData(" ")] [InlineData("")] [InlineData(null)] - public void IsNotNullOrEmpty(string value) + public void IsNotNullOrEmpty(string? value) { Assert.Equal(!string.IsNullOrEmpty(value), value.IsNotNullOrEmpty()); } @@ -90,7 +90,7 @@ public void IsNotNullOrEmpty(string value) [InlineData(" ")] [InlineData("")] [InlineData(null)] - public void IsNullOrWhiteSpace(string value) + public void IsNullOrWhiteSpace(string? value) { Assert.Equal(string.IsNullOrWhiteSpace(value), value.IsNullOrWhiteSpace()); } @@ -101,7 +101,7 @@ public void IsNullOrWhiteSpace(string value) [InlineData(" ")] [InlineData("")] [InlineData(null)] - public void IsNotNullOrWhiteSpace(string value) + public void IsNotNullOrWhiteSpace(string? value) { Assert.Equal(!string.IsNullOrWhiteSpace(value), value.IsNotNullOrWhiteSpace()); } diff --git a/test/WeihanLi.Common.Test/HelpersTest/BoundedConcurrentQueue.cs b/test/WeihanLi.Common.Test/HelpersTest/BoundedConcurrentQueue.cs index fa3d0e37..e8fe5839 100644 --- a/test/WeihanLi.Common.Test/HelpersTest/BoundedConcurrentQueue.cs +++ b/test/WeihanLi.Common.Test/HelpersTest/BoundedConcurrentQueue.cs @@ -17,7 +17,7 @@ public void FullQueue_DropWrite() Assert.False(queue.TryEnqueue(null)); Assert.Equal(1, queue.Count); } - + [Fact] public void FullQueue_DropOldest() {