I have been playing with Reflector and Reflexil, but when I created a static method, it didn't work. On some ispection, I found that methods have 2 different parameters/flags: IsStatic
and HasThis
.
What is the diference? Or is there no difference and one of the flags is simply unused? I have looked at extension methods and constructors, however extension methods are marked as normal static methods, and constructors as normal member methods (in regard to these 2 flags).
Reflexil displays together two sets of flags - method atttributes from the method definition in case of IsStatic, and calling conventions from the method signature definition in case of HasThis.
Method attributes contain general information about a specific method, like its accessibility, abstract/virtual/sealed status etc. (e.g. Static
), while the signature is what the method takes and returns and how, which can be separated into several calling conventions.
The managed calling conventions are DEFAULT
, VARARG
, HASTHIS
and EXPLICITTHIS
. HASTHIS
(instance
in CIL) simply means that the this
instance is internally passed as the first argument to the method (referenced by ldarg.0
).
HASTHIS
is useful in places where you would only be able to use a method signature, like in function pointers or the calli
instruction (both unavailable in C#, though not in C++/CLI). However, EXPLICITTHIS
would be more appropriate on these occasions.
The Static
flag may not be needed for methods, but is certainly required for fields, as they have no calling convention, so probably consistency is the reason.
So, conceptually they are a bit different, but technically mean the same thing.
While in theory a non-static method may not need a this
reference, setting both flags at the same time is prohibited, and ilasm doesn't allow me to construct a method with both flags on or off, setting them both based only on the presence of the static
keyword.
Extension methods are only a C# thing, the "this" reference is the actual first parameter of the method and the rest is syntactic sugar.