private GetGenericMethodDefinition ( ) : |
||
Résultat |
protected virtual MethodInfo VisitMethod(MethodInfo methodToSearch) { var newReflectedType = VisitType(methodToSearch.ReflectedType); if (newReflectedType != methodToSearch.ReflectedType) { methodToSearch = newReflectedType.GetMethod(methodToSearch.Name, (methodToSearch.IsStatic ? BindingFlags.Static : BindingFlags.Instance) | (methodToSearch.IsPublic ? BindingFlags.Public : BindingFlags.NonPublic), null, methodToSearch.GetParameters().Select(x => x.ParameterType).ToArray(), null); } if (!methodToSearch.IsGenericMethod) return methodToSearch; var genArgs = methodToSearch.GetGenericArguments(); var newGenArgs = genArgs.Select(VisitType).ToArray(); if (genArgs.SequenceEqual(newGenArgs)) return methodToSearch; return methodToSearch.GetGenericMethodDefinition().MakeGenericMethod(newGenArgs); }
public IHqlGeneratorForMethod GetMethodGenerator(MethodInfo method) { IHqlGeneratorForMethod methodGenerator; if (method.IsGenericMethod) { method = method.GetGenericMethodDefinition(); } if (_registeredMethods.TryGetValue(method, out methodGenerator)) { return methodGenerator; } // No method generator registered. Look to see if it's a standard LinqExtensionMethod var attr = method.GetCustomAttributes(typeof (LinqExtensionMethodAttribute), false); if (attr.Length == 1) { // It is // TODO - cache this? Is it worth it? return new HqlGeneratorForExtensionMethod((LinqExtensionMethodAttribute) attr[0], method); } // Not that either. Let's query each type generator to see if it can handle it foreach (var typeGenerator in _typeGenerators) { if (typeGenerator.SupportsMethod(method)) { return typeGenerator.GetMethodGenerator(method); } } throw new NotSupportedException(method.ToString()); }
public bool TryGetExpectProvider(MethodInfo method, out IMethodExpectProvider provider) { var foundExact = lookup.TryGetValue(method, out provider); if(!foundExact && method.IsGenericMethod) return lookup.TryGetValue(method.GetGenericMethodDefinition(), out provider); return foundExact; }
protected virtual IMethodCallTranslator GetItem(MethodInfo key) { if (key == null) { throw new ArgumentNullException("key"); } IMethodCallTranslator translator; if (Registry.TryGetValue(key, out translator)) { return translator; } if (key.IsGenericMethod && !key.IsGenericMethodDefinition) { if (Registry.TryGetValue(key.GetGenericMethodDefinition(), out translator)) { return translator; } } // Check if the generic form of the declaring type matches translator = GetItemFromGenericType(key); if (translator != null) { return translator; } // Finally, check any interfaces that may have a matching return GetItemFromInterfaces(key); }
private MethodInfo EnsureClosedMethod(MethodInfo method) { if (method.ContainsGenericParameters) { return method.GetGenericMethodDefinition().MakeGenericMethod(this.genericMethodArguments); } return method; }
protected virtual IMethodCallTranslator GetItem(MethodInfo key) { lock (Registry) { if (key == null) { throw new ArgumentNullException("key"); } IMethodCallTranslator translator; if (Registry.TryGetValue(key, out translator)) { return translator; } if (key.IsGenericMethod && !key.IsGenericMethodDefinition) { if (Registry.TryGetValue(key.GetGenericMethodDefinition(), out translator)) { return translator; } } // Check if the generic form of the declaring type matches translator = GetItemFromGenericType(key); if (translator != null) { return translator; } // Check any interfaces that may have a matching method translator = GetItemFromInterfaces(key); if (translator != null) { return translator; } // If no preregistered method is found, check to see if the method is decorated with N1QlFunctionAttribute translator = CreateFromN1QlFunctionAttribute(key); if (translator != null) { // Save this translator for reuse Registry.Add(key, translator); return translator; } // Finally, check base method if this is a virtual method var baseMethod = key.GetBaseDefinition(); if ((baseMethod != null) && (baseMethod != key)) { return GetItem(baseMethod); } // No match found return null; } }
public static void EnsureSatisfiesClassConstraints(MethodInfo reflectionMethodInfo) { MethodInfo genericMethodDefinition = reflectionMethodInfo.GetGenericMethodDefinition(); TypeInfo[] methodArguments = TypesToTypeInfos(reflectionMethodInfo.GetGenericArguments()); TypeInfo[] methodParameters = TypesToTypeInfos(genericMethodDefinition.GetGenericArguments()); TypeInfo[] typeArguments = TypesToTypeInfos(reflectionMethodInfo.DeclaringType.GetGenericArguments()); SigTypeContext typeContext = new SigTypeContext(typeArguments, methodArguments); EnsureSatisfiesClassConstraints(methodParameters, methodArguments, genericMethodDefinition, typeContext); }
private static bool ArgsMatch(MethodInfo m, ReadOnlyCollection<Expression> args, Type[] typeArgs) { ParameterInfo[] parameters = m.GetParameters(); if (parameters.Length != args.Count) { return false; } if ((!m.IsGenericMethod && (typeArgs != null)) && (typeArgs.Length > 0)) { return false; } if ((!m.IsGenericMethodDefinition && m.IsGenericMethod) && m.ContainsGenericParameters) { m = m.GetGenericMethodDefinition(); } if (m.IsGenericMethodDefinition) { if ((typeArgs == null) || (typeArgs.Length == 0)) { return false; } if (m.GetGenericArguments().Length != typeArgs.Length) { return false; } m = m.MakeGenericMethod(typeArgs); parameters = m.GetParameters(); } int index = 0; int count = args.Count; while (index < count) { Type parameterType = parameters[index].ParameterType; if (parameterType == null) { return false; } if (parameterType.IsByRef) { parameterType = parameterType.GetElementType(); } Expression operand = args[index]; if (!parameterType.IsAssignableFrom(operand.Type)) { if (operand.NodeType == ExpressionType.Quote) { operand = ((UnaryExpression) operand).Operand; } if (!parameterType.IsAssignableFrom(operand.Type) && !parameterType.IsAssignableFrom(StripExpression(operand.Type))) { return false; } } index++; } return true; }
private static MethodInfo GetBaseDefinition(MethodInfo method) { if (method.IsGenericMethod && !method.IsGenericMethodDefinition) { method = method.GetGenericMethodDefinition(); } return method.GetBaseDefinition(); }
public Maybe<ITransactionOptions> AsTransactional(MethodInfo target) { Contract.Requires(target != null); Contract.Ensures(Contract.Result<Maybe<TransactionAttribute>>() != null); var rawMethodInfo = target.IsGenericMethod ? target.GetGenericMethodDefinition() : target; return _TxMethods.ContainsKey(rawMethodInfo) ? Maybe.Some<ITransactionOptions>(_TxMethods[rawMethodInfo]) : Maybe.None<ITransactionOptions>(); }
public static object InvokeGenericMethod(object instance, MethodInfo methodInfo, Type[] typeArguments, object[] parameters) { if (methodInfo == null) throw new ArgumentNullException("methodInfo"); // Could check that methodInfo.GetParameters().Count() == parameters.Count(), and methodInfo.GetGenericArguments().Count() == typeArguments.Count() ? var method = methodInfo.GetGenericMethodDefinition(); var genericMethod = method.MakeGenericMethod(typeArguments); var result = genericMethod.Invoke(instance, parameters); return result; }
public virtual bool TryGetGenerator(MethodInfo method, out IHqlGeneratorForMethod generator) { if (method.IsGenericMethod) { method = method.GetGenericMethodDefinition(); } if (registeredMethods.TryGetValue(method, out generator)) return true; // Not that either. Let's query each type generator to see if it can handle it if (GetRuntimeMethodGenerator(method, out generator)) return true; return false; }
public override IMethodInfo ImplementGenericPattern(MethodInfo method, Type[] typeArgs, Type[] methodArgs) { var jointArgs = new List<Type>(); var patternArgs = method.IsGenericMethod ? method.GetGenericMethodDefinition().GetGenericArguments() : new Type[0]; var patternMethodArgs = GetGenericPattern(method).GetMethodGenericArguments(); var patternTypeArgs = GetGenericPattern(method).GetTypeGenericArguments(); for (var i = 0; i < patternArgs.Length; ++i) { var patternArg = patternArgs[i]; for (var j = 0; j < patternMethodArgs.Length; ++j) { var patternMethodArg = patternMethodArgs[j]; if (patternMethodArg.SameMetadataToken(patternArg)) { jointArgs.Add(methodArgs[j]); break; } } for (var j = 0; j < patternTypeArgs.Length; ++j) { var patternTypeArg = patternTypeArgs[j]; if (patternTypeArg.SameMetadataToken(patternArg)) { jointArgs.Add(typeArgs[j]); break; } } } var pattern = method.IsGenericMethod ? method.GetGenericMethodDefinition() : method; return ForgedMethod.Forge(pattern.MakeGenericMethod(jointArgs.ToArray()), this); }
private System.Reflection.MethodInfo ReplaceAnonymousType(System.Reflection.MethodInfo methodInfo) { if (methodInfo.IsGenericMethod) { var args = from a in methodInfo.GetGenericArguments() select ReplaceAnonymousType(a); return(methodInfo.GetGenericMethodDefinition().MakeGenericMethod(args.ToArray())); } else { return(methodInfo); } }
static MethodInfo ExtractBaseMethod (MethodInfo info) { if (info is MethodBuilder) return info; if (info is MethodOnTypeBuilderInst) return ((MethodOnTypeBuilderInst)info).base_method; if (info.IsGenericMethod) info = info.GetGenericMethodDefinition (); Type t = info.DeclaringType; if (!t.IsGenericType || t.IsGenericTypeDefinition) return info; return (MethodInfo)t.Module.ResolveMethod (info.MetadataToken); }
public void Generate(MethodInfo newMethod) { if (newMethod.IsGenericMethod) newMethod = newMethod.GetGenericMethodDefinition(); FieldInfo field = typeof (DynamicWrapperBase).GetField("UnderlyingObject", BindingFlags.Instance | BindingFlags.NonPublic); ParameterInfo[] parameters = newMethod.GetParameters(); Type[] parameterTypes = Enumerable.ToArray<Type>(Enumerable.Select<ParameterInfo, Type>((IEnumerable<ParameterInfo>) parameters, (Func<ParameterInfo, Type>) (parameter => parameter.ParameterType))); MethodBuilder methodBuilder = this._wrapperBuilder.DefineMethod(newMethod.Name, MethodAttributes.Public | MethodAttributes.Virtual, newMethod.ReturnType, parameterTypes); if (newMethod.IsGenericMethod) methodBuilder.DefineGenericParameters(Enumerable.ToArray<string>(Enumerable.Select<Type, string>((IEnumerable<Type>) newMethod.GetGenericArguments(), (Func<Type, string>) (arg => arg.Name)))); ILGenerator ilGenerator = methodBuilder.GetILGenerator(); WrapperMethodBuilder.LoadUnderlyingObject(ilGenerator, field); WrapperMethodBuilder.PushParameters((ICollection<ParameterInfo>) parameters, ilGenerator); this.ExecuteMethod((MethodBase) newMethod, parameterTypes, ilGenerator); WrapperMethodBuilder.Return(ilGenerator); }
public bool TryGetMethodGenerator(MethodInfo method, out IHqlGeneratorForMethod methodGenerator) { if (method.IsGenericMethod) { method = method.GetGenericMethodDefinition(); } if (GetRegisteredMethodGenerator(method, out methodGenerator)) return true; // No method generator registered. Look to see if it's a standard LinqExtensionMethod if (GetStandardLinqExtensionMethodGenerator(method, out methodGenerator)) return true; // Not that either. Let's query each type generator to see if it can handle it if (GetMethodGeneratorForType(method, out methodGenerator)) return true; return false; }
private static MethodInfo ObtainMethod(MethodInfo proxiedMethod, Type type) { Type[] genericArguments = null; if (proxiedMethod.IsGenericMethod) { genericArguments = proxiedMethod.GetGenericArguments(); proxiedMethod = proxiedMethod.GetGenericMethodDefinition(); } Type declaringType = proxiedMethod.DeclaringType; MethodInfo methodOnTarget = null; if (declaringType.IsInterface) { var mapping = type.GetInterfaceMap(declaringType); int index = Array.IndexOf(mapping.InterfaceMethods, proxiedMethod); Debug.Assert(index != -1); methodOnTarget = mapping.TargetMethods[index]; } else { // NOTE: this implementation sucks, feel free to improve it. var methods = MethodFinder.GetAllInstanceMethods(type, BindingFlags.Public | BindingFlags.NonPublic); foreach (var method in methods) { if (MethodSignatureComparer.Instance.Equals(method.GetBaseDefinition(), proxiedMethod)) { methodOnTarget = method; break; } } } if (methodOnTarget == null) { throw new ArgumentException( string.Format("Could not find method overriding {0} on type {1}. This is most likely a bug. Please report it.", proxiedMethod, type)); } if (genericArguments == null) { return methodOnTarget; } return methodOnTarget.MakeGenericMethod(genericArguments); }
private InterceptMethodElement FindInterceptMethodElement(InterceptContractElement interceptContractElement, MethodInfo method) { if (interceptContractElement == null) return null; if (interceptContractElement.Methods == null) return null; foreach (InterceptMethodElement interceptMethodElement in interceptContractElement.Methods) { string methodSignature = null; if (method.IsGenericMethod) methodSignature = method.GetGenericMethodDefinition().GetSignature(); else methodSignature = method.GetSignature(); if (interceptMethodElement.Signature.Equals(methodSignature)) return interceptMethodElement; } return null; }
static StackObject *GetGenericMethodDefinition_10(ILIntepreter __intp, StackObject *__esp, IList <object> __mStack, CLRMethod __method, bool isNewObj) { ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain; StackObject *ptr_of_this_method; StackObject *__ret = ILIntepreter.Minus(__esp, 1); ptr_of_this_method = ILIntepreter.Minus(__esp, 1); System.Reflection.MethodInfo instance_of_this_method = (System.Reflection.MethodInfo) typeof(System.Reflection.MethodInfo).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack)); __intp.Free(ptr_of_this_method); var result_of_this_method = instance_of_this_method.GetGenericMethodDefinition(); object obj_result_of_this_method = result_of_this_method; if (obj_result_of_this_method is CrossBindingAdaptorType) { return(ILIntepreter.PushObject(__ret, __mStack, ((CrossBindingAdaptorType)obj_result_of_this_method).ILInstance)); } return(ILIntepreter.PushObject(__ret, __mStack, result_of_this_method)); }
private static MethodInfo ObtainMethod(MethodInfo proxiedMethod, Type type) { Type[] genericArguments = null; if (proxiedMethod.IsGenericMethod) { genericArguments = proxiedMethod.GetGenericArguments(); proxiedMethod = proxiedMethod.GetGenericMethodDefinition(); } var declaringType = proxiedMethod.DeclaringType; MethodInfo methodOnTarget = null; if (declaringType.IsInterface && !type.IsInterface) { var mapping = type.GetInterfaceMap(declaringType); var index = Array.IndexOf(mapping.InterfaceMethods, proxiedMethod); Debug.Assert(index != -1); methodOnTarget = mapping.TargetMethods[index]; } else { // NOTE: this implementation sucks, feel free to improve it. var methods = MethodFinder.GetAllInstanceMethods(type, BindingFlags.Public | BindingFlags.NonPublic); foreach (var method in methods) { if (MethodSignatureComparer.Instance.Equals(method.GetBaseDefinition(), proxiedMethod)) { methodOnTarget = method; break; } } } if (methodOnTarget == null) { return null; } if (genericArguments == null) { return methodOnTarget; } return methodOnTarget.MakeGenericMethod(genericArguments); }
public static InferenceStrategy Derive(MethodInfo method) { Throw.If(!method.IsGenericMethod, "Method must be generic!"); var genericDefinition = method.GetGenericMethodDefinition(); var genericArguments = genericDefinition.GetGenericArguments(); var parameterTypes = genericDefinition.GetParameters().Select(p => p.ParameterType).ToList(); var inferencePaths = new List<IList<int>>(Enumerable.Repeat<IList<int>>(null, genericArguments.Length)); // for each parameter type, search for inference paths var currentPath = new Stack<int>(); for (var i = 0; i < parameterTypes.Count; ++i) { currentPath.Push(i); DeriveAllInferencePaths(parameterTypes[i], currentPath, inferencePaths, genericArguments); currentPath.Pop(); } return new InferenceStrategy { Method = genericDefinition, InferencePaths = inferencePaths.AsReadOnly() }; }
/// <summary> /// Identifies methods as instances of known sequence operators. /// </summary> /// <param name="method"> Method info to identify </param> /// <param name="sequenceMethod"> Identified sequence operator </param> /// <returns> /// <c>true</c> if method is known; <c>false</c> otherwise /// </returns> internal static bool TryIdentifySequenceMethod(MethodInfo method, out SequenceMethod sequenceMethod) { method = method.IsGenericMethod ? method.GetGenericMethodDefinition() : method; return _methodMap.TryGetValue(method, out sequenceMethod); }
private static MethodInfo MakeGenericMethod(MethodInfo method, IEnumerable<Type> typeParameters) { if(!method.IsGenericMethodDefinition) method = method.GetGenericMethodDefinition(); return method.MakeGenericMethod(typeParameters.ToArray()); }
private static bool ArgsMatch(MethodInfo m, ReadOnlyCollection<Expression> args, Type[] typeArgs) { ParameterInfo[] mParams = m.GetParameters(); if (mParams.Length != args.Count) return false; if (!m.IsGenericMethod && typeArgs != null && typeArgs.Length > 0) { return false; } if (!m.IsGenericMethodDefinition && m.IsGenericMethod && m.ContainsGenericParameters) { m = m.GetGenericMethodDefinition(); } if (m.IsGenericMethodDefinition) { if (typeArgs == null || typeArgs.Length == 0) return false; if (m.GetGenericArguments().Length != typeArgs.Length) return false; m = m.MakeGenericMethod(typeArgs); mParams = m.GetParameters(); } for (int i = 0, n = args.Count; i < n; i++) { Type parameterType = mParams[i].ParameterType; if (parameterType == null) return false; if (parameterType.IsByRef) parameterType = parameterType.GetElementType(); Expression arg = args[i]; if (!parameterType.IsAssignableFrom(arg.Type)) { if (arg.NodeType == ExpressionType.Quote) { arg = ((UnaryExpression)arg).Operand; } if (!parameterType.IsAssignableFrom(arg.Type) && !parameterType.IsAssignableFrom(StripExpression(arg.Type))) { return false; } } } return true; }
private MethodInfo EnsureClosedMethod(MethodInfo method) { if (method.ContainsGenericParameters) { Debug.Assert(genericMethodArguments != null); return method.GetGenericMethodDefinition().MakeGenericMethod(genericMethodArguments); } return method; }
bool InvokeOptimizedMethod(IMethod method, MethodInfo mi, MethodInvocationExpression node) { if (Builtins_ArrayTypedConstructor == mi) { // optimize constructs such as: // array(int, 2) IType type = TypeSystemServices.GetReferencedType(node.Arguments[0]); if (null != type) { EmitNewArray(type, node.Arguments[1]); return true; } } else if (mi.IsGenericMethod && Builtins_ArrayGenericConstructor == mi.GetGenericMethodDefinition()) { // optimize constructs such as: // array[of int](2) IType type = method.ConstructedInfo.GenericArguments[0]; if (null != type) { EmitNewArray(type, node.Arguments[0]); return true; } } else if (Builtins_ArrayTypedCollectionConstructor == mi) { // optimize constructs such as: // array(int, (1, 2, 3)) // array(byte, [1, 2, 3, 4]) IType type = TypeSystemServices.GetReferencedType(node.Arguments[0]); if (null != type) { ListLiteralExpression items = node.Arguments[1] as ListLiteralExpression; if (null != items) { EmitArray(type, items.Items); PushType(type.MakeArrayType(1)); return true; } } } else if (Array_get_Length == mi) { // don't use ldlen for System.Array if (!GetType(node.Target).IsArray) return false; // optimize constructs such as: // len(anArray) // anArray.Length Visit(node.Target); PopType(); _il.Emit(OpCodes.Ldlen); PushType(TypeSystemServices.IntType); return true; } return false; }
private static MethodInfo EnsureNonGeneric(MethodInfo methodInfo) { return methodInfo.IsGenericMethod ? methodInfo.GetGenericMethodDefinition() : methodInfo; }
public static bool IsLinqOperatorCall(MethodInfo mi) { if (mi.IsGenericMethod) { mi = mi.GetGenericMethodDefinition(); } return method2operator.ContainsKey(mi); }
private static bool ArgsMatch(MethodInfo methodInfo, IList<Expression> arguments, Type[] typeArgs) { ParameterInfo[] parameters = methodInfo.GetParameters(); if (parameters.Length != arguments.Count) { return false; } if (!methodInfo.IsGenericMethod && typeArgs != null && typeArgs.Length > 0) { return false; } if (!methodInfo.IsGenericMethodDefinition && methodInfo.IsGenericMethod && methodInfo.ContainsGenericParameters) { methodInfo = methodInfo.GetGenericMethodDefinition(); } if (methodInfo.IsGenericMethodDefinition) { if (typeArgs == null || typeArgs.Length == 0) { return false; } if (methodInfo.GetGenericArguments().Length != typeArgs.Length) { return false; } methodInfo = methodInfo.MakeGenericMethod(typeArgs); parameters = methodInfo.GetParameters(); } for (int i = 0; i < arguments.Count; i++) { Type parameterType = parameters[i].ParameterType; if (parameterType == null) { return false; } if (parameterType.IsByRef) { parameterType = parameterType.GetElementType(); } Expression operand = arguments[i]; if (!parameterType.IsAssignableFrom(operand.Type)) { if (operand.NodeType == ExpressionType.Quote) { operand = ((UnaryExpression)operand).Operand; } if (!parameterType.IsAssignableFrom(operand.Type) && !parameterType.IsAssignableFrom(StripExpression(operand.Type))) { return false; } } } return true; }
protected static MethodInfo GetGenericMethodFromTypes(MethodInfo method, Type[] genericTypes) { if (!method.IsGenericMethod) throw new ArgumentException("Generic types cannot be specified for a non-generic method: " + method.Name); Type[] genericArguments = method.GetGenericArguments(); if (genericArguments.Length != genericTypes.Length) throw new ArgumentException("An incorrect number of generic arguments was specified: " + genericTypes.Length + " (needed " + genericArguments.Length + ")"); method = method.GetGenericMethodDefinition().MakeGenericMethod(genericTypes); return method; }
internal static bool IsClosedFormOf( this MethodInfo methodInfo, MethodInfo genericMethod) => methodInfo.IsGenericMethod && Equals( methodInfo.GetGenericMethodDefinition(), genericMethod);
protected static MethodInfo GetGenericMethodFromArguments(MethodInfo method, object[] args) { method = new[] {method.GetGenericMethodDefinition()} .MatchingArguments(args) .First() .ToSpecializedMethod(args); return method; }