ILSpy icon indicating copy to clipboard operation
ILSpy copied to clipboard

Cannot decompile ldtoken instruction

Open dgrunwald opened this issue 8 years ago • 3 comments

There's no straightforward translation of ldtoken to C# (unless it's a type token -> typeof), but we should try to figure out some way of representing these.

See #892 for previous discussion. As for why selecting the right method can be tricky, see https://stackoverflow.com/questions/3631547/select-right-generic-method-with-reflection

dgrunwald avatar Oct 13 '17 08:10 dgrunwald

See https://github.com/dotnet/csharplang/issues/191

siegfriedpammer avatar Jul 18 '18 07:07 siegfriedpammer

Note that ldtoken and ldftn are slightly different issues. In particular for ldftn there's another proposal: https://github.com/dotnet/csharplang/blob/master/proposals/function-pointers.md

We could just start emitting code according to one of these proposals, and hope that they get implemented at some point in time.

Alternatively, this idea should still work:

I think a possible way to translate ldtoken for now would be: new DelegateType(Method).MethodInfo.MethodHandle where DelegateType is a delegate that matches the method's signature (if there's no ref/out involved, Action<>/Func<> will do the job; otherwise the decompiler will have to declare a new delegate). Only problem is if it's an instance method belonging to another class, as we need an instance for creating a delegate.

dgrunwald avatar Aug 28 '19 17:08 dgrunwald

I guess a different idea would be to use a lambda: LdToken((InstanceType x) => x.Method(default(ParamType1), ...)) I think it might even be possible to provide a real implementation for LdToken by extracting the token via expression tree.

dgrunwald avatar Apr 28 '22 09:04 dgrunwald