ltrzesniewski / InlineIL.Fody

Inject arbitrary IL code at compile time.
MIT License
240 stars 17 forks source link

Loading of invalid method token #18

Closed sakno closed 4 years ago

sakno commented 4 years ago

Hi Lucas!

I found interesting issue when trying to obtain RuntimeMethodHandle:

Ldtoken(PropertyGet(Type<IReadOnlyList<T>>(), "Item"));
Pop(out RuntimeMethodHandle handle);
var getter = MethodBase.GetMethodFromHandle(handle);

The exception is

System.ArgumentException : Cannot resolve method Int64 get_Item(Int32) because the declaring type of the method handle System.Collections.Generic.IReadOnlyList`1[T] is generic. Explicitly provide the declaring type to GetMethodFromHandle.

There are two possible roots of this problem:

sakno commented 4 years ago

The method reference in IL inserted as

instance !0 [netstandard]System.Collections.Generic.IReadOnlyList`1<!!T>::get_Item(int32)

Maybe it should be as follows:

instance !!T [netstandard]System.Collections.Generic.IReadOnlyList`1<!!T>::get_Item(int32)
sakno commented 4 years ago

Closing this task, I found an issue: GetMethodFromHandle requires the second argument for the method handle obtained from the generic type.

ltrzesniewski commented 4 years ago

Hi,

I suppose you already figured it out, but given the error message, I'd expect the following to work (not tested though):

Ldtoken(PropertyGet(typeof(IReadOnlyList<>), "Item"));
sakno commented 4 years ago

Here is the correct code:

Ldtoken(PropertyGet(Type<IReadOnlyList<T>>(), "Item"));
Pop(out RuntimeMethodHandle method);
Ldtoken(Type<IReadOnlyList<T>>());
Pop(out RuntimeTypeHandle type);
var getter = MethodBase.GetMethodFromHandle(method, type);