Many thanks, this did indeed fix the problem.

However, to make sure that I don't keep running into this, I would
like to understand this better. Could you explain to me exactly what I
was doing wrong? You say I was using a MethodDefinition from corlib,
but I don't see how — I was only using
IEnumerableGetEnumerator.ReturnType, which I agree is from corlib, but
shouldn't module.Import() then import it?

Also — I can kind of understand why your solution works, but how do
you come up with it? It is unintuitive to me that I should have to
fiddle with the DeclaringType property of the reference I got
from .Import() instead of constructing a new reference cleanly — it
feels kind of subversive. Does this have something to do with the way
metadata tokens specify the relationship between types and methods,
and if so, what exactly?

Many thanks in advance!
Timwi


On May 18, 1:28 pm, Jb Evain <[email protected]> wrote:
> On Tue, May 18, 2010 at 10:09 AM, Timwi <[email protected]> wrote:
> > What am I doing wrong?
>
> I changed your code to:
>
>        var self = AssemblyDefinition.ReadAssembly(
>            Assembly.GetExecutingAssembly().Location);
>        var TestU = self.MainModule.Types.Single(
>            t => t.FullName == "Test`1");
>
>        // This gets me the method declared above, to which I want to
>        // add an explicit override declaration
>        var TestUGetEnumerator = TestU.Methods.Single(
>            t => t.Name == "GetEnumerator");
>
>        var IEnumerableU = TestU.Interfaces.Single(i => i.FullName
>            .StartsWith("System.Collections.Generic.IEnumerable`1"));
>        var IEnumerable = IEnumerableU.GetElementType().Resolve();
>
>        // This gets me the right method in the original,
>        // non-constructed generic IEnumerable<T> type
>        var IEnumerableGetEnumerator = IEnumerable.Methods.Single(
>            m => m.Name == "GetEnumerator");
>
>            var IEnumerableGetEnumerator_ref = self.MainModule.Import
> (IEnumerableGetEnumerator);
>
>            var instance = new GenericInstanceType
> (IEnumerableGetEnumerator_ref.DeclaringType);
>            instance.GenericArguments.Add (TestU.GenericParameters [0]);
>            IEnumerableGetEnumerator_ref.DeclaringType = instance;
>
>        // Try to add the override!
>        TestUGetEnumerator.Overrides.Add(IEnumerableGetEnumerator_ref);
>
>        self.Write("Temp_out.exe");
>
> To make it work.
>
> You can't use directly a non imported method from another assembly. In
> this case you were using the MethodDefinition from the corlib in your
> module. Also you need to specify the properly constructed declaring
> type for a generic method reference.
>
> --
> Jb Evain  <[email protected]>
>
> --
> --
> mono-cecil

-- 
--
mono-cecil

Reply via email to