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