From f6ab67ea78d783898db953c5c8c9dd112b4e3aab Mon Sep 17 00:00:00 2001 From: "agrath@gmail.com" Date: Thu, 21 Jul 2011 11:27:34 -1200 Subject: [PATCH] Added support to ExtensionMethodFinder to do overload type resolution to locate the correct extension method based on types when there are multiple overloads available --- .../RazorDynamicNode/ExtensionMethodFinder.cs | 51 +++++++++++++++---- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/umbraco.MacroEngines.Juno/RazorDynamicNode/ExtensionMethodFinder.cs b/umbraco.MacroEngines.Juno/RazorDynamicNode/ExtensionMethodFinder.cs index 57c3886b31..c74776af8c 100644 --- a/umbraco.MacroEngines.Juno/RazorDynamicNode/ExtensionMethodFinder.cs +++ b/umbraco.MacroEngines.Juno/RazorDynamicNode/ExtensionMethodFinder.cs @@ -6,6 +6,7 @@ using System.Reflection; using System.Web.Compilation; using System.Runtime.CompilerServices; using System.Collections; +using System.Linq.Expressions; namespace umbraco.MacroEngines { @@ -123,23 +124,53 @@ namespace umbraco.MacroEngines { return null; } - - MethodInfo firstMethod = methods.First(); - // NH: this is to ensure that it's always the correct one being chosen when using the LINQ extension methods - if (methods.Count > 1) - firstMethod = methods.First(x => x.IsGenericMethodDefinition); - MethodInfo methodToExecute = null; - if (firstMethod.IsGenericMethodDefinition) + //Given the args, lets get the types and compare the type sequence to try and find the correct overload + var argTypes = args.ToList().ConvertAll(o => { - if (genericType != null) + Expression oe = (o as Expression); + if (oe != null) { - methodToExecute = firstMethod.MakeGenericMethod(genericType); + return oe.Type.FullName; } + return o.GetType().FullName; + }); + var methodsWithArgTypes = methods.ConvertAll(method => new { method = method, types = method.GetParameters().ToList().ConvertAll(pi => pi.ParameterType.FullName) }); + var firstMatchingOverload = methodsWithArgTypes.FirstOrDefault(m => + { + return m.types.SequenceEqual(argTypes); + }); + if (firstMatchingOverload != null) + { + methodToExecute = firstMatchingOverload.method; } else { - methodToExecute = firstMethod; + MethodInfo firstMethod = methods.FirstOrDefault(); + // NH: this is to ensure that it's always the correct one being chosen when using the LINQ extension methods + if (methods.Count > 1) + { + var firstGenericMethod = methods.FirstOrDefault(x => x.IsGenericMethodDefinition); + if (firstGenericMethod != null) + { + firstMethod = firstGenericMethod; + } + } + + if (firstMethod != null) + { + if (firstMethod.IsGenericMethodDefinition) + { + if (genericType != null) + { + methodToExecute = firstMethod.MakeGenericMethod(genericType); + } + } + else + { + methodToExecute = firstMethod; + } + } } return methodToExecute; }