Reputation:
I'm making a silly AOT .net compiler for fun, and ran into a problem.
I'm just loading the assembly into memory (I'm writing it in C#) and spamming reflection left and right to get the information I need (such as the CIL for the method bodies).
This page says "We need the reference to the current instance (stored in local argument index 0) [...]". However, when I call MethodInfo.GetParameters()
, this argument is not returned.
I'm resolving the fields in opcodes such as Ldarg
to ParameterInfo objects, and not raw indices, so it gets very confused when "Ldarg.0" is inside an instance method - since arg 0 is not in GetParameters
!
My main question: Is there some way I can get an instance of the ParameterInfo
object for the this
object (parameter index 0), or do I have to just use raw indices? (I really don't want to use int indices...)
Here's some code, since code is nice. (contained inside the class Program)
static void Main(string[] args)
{
// obviously throws an IndexOutOfRangeException instead of returning the (true) argument 0
Console.WriteLine(typeof (Program).GetMethod("Test").GetParameters()[0]);
}
public void Test()
{
}
Upvotes: 4
Views: 2437
Reputation: 1062770
You don't get a ParameterInfo for that. Simply: if it is an instance method, there is a "this" that maps to the arg-0 of the method's declaring type. It has no name and no interesting properties other than its type. All other parameters are offset by one. For static methods this is not the case. Note that this gets even more interesting for instance methods on value-types where it is a by-ref argument.
Upvotes: 2