Hey Simon,

Sadly there's nothing in Cecil or in Cecil.Rocks to help you with that
right now. Also, the approach you posted won't work. You want to do a
deep clone of the method, which will take care of properly importing
all types in the module you're copying.

It means that you want to have:

var target_module = ...;

var new_method = new MethodDefinition (template.Name,
template.Attribute, target_module.Import (template.ReturnType));

new_methods.Parameters.Add (new ParameterDefinition
(target_module.Import (template.Parameters [0].ParameterType)));

And so on.

And it may actually be not complete enough. You might want to have a
special strategy for importing types. Let say you have a method
Foo::Bar in the module foo.dll. You want to clone it into Baz::Bar
into baz.dll. If the return type of the template method is a string,
you want to create a reference to string in baz.dll. But what if
Foo::Bar returns a type defined in foo.dll, say Bang ? You have to
decide whether you clone the whole Bang type into baz.dll, or simply
create a reference to Bang from foo into baz.dll.

So it's not simple, you have to know what you want and what you're doing :)

On Tue, Nov 2, 2010 at 8:14 AM, Simon Cropp <[email protected]> wrote:
> I am trying to copy a very simple method from one type to another Type
>
> So far I have this
>
>        private MethodDefinition CopyMethod(MethodDefinition
> templateMethod, TypeDefinition targetType)
>        {
>                        var newMethod = new 
> MethodDefinition(templateMethod.Name,
> templateMethod.Attributes, templateMethod.ReturnType);
>
>                foreach (var parameterDefinition in templateMethod.Parameters)
>                {
>                        newMethod.Parameters.Add(parameterDefinition);
>                }
>                foreach (var instruction in templateMethod.Body.Instructions)
>                {
>                        newMethod.Body.Instructions.Add(instruction);
>                }
>                targetType.Methods.Add(newMethod);
>                return newMethod;
>        }
>
> But I am getting a "Invalid Typeref token." when I try to execute the code.
>
> Any pointers?
>
> --
> --
> mono-cecil

-- 
--
mono-cecil

Reply via email to