Hi Jb Evain.
Many thanks for your reply. Unfortunately, it still doesn't work. The
problem seems to be that the MethodReference generated by your
MakeGeneric() method has a wrong metadata token.
To show you exactly what I am doing, I have pasted some example code
here:
http://pastebin.com/d05cPexn
If you run this code, it will use Mono.Cecil to generate a new
assembly (output.exe). If you run that output file, it prints the
following error message:
Declaration token used in a method implementation is out of range.
Also, if you look at this new assembly in ildasm and navigate to
MyClass.InterfaceMethod(), you can see this:
.override 'INVALID TOKEN: 0x06000000'
Is there a way to fix this?
Thanks again for any help,
Timwi
On Mar 24, 5:21 pm, Jb Evain <[email protected]> wrote:
> Hey,
>
> On Wed, Mar 24, 2010 at 6:10 PM, Timwi <[email protected]> wrote:
> > To do this, I need to add (to the class method's Overrides) the
> > MethodReference pointing to MyInterface<string>.InterfaceMethod(). How
> > do I get it?
>
> static TypeReference MakeGenericType (TypeReference type, params
> TypeReference [] arguments)
> {
> if (type.GenericParameters.Count != arguments.Length)
> throw new ArgumentException();
>
> var instance = new GenericInstanceType (type);
> foreach (var argument in arguments)
> instance.GenericArguments.Add (argument);
>
> return instance;
>
> }
>
> static MethodReference MakeGeneric (MethodReference method,
> TypeReference declaringType)
> {
> var reference = new MethodReference (method.Name, declaringType,
> method.ReturnType.ReturnType, method.HasThis, method.ExplicitThis,
> MethodCallingConvention.Generic);
> foreach (var parameter in method.Parameters)
> reference.Parameters.Add (new ParameterDefinition
> (parameter.ParameterType));
>
> return reference;
>
> }
>
> //
>
> TypeReference my_iface_of_t = ...;
> TypeReference string_ref;
>
> TypeReference my_iface_of_string = MakeGenericType (my_iface_of_t,
> string_ref);
>
> MethodReference my_iface_of_t_ifacemethod = ...;
>
> MethodReference my_iface_of_string_ifacemethod = MakeGeneric
> (my_iface_of_t_iface_method, my_iface_of_string);
>
> --
> Jb Evain <[email protected]>
--
--
mono-cecil
To unsubscribe from this group, send email to
mono-cecil+unsubscribegooglegroups.com or reply to this email with the words
"REMOVE ME" as the subject.