public void Test_ClosedGenericMethod_InClosedGenericType() { var method = typeof(GenericClass <int>).GetMethod("GenericMethod").MakeGenericMethod(typeof(string)); var registerableMethod = MethodInfoBasedNodeTypeRegistry.GetRegisterableMethodDefinition(method, true); Assert.That(registerableMethod, Is.SameAs(typeof(GenericClass <>).GetMethod("GenericMethod"))); }
public void Test_ClosedGenericMethod() { var method = ReflectionUtility.GetMethod(() => Queryable.Count <object> (null)); var registerableMethod = MethodInfoBasedNodeTypeRegistry.GetRegisterableMethodDefinition(method, true); Assert.That(registerableMethod, Is.SameAs(method.GetGenericMethodDefinition())); }
public void Test_NonGenericMethod_InGenericTypeDefinition() { var method = typeof(GenericClass <>).GetMethod("NonGenericMethod"); var registerableMethod = MethodInfoBasedNodeTypeRegistry.GetRegisterableMethodDefinition(method, true); Assert.That(registerableMethod, Is.SameAs(method)); }
public void Test_ClosedGenericMethod_InClosedGenericType_HavingOverloadsDistinguishedByParameterPosition() { var methodName = "GenericMethodOverloadedWithGenericParameterFromTypeAtDifferentPosition"; var method1 = typeof(GenericClass <int, string>).GetMethods() .Where(m => m.Name == methodName && m.GetParameters()[0].ParameterType.Name == "Int32") .Select(m => m.MakeGenericMethod(typeof(double))) .Single(); var method2 = typeof(GenericClass <int, string>).GetMethods() .Where(m => m.Name == methodName && m.GetParameters()[0].ParameterType.Name == "T3") .Select(m => m.MakeGenericMethod(typeof(double))) .Single(); var expectedMethod1 = typeof(GenericClass <,>).GetMethods() .Single(m => m.Name == methodName && m.GetParameters()[0].ParameterType.Name == "T1"); var expectedMethod2 = typeof(GenericClass <,>).GetMethods() .Single(m => m.Name == methodName && m.GetParameters()[0].ParameterType.Name == "T3"); var registerableMethod1 = MethodInfoBasedNodeTypeRegistry.GetRegisterableMethodDefinition(method1, true); var registerableMethod2 = MethodInfoBasedNodeTypeRegistry.GetRegisterableMethodDefinition(method2, true); Assert.That(registerableMethod1, Is.SameAs(expectedMethod1)); Assert.That(registerableMethod2, Is.SameAs(expectedMethod2)); }
public void Test_OrdinaryMethod() { var method = typeof(object).GetMethod("Equals", BindingFlags.Public | BindingFlags.Instance); var registerableMethod = MethodInfoBasedNodeTypeRegistry.GetRegisterableMethodDefinition(method, true); Assert.That(registerableMethod, Is.SameAs(method)); }
static MethodInfo GetSupportedMethod <T>(Expression <Func <T> > methodCall) { Utils.CheckNotNull("methodCall", methodCall); var method = ReflectionUtility.GetMethod(methodCall); return(MethodInfoBasedNodeTypeRegistry.GetRegisterableMethodDefinition(method, throwOnAmbiguousMatch: true)); }
/// <summary> /// Gets the <see cref="MethodInfo"/> from a given <see cref="LambdaExpression"/> that has to wrap a <see cref="MethodCallExpression"/>. /// If the method is a generic method, its open generic method definition is returned. /// This method can be used for registration of the node type with an <see cref="MethodInfoBasedNodeTypeRegistry"/>. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="methodCall">The method call.</param> /// <returns></returns> protected static MethodInfo GetSupportedMethod <T> (Expression <Func <T> > methodCall) { ArgumentUtility.CheckNotNull("methodCall", methodCall); var method = ReflectionUtility.GetMethod(methodCall); return(MethodInfoBasedNodeTypeRegistry.GetRegisterableMethodDefinition(method)); }
public static MethodInfo GetSupportedMethod <T>(Expression <Func <T> > methodCall) { CheckNotNull("methodCall", methodCall); var method = GetMethod(methodCall); return(MethodInfoBasedNodeTypeRegistry.GetRegisterableMethodDefinition(method, true)); }
public void Test_NonGenericMethod_InClosedGenericType_HavingOverloadsDistinguishedByParameterTypeFromGenericClass_WithDoNotThrowOnAmbiguity_ReturnsNull() { var methodName = "NonGenericMethodOverloadedWithGenericParameterFromTypeAndSameParameterName"; var method1 = typeof(GenericClass <int, string>).GetMethods() .Single(m => m.Name == methodName && m.GetParameters()[0].ParameterType.Name == "Int32"); var method2 = typeof(GenericClass <int, string>).GetMethods() .Single(m => m.Name == methodName && m.GetParameters()[0].ParameterType.Name == "String"); Assert.That(MethodInfoBasedNodeTypeRegistry.GetRegisterableMethodDefinition(method1, throwOnAmbiguousMatch: false), Is.Null); Assert.That(MethodInfoBasedNodeTypeRegistry.GetRegisterableMethodDefinition(method2, throwOnAmbiguousMatch: false), Is.Null); }
public void Test_NonGenericMethod_InClosedGenericType_HavingOverloadsDistinguishedByParameterCount() { var methodName = "NonGenericMethodOverloadedWithGenericParameterFromTypeAndDifferentParameterCount"; var method1 = typeof(GenericClass <int, string>).GetMethods() .Single(m => m.Name == methodName && m.GetParameters().Length == 1); var method2 = typeof(GenericClass <int, string>).GetMethods() .Single(m => m.Name == methodName && m.GetParameters().Length == 2); var expectedMethod1 = typeof(GenericClass <,>).GetMethods() .Single(m => m.Name == methodName && m.GetParameters().Length == 1); var expectedMethod2 = typeof(GenericClass <,>).GetMethods() .Single(m => m.Name == methodName && m.GetParameters().Length == 2); var registerableMethod1 = MethodInfoBasedNodeTypeRegistry.GetRegisterableMethodDefinition(method1, true); var registerableMethod2 = MethodInfoBasedNodeTypeRegistry.GetRegisterableMethodDefinition(method2, true); Assert.That(registerableMethod1, Is.SameAs(expectedMethod1)); Assert.That(registerableMethod2, Is.SameAs(expectedMethod2)); }
public void Test_NonGenericMethod_InClosedGenericType_HavingOverloadsDistinguishedByParameterTypeFromGenericClass_WithThrowOnAmbiguity_ThrowsNotSupportedException() { var methodName = "NonGenericMethodOverloadedWithGenericParameterFromTypeAndSameParameterName"; var method1 = typeof(GenericClass <int, string>).GetMethods() .Single(m => m.Name == methodName && m.GetParameters()[0].ParameterType.Name == "Int32"); var method2 = typeof(GenericClass <int, string>).GetMethods() .Single(m => m.Name == methodName && m.GetParameters()[0].ParameterType.Name == "String"); Assert.That( () => MethodInfoBasedNodeTypeRegistry.GetRegisterableMethodDefinition(method1, throwOnAmbiguousMatch: true), Throws.TypeOf <NotSupportedException>().With.Message.StringStarting( "A generic method definition cannot be resolved for method 'Boolean NonGenericMethodOverloadedWithGenericParameterFromTypeAndSameParameterName(Int32, Double)' " + "on type 'Remotion.Linq.UnitTests.Parsing.Structure.NodeTypeProviders.MethodInfoBasedNodeTypeRegistryTests.TestDomain.GenericClass`2[T1,T2]' because a distinct match is not possible.")); Assert.That( () => MethodInfoBasedNodeTypeRegistry.GetRegisterableMethodDefinition(method2, throwOnAmbiguousMatch: true), Throws.TypeOf <NotSupportedException>().With.Message.StringStarting( "A generic method definition cannot be resolved for method 'Boolean NonGenericMethodOverloadedWithGenericParameterFromTypeAndSameParameterName(System.String, Double)' " + "on type 'Remotion.Linq.UnitTests.Parsing.Structure.NodeTypeProviders.MethodInfoBasedNodeTypeRegistryTests.TestDomain.GenericClass`2[T1,T2]' because a distinct match is not possible.")); }
public void Test_NonGenericMethod_InClosedGenericType_HavingOverloadsDistinguishedByGenericParamterAndReturnType() { var methodName = "NonGenericMethodOverloadedWithGenericParameterFromTypeAndDifferentReturnTypes"; var method1 = typeof(GenericClass <int, string>).GetMethods() .Single(m => m.Name == methodName && m.GetParameters()[0].ParameterType.Name == "Int32"); var method2 = typeof(GenericClass <int, string>).GetMethods() .Single(m => m.Name == methodName && m.GetParameters()[0].ParameterType.Name == "String"); var expectedMethod1 = typeof(GenericClass <,>).GetMethods() .Single(m => m.Name == methodName && m.GetParameters()[0].ParameterType.Name == "T1"); var expectedMethod2 = typeof(GenericClass <,>).GetMethods() .Single(m => m.Name == methodName && m.GetParameters()[0].ParameterType.Name == "T2"); var registerableMethod1 = MethodInfoBasedNodeTypeRegistry.GetRegisterableMethodDefinition(method1, true); var registerableMethod2 = MethodInfoBasedNodeTypeRegistry.GetRegisterableMethodDefinition(method2, true); Assert.That(registerableMethod1, Is.SameAs(expectedMethod1)); Assert.That(registerableMethod2, Is.SameAs(expectedMethod2)); }
public void ParseTree_MethodCallExpression_WithInstanceMethod_InGenericType() { var containsMethod = typeof(List <int>).GetMethod("Contains"); var querySourceExpression = Expression.Parameter(typeof(List <int>), "querySource"); var itemExpression = Expression.Constant(4); var expression = Expression.Call(querySourceExpression, containsMethod, itemExpression); _methodInfoBasedNodeTypeRegistry.Register( new[] { MethodInfoBasedNodeTypeRegistry.GetRegisterableMethodDefinition(containsMethod, true) }, typeof(ContainsExpressionNode)); var result = _expressionTreeParser.ParseTree(expression); Assert.That(result, Is.InstanceOf(typeof(ContainsExpressionNode))); Assert.That(((ContainsExpressionNode)result).Item, Is.SameAs(itemExpression)); var source = ((ContainsExpressionNode)result).Source; Assert.That(source, Is.InstanceOf(typeof(MainSourceExpressionNode))); Assert.That(((MainSourceExpressionNode)source).ParsedExpression, Is.SameAs(querySourceExpression)); }
protected MethodInfo GetGenericMethodDefinition <TReturn> (Expression <Func <TReturn> > methodCall) { var method = ReflectionUtility.GetMethod(methodCall); return(MethodInfoBasedNodeTypeRegistry.GetRegisterableMethodDefinition(method, throwOnAmbiguousMatch: true)); }