Jump to content
Tuts 4 You

dnLib Inject MethodDef in TypeDef


Kaidoz

Recommended Posts

Posted

Hey everyone. I try inject Method in TypeDef, but i cant do. Maybe anybody help me?
My code:
 

        try
        {
            ModuleDefMD typeModule = ModuleDefMD.Load(typeof(Encrypt).Module);
            var mthDef = typeModule.ResolveMethod(MDToken.ToRID(typeof(Encrypt).GetMethods().Where(x => x.Name == "EncryptOrDecrypt").First().MetadataToken));
            foreach (var d in module.Types)
                if (d.HasMethods)
                {
                    d.Methods.Add(mthDef);
                }


            foreach (MethodDef md in module.GlobalType.Methods)
            {
                if (md.Name == ".ctor")
                {
                    module.GlobalType.Remove(md);
                    break;
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }

 

  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...