string GetName(bcl.MethodBase method) { if (method.IsGenericMethod) { var genericArgs = method.GetGenericArguments(); return method.Name + "<" + string.Join(", ", genericArgs.Select(TypeUtils.ToFriendlyName)) + ">"; } return method.Name; }
MethodReference ImportMethodSpecification (SR.MethodBase method, IGenericContext context) { var method_info = method as SR.MethodInfo; if (method_info == null) throw new InvalidOperationException (); var element_method = ImportMethod (method_info.GetGenericMethodDefinition (), context, ImportGenericKind.Definition); var instance = new GenericInstanceMethod (element_method); var arguments = method.GetGenericArguments (); var instance_arguments = instance.GenericArguments; for (int i = 0; i < arguments.Length; i++) instance_arguments.Add (ImportType (arguments [i], context ?? element_method)); return instance; }
public MethodReference ImportMethod (SR.MethodBase method, IGenericContext context, ImportGenericKind import_kind) { if (IsMethodSpecification (method) || ImportOpenGenericMethod (method, import_kind)) return ImportMethodSpecification (method, context); var declaring_type = ImportType (method.DeclaringType, context); if (IsGenericInstance (method.DeclaringType)) method = method.Module.ResolveMethod (method.MetadataToken); var reference = new MethodReference { Name = method.Name, HasThis = HasCallingConvention (method, SR.CallingConventions.HasThis), ExplicitThis = HasCallingConvention (method, SR.CallingConventions.ExplicitThis), DeclaringType = ImportType (method.DeclaringType, context, ImportGenericKind.Definition), }; if (HasCallingConvention (method, SR.CallingConventions.VarArgs)) reference.CallingConvention &= MethodCallingConvention.VarArg; if (method.IsGenericMethod) ImportGenericParameters (reference, method.GetGenericArguments ()); var method_info = method as SR.MethodInfo; reference.ReturnType = method_info != null ? ImportType (method_info.ReturnType, context ?? reference) : ImportType (typeof (void), null); var parameters = method.GetParameters (); var reference_parameters = reference.Parameters; for (int i = 0; i < parameters.Length; i++) reference_parameters.Add ( new ParameterDefinition (ImportType (parameters [i].ParameterType, context ?? reference))); reference.DeclaringType = declaring_type; return reference; }
private SR.MethodBase FindMatchingMethodByName(Type targetType, SR.MethodInfo injectionMethod, string methodName) { var pars = injectionMethod.GetParameters(); var genArgs = injectionMethod.GetGenericArguments(); // Handle generic parameters Func<Type, Type> rMapper = (injParamType) => { var mappedType = TryMapTypeToTargetAssemblyType(targetType.Assembly, injParamType); return mappedType != null ? mappedType : injParamType; }; foreach (var m in targetType.GetMethodsAndCtors(BINDING_FLAGS)) { if (m.Name != methodName) continue; var mParams = m.GetParameters(); if (mParams.Length != pars.Length) continue; if (!mParams.Zip(pars, (p1, p2) => new { P1 = p1, P2 = p2 }).All(a => a.P1.ParameterMatches(a.P2, rMapper))) continue; if (!m.IsConstructor) { var mGenArgs = m.GetGenericArguments(); if (mGenArgs.Length != genArgs.Length) continue; } return m; } return null; }
public static bool MethodMatches(this MethodReference method, SR.MethodBase matches) { if (method.Name != matches.Name) return false; if (matches is SR.MethodInfo) { if (!method.ReturnType.TypeMatches(((SR.MethodInfo)matches).ReturnType)) return false; } var m1Params = method.Parameters; var m2Params = matches.GetParameters(); if (m1Params.Count != m2Params.Length) return false; for (var i = 0; i < m1Params.Count; i++) { if (!m1Params[i].ParameterMatches(m2Params[i])) return false; } if (!matches.IsConstructor) { var m1GenericArgs = method.Resolve().GenericParameters; var m2GenericArgs = matches.GetGenericArguments(); if (m1GenericArgs.Count != m2GenericArgs.Length) // No need to check types or constraints, no overloading for these in C# return false; } return true; }
MethodReference ImportMethodSpecification(SR.MethodBase method, Mono.Collections.Generic.Collection<IGenericParameterProvider> context) { var method_info = method as SR.MethodInfo; if (method_info == null) throw new InvalidOperationException (); var element_method = ImportMethod (method_info.GetGenericMethodDefinition (), context, ImportGenericKind.Definition); var instance = new GenericInstanceMethod (element_method); var arguments = method.GetGenericArguments (); var instance_arguments = instance.GenericArguments; AddToContext(ref context, element_method); try { for (int i = 0; i < arguments.Length; i++) instance_arguments.Add(ImportType(arguments[i], context)); return instance; } finally { RemoveFromContext(context); } }