ResolveMethod () Returning a completely different method

I solved the methods from the .NET DLL and noticed that the method returned by Module.ResolveMethod () was completely different from the original method. I specify the method exactly MetadataToken, so it makes absolutely no sense to me why I get everything except the original method.

In the example below, I have a Dispose () method. I get its metadata token and resolve it, only to find that now I have the OnBackColorChanged (System.EventArgs) method

static void Main(string[] args)
    {
        Assembly assembly = Assembly.LoadFrom(@"C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Windows.Forms.dll");
        MethodInfo method = assembly.GetModules()[0].GetTypes()[300].GetMethods()[362];
        Console.WriteLine(method); //Returns 'Void Dispose()'

        MethodInfo method2 = (MethodInfo)assembly.GetModules()[0].ResolveMethod(method.MetadataToken);
        Console.WriteLine(method2); //Returns 'Void OnBackColorChanged(System.EventArgs)' ...why?
    }
+4
source share
1 answer

Button, through a long chain of inheritance, inherits from a class Componentthat implements IDisposableand has a method void Dispose(). This is the method you get through

assembly.GetModules()[0].GetTypes()[300].GetMethods()[362];

Component, System, , (System.Windows.Forms) .

, , BindingFlags.DeclaredOnly:

var allMethods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);

, , Button, - , .

- :

Assembly assembly = Assembly.LoadFrom(@"C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Windows.Forms.dll");
var module = assembly.GetModules()[0];
var type = module.GetTypes()[300];
var allMethods = type.GetMethods().Where(c => c.Module == module).ToArray();
+3

Source: https://habr.com/ru/post/1694743/


All Articles