Sfoglia il codice sorgente

Prioritize Non-Generic methods (#815)

Bernhard Windisch 4 anni fa
parent
commit
c0da73b673

+ 13 - 1
Jint.Tests/Runtime/ExtensionMethods/CustomStringExtensions.cs

@@ -1,4 +1,6 @@
-using System.Linq;
+using System.Dynamic;
+using System.Linq;
+using Newtonsoft.Json;
 
 namespace Jint.Tests.Runtime.ExtensionMethods
 {
@@ -8,5 +10,15 @@ namespace Jint.Tests.Runtime.ExtensionMethods
         {
             return new string(value.Reverse().ToArray());
         }
+
+        public static T DeserializeObject<T>(this string json)
+        {
+            return JsonConvert.DeserializeObject<T>(json);
+        }
+
+        public static ExpandoObject DeserializeObject(this string json)
+        {
+            return DeserializeObject<ExpandoObject>(json);
+        }
     }
 }

+ 12 - 0
Jint.Tests/Runtime/ExtensionMethods/ExtensionMethodsTest.cs

@@ -45,5 +45,17 @@ namespace Jint.Tests.Runtime.ExtensionMethods
 
             Assert.Equal(40, result);
         }
+
+        [Fact]
+        public void ShouldPrioritizingNonGenericMethod()
+        {
+            var options = new Options();
+            options.AddExtensionMethods(typeof(CustomStringExtensions));
+
+            var engine = new Engine(options);
+            var result = engine.Execute("\"{'name':'Mickey'}\".DeserializeObject()").GetCompletionValue().ToObject() as dynamic;
+
+            Assert.Equal("Mickey", result.name);
+        }
     }
 }

+ 13 - 2
Jint/Runtime/Interop/MethodDescriptor.cs

@@ -60,13 +60,24 @@ namespace Jint.Runtime.Interop
         {
             static int CreateComparison(MethodDescriptor d1, MethodDescriptor d2)
             {
+                // if its a generic method, put it on the end
+                if (d1.Method.IsGenericMethod && !d2.Method.IsGenericMethod)
+                {
+                    return 1;
+                }
+
+                if (d2.Method.IsGenericMethod && !d1.Method.IsGenericMethod)
+                {
+                    return -1;
+                }
+
                 // put params versions to end, they can be tricky to match and can cause trouble / extra overhead
-                if (d1.HasParams)
+                if (d1.HasParams && !d2.HasParams)
                 {
                     return 1;
                 }
 
-                if (d2.HasParams)
+                if (d2.HasParams && !d1.HasParams)
                 {
                     return -1;
                 }