public void Find_Generic_Enumerable_Method() { MethodInfo method; var class1 = Enumerable.Empty <TestClass>(); method = ExtensionMethodFinder.FindExtensionMethod(new NullCacheProvider(), typeof(IEnumerable <TestClass>), new object[] { }, "GenericMethod", false); Assert.IsNotNull(method); method.Invoke(null, new object[] { class1 }); var class2 = new TestClassCollection(); method = ExtensionMethodFinder.FindExtensionMethod(new NullCacheProvider(), typeof(TestClassCollection), new object[] { }, "GenericMethod", false); Assert.IsNotNull(method); method.Invoke(null, new object[] { class2 }); }
public void FinderTests() { MethodInfo method; var class1 = new Class1(); method = ExtensionMethodFinder.FindExtensionMethod(typeof(Class1), new object[] { 1 }, "TestMethod1", false); Assert.IsNotNull(method); method.Invoke(null, new object[] { class1, 1 }); method = ExtensionMethodFinder.FindExtensionMethod(typeof(Class1), new object[] { "x" }, "TestMethod1", false); Assert.IsNull(method); // note - fails, return TestMethod1! method = ExtensionMethodFinder.FindExtensionMethod(typeof(Class1), new object[] { 1 }, "TestMethod2", false); Assert.IsNotNull(method); method.Invoke(null, new object[] { class1, "1" }); method = ExtensionMethodFinder.FindExtensionMethod(typeof(Class1), new object[] { "x" }, "TestMethod2", false); Assert.IsNotNull(method); method.Invoke(null, new object[] { class1, "x" }); }
private static MethodInfo FindExtensionMethod(Type thisType, object[] args, string name, bool argsContainsThis) { return(ExtensionMethodFinder.FindExtensionMethod(NullCache, thisType, args, name, argsContainsThis)); }
private object ExecuteExtensionMethod(object[] args, string name) { object result = null; var methodTypesToFind = new[] { typeof(IEnumerable <DynamicPublishedContent>), typeof(DynamicPublishedContentList) }; //find known extension methods that match the first type in the list MethodInfo toExecute = null; foreach (var t in methodTypesToFind) { toExecute = ExtensionMethodFinder.FindExtensionMethod(t, args, name, false); if (toExecute != null) { break; } } if (toExecute != null) { if (toExecute.GetParameters().First().ParameterType == typeof(DynamicPublishedContentList)) { var genericArgs = (new[] { this }).Concat(args); result = toExecute.Invoke(null, genericArgs.ToArray()); } else if (TypeHelper.IsTypeAssignableFrom <IQueryable>(toExecute.GetParameters().First().ParameterType)) { //if it is IQueryable, we'll need to cast Items AsQueryable var genericArgs = (new[] { Items.AsQueryable() }).Concat(args); result = toExecute.Invoke(null, genericArgs.ToArray()); } else { var genericArgs = (new[] { Items }).Concat(args); result = toExecute.Invoke(null, genericArgs.ToArray()); } } else { throw new MissingMethodException(); } if (result != null) { if (result is IPublishedContent) { result = new DynamicPublishedContent((IPublishedContent)result); } if (result is IEnumerable <IPublishedContent> ) { result = new DynamicPublishedContentList((IEnumerable <IPublishedContent>)result); } if (result is IEnumerable <DynamicPublishedContent> ) { result = new DynamicPublishedContentList((IEnumerable <DynamicPublishedContent>)result); } } return(result); }