Hey,
It's very simple, I just did it.
TestLib.dll:
namespace TestLib
{
public delegate void DoStuff();
// class MainTest
public void DoSomething(DoStuff stuff)
{
stuff();
}
}
TestExe.exe, Program.Main:
TestLib.MainTest.DoSomething(() => { Console.WriteLine("stuff!"); });
In the processing exe, you open TestExe.exe, copy TestLib.dll to the same
directory as the processing exe (so the resolver can find it), find and
resolve the reference for the TestLib.DoStuff type, and then clone it and
add it to the exe assembly, then write it to a new file.
The cloning method:
public static TypeDefinition CloneOnlyType(this TypeDefinition typeDef)
{
var newTypeDef = new TypeDefinition(typeDef.Namespace, typeDef.Name,
typeDef.Attributes, typeDef.BaseType);
foreach(TypeReference interfaceTypeRef in typeDef.Interfaces)
{
newTypeDef.Interfaces.Add(interfaceTypeRef.Resolve().Clone());
}
return newTypeDef;
}
public static TypeDefinition Clone(this TypeDefinition typeDef)
{
var newTypeDef = CloneOnlyType(typeDef);
foreach (MethodDefinition methodDef in typeDef.Methods)
{
newTypeDef.Methods.Add(methodDef.Clone());
}
foreach(FieldDefinition fieldDef in typeDef.Fields)
{
newTypeDef.Fields.Add(fieldDef.Clone());
}
return newTypeDef;
}
I also recall having a similar problem when you actually try to read the
body of the constructor of a custom delegate type. Maybe that's the same
thing, and if so, that's much easier to reproduce.
Thank you for the quick reply!
2010/6/30 Jb Evain <[email protected]>
> Hey,
>
> On Wed, Jun 30, 2010 at 7:30 PM, Shadow <[email protected]> wrote:
> > Everything works perfectly, until there is a custom delegate type, in
> > which case the program crashes with a NullReferenceException in
> > Mono.Cecil when the modified assembly is being written to the disk.
>
> That looks indeed weird. Could you please provide a way to reproduce this?
>
> Thanks!
>
> --
> Jb Evain <[email protected]>
>
> --
> --
> mono-cecil
--
--
mono-cecil