public static bool MethodIsClosedFormOf(this MethodInfo methodInfo, MethodInfo genericMethod) { return methodInfo.IsGenericMethod && ReferenceEquals( methodInfo.GetGenericMethodDefinition(), genericMethod); }
public static string ToSerializableForm(this MethodInfo method) { string serializableName = method.DeclaringType.AssemblyQualifiedName + Environment.NewLine; if (!method.IsGenericMethod) { serializableName += method.ToString(); } else { serializableName += method.GetGenericMethodDefinition().ToString() + Environment.NewLine + String.Join(Environment.NewLine, method.GetGenericArguments().Select(ty => ty.ToSerializableForm()).ToArray()); } return serializableName; }
public static bool ArgumentsMatch(this MethodInfo method, Type[] typeArguments, object[] arguments) { if ((!method.IsGenericMethodDefinition && method.IsGenericMethod) && method.ContainsGenericParameters) method = method.GetGenericMethodDefinition(); if (method.IsGenericMethodDefinition) { if (typeArguments == null || typeArguments.Length == 0) return false; if (method.GetGenericArguments().Length != typeArguments.Length) return false; method = method.MakeGenericMethod(typeArguments); } else if (typeArguments != null && typeArguments.Length > 0) return false; return method.ArgumentsMatch(arguments); }
public static MethodInfo XGetGenericDefinition(this MethodInfo mi) { if (mi == null) return null; return mi.IsGenericMethod ? mi.GetGenericMethodDefinition() : mi; }
private static bool IsSourceNameEqual(this MethodInfo target, MethodInfo source, Tuple<string, string> rootTargetAndSourceFullNames) { Contract.Requires(target != null); Contract.Requires(source != null); Contract.Requires(rootTargetAndSourceFullNames != null); Contract.Requires(rootTargetAndSourceFullNames.Item1 != null); Contract.Requires(rootTargetAndSourceFullNames.Item2 != null); Contract.Requires(rootTargetAndSourceFullNames.Item1 != rootTargetAndSourceFullNames.Item2); if (!target.DeclaringType.IsSourceNameEqual(source.DeclaringType, rootTargetAndSourceFullNames)) { return false; } if (source.IsGenericMethodDefinition || !source.IsGenericMethod) { return source.ToString().Replace(rootTargetAndSourceFullNames.Item2, rootTargetAndSourceFullNames.Item1) == target.ToString(); } else { if (target.IsGenericMethodDefinition || !target.IsGenericMethod) { return false; } var sourceGenericArguments = source.GetGenericArguments(); var targetGenericArguments = target.GetGenericArguments(); if (sourceGenericArguments.Length != targetGenericArguments.Length) { return false; } if (!target.GetGenericMethodDefinition().IsSourceNameEqual(source.GetGenericMethodDefinition(), rootTargetAndSourceFullNames)) { return false; } return !sourceGenericArguments.Where( (t, i) => !targetGenericArguments[i].IsSourceNameEqual(t, rootTargetAndSourceFullNames)).Any(); } }
internal static MethodInfo TryGetGenericMethodDefinition(this MethodInfo method) { return method != null && method.IsGenericMethod && !method.IsGenericMethodDefinition ? method.GetGenericMethodDefinition() : method; }
public static MethodInfo GetGenericMethodOrRegular(this MethodInfo methodInfo) { return methodInfo.IsGenericMethod ? methodInfo.GetGenericMethodDefinition() : methodInfo; }
internal static string GetMethodDescription(this MethodInfo @this, SortedSet<string> namespaces, bool includeOverride, RequiresExplicitInterfaceImplementation requiresExplicitInterfaceImplementation) { if (@this.IsGenericMethod) { @this = @this.GetGenericMethodDefinition(); } @this.ReturnType.AddNamespaces(namespaces); var isOverride = includeOverride ? (@this.DeclaringType.IsClass ? "override " : string.Empty) : string.Empty; var returnType = @this.ReturnType == typeof(void) ? "void" : $"{@this.ReturnType.GetFullName(namespaces)}"; var methodName = @this.Name; var generics = string.Empty; var constraints = string.Empty; if (@this.IsGenericMethodDefinition) { var result = @this.GetGenericArguments(namespaces); generics = result.Arguments; constraints = result.Constraints.Length == 0 ? string.Empty : $" {result.Constraints}"; } var parameters = @this.GetParameters(namespaces); var explicitInterfaceName = requiresExplicitInterfaceImplementation == RequiresExplicitInterfaceImplementation.Yes ? $"{@this.DeclaringType.GetFullName(namespaces)}." : string.Empty; return $"{isOverride}{returnType} {explicitInterfaceName}{methodName}{generics}({parameters}){constraints}"; }