From 77c6077a4c6e88ecc55830ccd7cf33c15552d843 Mon Sep 17 00:00:00 2001 From: hyzx86 Date: Mon, 7 Aug 2023 11:01:19 +0800 Subject: [PATCH 1/3] update oc 1.7.0-preview-17520 --- EasyOC.build/Commons.props | 2 +- src/Modules/EasyOC.Users/Controllers/ControllerExtensions.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/EasyOC.build/Commons.props b/EasyOC.build/Commons.props index 9d7bfe8..471bc8a 100644 --- a/EasyOC.build/Commons.props +++ b/EasyOC.build/Commons.props @@ -3,7 +3,7 @@ 1.0.7 - 1.6.0 + 1.7.0-preview-17520 OrcardCore,EasyOC $(VersionSuffix) diff --git a/src/Modules/EasyOC.Users/Controllers/ControllerExtensions.cs b/src/Modules/EasyOC.Users/Controllers/ControllerExtensions.cs index ddc37b8..c3dca71 100644 --- a/src/Modules/EasyOC.Users/Controllers/ControllerExtensions.cs +++ b/src/Modules/EasyOC.Users/Controllers/ControllerExtensions.cs @@ -38,7 +38,7 @@ public static async Task SendEmailAsync(this Controller controller, string To = email, Subject = subject, Body = body, - IsBodyHtml = true + IsHtmlBody = true }; var result = await smtpService.SendAsync(message); From 7020e6067d56677c775cfe2baed90ae85fc7ac76 Mon Sep 17 00:00:00 2001 From: hyzx86 Date: Sat, 7 Oct 2023 15:14:26 +0800 Subject: [PATCH 2/3] Support for overloaded method matching --- .../ActionReplaceOption.cs | 47 +++++++--- src/Modules/EasyOC.ReplaceAction/Readme.md | 10 +++ .../EasyOC.ReplaceAction/ServiceExtensions.cs | 89 ++++++++++++++++--- src/Modules/EasyOC.ReplaceAction/Startup.cs | 1 + 4 files changed, 124 insertions(+), 23 deletions(-) diff --git a/src/Modules/EasyOC.ReplaceAction/ActionReplaceOption.cs b/src/Modules/EasyOC.ReplaceAction/ActionReplaceOption.cs index ac800f4..48d7862 100644 --- a/src/Modules/EasyOC.ReplaceAction/ActionReplaceOption.cs +++ b/src/Modules/EasyOC.ReplaceAction/ActionReplaceOption.cs @@ -8,17 +8,6 @@ public class ActionReplaceOption { public List Items { get; set; } = new List(); - /// - /// Find first Match - /// - /// - /// - public ActionReplaceOptionItem FindOption(MethodInfo method) - { - return Items.OrderBy(x => x.Order).FirstOrDefault(x => x.TargetControllerFullName.Equals(method.DeclaringType.FullName) - && x.ActionMapping.ContainsKey(method.Name)); - } - public List AddReplaceOption(string targetControllerName, IDictionary actionMapping, int order = 0) where TargetNew : class @@ -37,16 +26,52 @@ public List AddReplaceOption(string targetCo return Items; } + + public List AddReplaceOption(string targetControllerName, IDictionary actionMapping) + where TargetNew : class + { + var type = typeof(TargetNew); + var typeInfo = type.GetTypeInfo(); + foreach (var kv in actionMapping) + { + var newMethod = kv.Value.Parameters != null ? typeInfo.GetMethod(kv.Value.Name, kv.Value.Parameters) : + typeInfo.GetMethod(kv.Value.Name); + if (newMethod != null) + { + var item = new ActionReplaceOptionItem + { + TargetControllerFullName = targetControllerName, + NewController = type, + TargetMethodDescriptions = new KeyValuePair(kv.Key, newMethod) + }; + Items.Add(item); + } + } + + return Items; + } + public List AddReplaceOption(IDictionary ActionMapping, int order = 0) where TargetNew : class { return AddReplaceOption(typeof(SourceOld).FullName, ActionMapping, order); } } + + public class MethodDescription + { + public string Name { get; set; } + /// + /// 如果未指定参数则不检查 + /// + public Type[] Parameters { get; set; } + } public class ActionReplaceOptionItem { public int Order { get; set; } = 0; public Dictionary ActionMapping { get; set; } + + public KeyValuePair? TargetMethodDescriptions { get; set; } public Type NewController { get; set; } public string TargetControllerFullName { get; set; } diff --git a/src/Modules/EasyOC.ReplaceAction/Readme.md b/src/Modules/EasyOC.ReplaceAction/Readme.md index a14ed7b..e7a2cda 100644 --- a/src/Modules/EasyOC.ReplaceAction/Readme.md +++ b/src/Modules/EasyOC.ReplaceAction/Readme.md @@ -74,5 +74,15 @@ public override void ConfigureServices(IServiceCollection services) nameof(EocAccountController.ExternalLoginCallback), nameof(EocAccountController.RegisterExternalLogin) ); + //sample 7 Support for overloaded method matching + services.ReplaceAction("OrchardCore.Workflows.Controllers.ActivityController", + new List() + { + new MethodDescription { Name = "Create", Parameters = new[] { typeof(string), typeof(long), typeof(string) }}, + new MethodDescription { Name = "Create", Parameters = new[] { typeof(string), typeof(ActivityEditViewModel) }}, + new MethodDescription { Name = "Edit", Parameters = new[] { typeof(long), typeof(string), typeof(string) }}, + }); } ``` + + diff --git a/src/Modules/EasyOC.ReplaceAction/ServiceExtensions.cs b/src/Modules/EasyOC.ReplaceAction/ServiceExtensions.cs index 8c64eb0..09e1d6e 100644 --- a/src/Modules/EasyOC.ReplaceAction/ServiceExtensions.cs +++ b/src/Modules/EasyOC.ReplaceAction/ServiceExtensions.cs @@ -3,6 +3,9 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; +using System; +using System.Collections.Generic; +using System.Linq; using System.Reflection; namespace EasyOC.ReplaceAction @@ -53,6 +56,27 @@ public static IServiceCollection ReplaceAction(this IServiceCollection ser }); } + + public static IServiceCollection ReplaceAction(this IServiceCollection services, string targetControllerFullName, IDictionary actionMapping) + where TNew : class + { + + return services.ReplaceAction(opt => + { + opt.AddReplaceOption(targetControllerFullName, actionMapping); + }); + } + + public static IServiceCollection ReplaceAction(this IServiceCollection services, string targetControllerFullName, IEnumerable actionMapping) + where TNew : class + { + + return services.ReplaceAction(opt => + { + opt.AddReplaceOption(targetControllerFullName, actionMapping.ToDictionary(k => k, v => v)); + }); + } + public static IServiceCollection ReplaceActionByActionNames(this IServiceCollection services, string targetControllerFullName, params string[] actionList) where TNew : class { @@ -85,20 +109,61 @@ public static IServiceProvider UseReplaceAction(this IServiceProvider servicePro else { var actionMethodName = descriptor.MethodInfo.Name; - if (descriptor.ControllerTypeInfo.FullName == item.TargetControllerFullName && - item.ActionMapping.ContainsKey(actionMethodName) - ) + if (descriptor.ControllerTypeInfo.FullName == item.TargetControllerFullName) { - descriptor.ControllerTypeInfo = item.NewController.GetTypeInfo(); - descriptor.MethodInfo = item.ActionMapping[actionMethodName]; - if (logger != null && logger.IsEnabled(LogLevel.Debug)) + if (item.TargetMethodDescriptions != null) { - logger.LogDebug("The Action:{action} of controller:{type} is replaced by {newContorller}.{method}", - item.TargetControllerFullName, - actionMethodName, - descriptor.ControllerTypeInfo.FullName, - item.ActionMapping[actionMethodName] - ); + var kv = item.TargetMethodDescriptions.Value; + if (kv.Key.Name != actionMethodName) + { + continue; + } + //如果未指定参数 则不检查 + if (kv.Key.Parameters != null && kv.Key.Parameters.Length > 0) + { + var parameters = descriptor.MethodInfo.GetParameters(); + if (parameters.Length != kv.Key.Parameters.Length) + { + continue; + } + for (int i = 0; i < parameters.Length; i++) + { + if (parameters[i].ParameterType != kv.Key.Parameters[i]) + { + continue; + } + } + } + descriptor.ControllerTypeInfo = item.NewController.GetTypeInfo(); + descriptor.MethodInfo = kv.Value; + + if (logger != null && logger.IsEnabled(LogLevel.Debug)) + { + logger.LogDebug("The Action:{action} of controller:{type} is replaced by {newContorller}.{method}", + item.TargetControllerFullName, + actionMethodName, + descriptor.ControllerTypeInfo.FullName, + kv.Value.Name + ); + } + } + else + { + if (item.ActionMapping.ContainsKey(actionMethodName)) + { + descriptor.ControllerTypeInfo = item.NewController.GetTypeInfo(); + + descriptor.MethodInfo = item.ActionMapping[actionMethodName]; + if (logger != null && logger.IsEnabled(LogLevel.Debug)) + { + logger.LogDebug("The Action:{action} of controller:{type} is replaced by {newContorller}.{method}", + item.TargetControllerFullName, + actionMethodName, + descriptor.ControllerTypeInfo.FullName, + item.ActionMapping[actionMethodName] + ); + } + } } } } diff --git a/src/Modules/EasyOC.ReplaceAction/Startup.cs b/src/Modules/EasyOC.ReplaceAction/Startup.cs index df06566..0f5c24d 100644 --- a/src/Modules/EasyOC.ReplaceAction/Startup.cs +++ b/src/Modules/EasyOC.ReplaceAction/Startup.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.DependencyInjection; using OrchardCore.Modules; +using System; namespace EasyOC.ReplaceAction { From 50ac39ad08d6d1dea124031685f2c4c68eda6a70 Mon Sep 17 00:00:00 2001 From: hyzx86 Date: Sat, 7 Oct 2023 15:18:18 +0800 Subject: [PATCH 3/3] upgrade to 1.7.0 --- EasyOC.build/Commons.props | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/EasyOC.build/Commons.props b/EasyOC.build/Commons.props index 471bc8a..1e7c7f9 100644 --- a/EasyOC.build/Commons.props +++ b/EasyOC.build/Commons.props @@ -2,8 +2,8 @@ - 1.0.7 - 1.7.0-preview-17520 + 1.0.8 + 1.7.0 OrcardCore,EasyOC $(VersionSuffix)