How to delete a method from a type

34 views
Skip to first unread message

Jirka Šáda

unread,
Jan 29, 2020, 12:05:11 PM1/29/20
to mono-cecil

I have an assembly that contains a type named EncryptedStrings, and inside it is a method called DecryptString. I wanted to remove this method from the type and write the modified assembly back. I wrote this very simple code for it:

var file = @"path-to-file.exe";
var ass = AssemblyDefinition.ReadAssembly(file); // load assembly
var type = ass.MainModule.GetType("EncryptedStrings"); // find the type
var method = type.Methods.First(meth => meth.Name == "DecryptString"); // find the method in the
type type
.Methods.Remove(method); // remove the method
ass
.Write("fixed.exe"); // write assembly back

However, when I run this code, I get an exception saying:

System.ArgumentException: Member 'System.String DecryptString(System.String)' is declared in another module and needs to be imported


I have no idea what it wants from me... The method IS defined in that module, so what am I supposed to import?

What should I do to fix this and delete the method from the type?

Many thanks, I a noob, started using Cecil yesterday

Jb Evain

unread,
Feb 3, 2020, 4:56:38 PM2/3/20
to mono-...@googlegroups.com
Hey,

I think that the problem is that you delete the method definition, but not its usage. If a method calls your DecryptString method, you'll likely face this exception, because the method has been detached from the module, but it still is referenced in the assembly.

So you'll need to go over each method body and decide what to do with that invocation.

Jb

--
--
--
mono-cecil
---
You received this message because you are subscribed to the Google Groups "mono-cecil" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mono-cecil+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mono-cecil/6146c1b4-6087-4ae9-9318-36808d48a95c%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages