static public TypeReference GetRealType(this MethodReference method, TypeReference type)
 {
     if (type.Name.StartsWith("!!"))
     {
         if (method.GenericParameters.Count > 0)
         {
             return(method.GenericParameters[int.Parse(type.Name.Substring(2))]);
         }
         return(method.Resolve().GenericParameters[int.Parse(type.Name.Substring(2))]);
     }
     if (type.Name.StartsWith("!"))
     {
         if (method.DeclaringType.GenericParameters.Count > 0)
         {
             return(method.DeclaringType.GenericParameters[int.Parse(type.Name.Substring(1))]);
         }
         return(method.DeclaringType.Resolve().GenericParameters[int.Parse(type.Name.Substring(1))]);
     }
     if (type.IsGenericParameter)
     {
         return(method.Resolve().GenericParameterTypes().Reverse().First(_Type => _Type.Name == type.Name));
     }
     if (type.GenericParameters.Count > 0)
     {
         return(type.MakeGenericType(type.GenericParameters.Select(_Type => method.GetRealType(_Type))));
     }
     if (type is GenericInstanceType)
     {
         return(method.Module.Import(type).MakeGenericType((type as GenericInstanceType).GenericArguments.Select(_Type => method.GetRealType(_Type))));
     }
     return(type);
 }
        static public MethodReference Repair2(this MethodReference @this, MethodReference method)
        {
            var _types = @this.GenericParameterTypes().Select(_Type => _Type.Name.StartsWith("!!") ? @this.GenericParameters[int.Parse(_Type.Name.Substring(2))] : (_Type.Name.StartsWith("!") ? @this.DeclaringType.GenericParameters[int.Parse(_Type.Name.Substring(1))] : _Type)).ToArray();

            if (_types.Length > 0)
            {
                if (method.GenericParameters.Count > 0)
                {
                    var _method = new GenericInstanceMethod(@this.Module.Import(method.Resolve()));
                    foreach (var k in method.GenericParameters)
                    {
                        _method.GenericArguments.Add(k.Name.StartsWith("!!") ? @this.GenericParameters[int.Parse(k.Name.Substring(2))] : (k.Name.StartsWith("!") ? @this.DeclaringType.GenericParameters[int.Parse(k.Name.Substring(1))] : k));
                    }
                    _method.DeclaringType = @this.GetRealType(method.DeclaringType);
                    return(_method);
                }
                else
                {
                    var dec = @this.GetRealType(method.DeclaringType);
                    if (dec is GenericInstanceType)
                    {
                        var _method = @this.Module.Import(method.Resolve()).MakeHostInstanceGeneric((dec as GenericInstanceType).GenericArguments.ToArray());
                        //var _method = new MethodReference(method.Name, that.GetRealType(method.ReturnType), that.GetRealType(method.DeclaringType));
                        //foreach (var _parameter in method.Parameters) { _method.Parameters.Add(new ParameterDefinition(_parameter.Name, _parameter.Attributes, that.GetRealType(_parameter.ParameterType))); }
                        return(_method);
                    }
                    else
                    {
                        return(method);
                    }
                }
            }
            //var met = method.Resolve();
            //if (met.GenericParameters.Count > 0)
            //{
            //    var kk = method.Module.Import(met).MakeGenericMethod(_types.Reverse().Take(met.GenericParameters.Count).Reverse().ToArray());
            //    if (met.DeclaringType.GenericParameters.Count > 0) { kk.DeclaringType = met.DeclaringType.MakeGenericType(_types.Take(met.DeclaringType.GenericParameters.Count)); }
            //    return kk;
            //}
            //if (met.DeclaringType.GenericParameters.Count > 0)
            //{
            //    var kk = method.Module.Import(met);
            //    kk.DeclaringType = met.DeclaringType.MakeGenericType(_types.Take(met.DeclaringType.GenericParameters.Count));
            //    return kk;
            //}
            return(method);
        }