domingo, 12 de enero de 2014

[.NET Reflection] Adding "SuppressIldasmAttribute" with Mono.Cecil

Hello,
"Mono.Cecil" is a fantastic library for editing the .NET applications which are already compiled. I'm going to show how to add the "SuppressIldasmAttribute" which does this:


















This can be easily done putting "<Assembly: SuppressIldasm()>" on the top of any class. But what about putting it with Mono.Cecil for an assembly we don't have the source code?
Follow this instructions:
1º Load with "AssemblyDefinition" any file or Byte[] and put it on a variable.
Dim my_sample As AssemblyDefinition = AssemblyDefinition.ReadAssembly("A file/Byte[]")
 2º We need to get the contructor of "SuppressIldasmAttribute" on "System.Runtime.CompilerServices". So do this:
Dim constructor As MethodReference = a.MainModule.Import(GetType(SuppressIldasmAttribute).GetConstructor(New System.Type() {}))
3º Add this constructor (.ctor) to the "CustomAttributes" of the assembly:
my_sample.CustomAttributes.Add(New CustomAttribute(constructor))
Now it's done! It will appear the avobe message when anyone try to dissassemble with IlDasm.

Enjoy!
LordCoder//REiS

No hay comentarios:

Publicar un comentario