So today I was browsing around ILSpy to get a better understanding of how .NET performs DllImports on external methods, when I came across something odd:
When searching for references to the enum value PInvokeImpl
, which is defined in the System.Reflection.MethodAttributes
enumeration, I noticed a matching definition in System.Reflection.FieldAttributes
.
Well sure enough, this appears to be more than just a behind-the-scenes, re-use of enumeration values: System.Reflection.FieldInfo
has an publicly defined property called IsPinvokeImpl
, which specifically checks if this implementation flag is set.
Interestingly enough, the MethodInfo
class doesn't even have this property - it must be determined from the MethodImplementationFlags
property instead.
Question:
Is it actually possible for a field to be PInvoke implemented, or is this just a stub implementation in the .NET framework for balance between field decorations and method decorations?
If it is possible, can it be done in C#, or is this a feature that requires C++/CLI?