Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect GenericVar.OwnerType in instruction operand #46

Closed
yck1509 opened this issue Oct 16, 2015 · 3 comments
Closed

Incorrect GenericVar.OwnerType in instruction operand #46

yck1509 opened this issue Oct 16, 2015 · 3 comments

Comments

@yck1509
Copy link
Contributor

yck1509 commented Oct 16, 2015

In instruction operand, a GenericVar's OwnerType returns the method's declaring type instead of the owner type of the GenericVar.

using System;
using System.Diagnostics;
using System.Linq;
using dnlib.DotNet;
using dnlib.DotNet.Emit;

internal class X<T> {
    public static T W() {
        return default(T);
    }
}

internal class Program {
    static string Q() {
        return X<string>.W();
    }

    static void Main(string[] args) {
        var module = ModuleDefMD.Load(typeof(Program).Assembly.Location);
        var type = module.Find("Program", true);
        var method = type.FindMethod("Q");

        var instr = method.Body.Instructions.First(i => i.OpCode == OpCodes.Call);
        var operand = (IMethod)instr.Operand;

        var x = module.Find("X`1", true);
        var retType = operand.MethodSig.RetType; // <T>
        Console.WriteLine("{0} {1}", x, retType);

        Debug.Assert(retType is GenericVar);
        Debug.Assert(new SigComparer().Equals(((GenericVar)retType).OwnerType, x));
        Console.ReadKey();
    }
}
@0xd4d 0xd4d closed this as completed in 5a87e35 Oct 17, 2015
@bprg
Copy link

bprg commented Oct 17, 2015

Hi,
Please try the assembly still the OwnerType return null .

        private static void Main(string[] args)
        {
            ModuleDef moduleDefinition = ModuleDefMD.Load(@"C:\Example.exe");

            foreach (var typeDef in moduleDefinition.GetTypes())
            {
                foreach (var methodDef in typeDef.Methods)
                {
                    if (!methodDef.HasBody)
                        continue;

                    foreach (var instr in methodDef.Body.Instructions)
                    {
                        if (instr.OpCode != OpCodes.Callvirt)
                            continue;

                        var method = (IMethod) instr.Operand;
                        foreach (var typeSig in method.MethodSig.Params)
                        {
                            if (typeSig is GenericVar)
                            {
                                if (((GenericVar) typeSig).OwnerType == null)
                                {
                                    Console.WriteLine("OwnerType == null");
                                }
                            }
                        }
                    }
                }
            }
        }

@0xd4d
Copy link
Collaborator

0xd4d commented Oct 17, 2015

You don't have an assembly and type resolver. Call ModuleDef.CreateModuleContext() and pass it in to Load().

@bprg
Copy link

bprg commented Oct 17, 2015

Thanks solved the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants