From 3f203f8d2cbe7122517f5f080dcf9c02d8b05c22 Mon Sep 17 00:00:00 2001 From: Jb Evain Date: Sun, 10 Jul 2016 17:33:11 -0700 Subject: Improve matching from import generic stack. Fix #278 That's not pretty, we should not match against strings. We should rather keep in the context the original method that we could compare, but as a quick fix, this will do. --- Mono.Cecil/Import.cs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/Mono.Cecil/Import.cs b/Mono.Cecil/Import.cs index fda6a9c..d1e9929 100644 --- a/Mono.Cecil/Import.cs +++ b/Mono.Cecil/Import.cs @@ -79,7 +79,7 @@ namespace Mono.Cecil { if (candidate == null) continue; - if (method != candidate.Name) + if (method != NormalizeMethodName (candidate)) continue; return candidate.GenericParameters [position]; @@ -88,6 +88,11 @@ namespace Mono.Cecil { throw new InvalidOperationException (); } + public string NormalizeMethodName (MethodReference method) + { + return method.DeclaringType.FullName + "." + method.Name; + } + public TypeReference TypeParameter (string type, int position) { for (int i = stack.Count - 1; i >= 0; i--) { @@ -231,18 +236,23 @@ namespace Mono.Cecil { throw new InvalidOperationException (); if (type.DeclaringMethod != null) - return context.MethodParameter (type.DeclaringMethod.Name, type.GenericParameterPosition); + return context.MethodParameter (NormalizeMethodName (type.DeclaringMethod), type.GenericParameterPosition); if (type.DeclaringType != null) - return context.TypeParameter (NormalizedFullName (type.DeclaringType), type.GenericParameterPosition); + return context.TypeParameter (NormalizeTypeFullName (type.DeclaringType), type.GenericParameterPosition); throw new InvalidOperationException(); } - private static string NormalizedFullName (Type type) + static string NormalizeMethodName (SR.MethodBase method) + { + return NormalizeTypeFullName (method.DeclaringType) + "." + method.Name; + } + + static string NormalizeTypeFullName (Type type) { if (IsNestedType (type)) - return NormalizedFullName (type.DeclaringType) + "/" + type.Name; + return NormalizeTypeFullName (type.DeclaringType) + "/" + type.Name; return type.FullName; } @@ -627,7 +637,7 @@ namespace Mono.Cecil { var mvar_parameter = (GenericParameter) type; if (mvar_parameter.DeclaringMethod == null) throw new InvalidOperationException (); - return context.MethodParameter (mvar_parameter.DeclaringMethod.Name, mvar_parameter.Position); + return context.MethodParameter (context.NormalizeMethodName (mvar_parameter.DeclaringMethod), mvar_parameter.Position); } throw new NotSupportedException (type.etype.ToString ()); -- cgit v1.2.3