PInvoke Implementation on Fields
Asked Answered
S

2

6

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?

Spermic answered 3/6, 2015 at 12:53 Comment(0)
D
7

When you look at the MSDN description for FieldAttributes then you'll see it documented as "Reserved for future use". That future has not arrived yet so its intention is not nailed down.

Types like FieldAttributes are not arbitrary, they follow the CLI specification. Ecma-335 nails down what the metadata in a .NET assembly needs to look like and how it should be interpreted. This document does reveal an interesting quirk.

Chapter II.16.1 describes the field attributes, you'll see a close match between the metadata tokens and the FieldAttributes enum. Note however that pinvokeimpl is missing in that chapter.

Chapter II.23.1.5 gives the specific values of the attributes, it has PInvokeImpl with the value 0x2000. Description is "Implementation is forwarded through PInvoke". Compare to II.23.1.10, describes method attributes. It has many values in common with field attributes.

This looks a lot like a copy/paste bug :)

Digging a bit deeper through the .NET Framework source code, the CLR and jitter only ever considers pinvokeimpl on methods. The C# compiler however appears to be based on the CLI spec and actually sets the attribute. Appears in emit.cpp, RegMeta::_DefinePinvokeMap() function, it will set the attribute if this function is called for a field instead of a method. That never actually happens.

Demodulation answered 3/6, 2015 at 13:46 Comment(0)
R
3

From FieldAttributes:

PinvokeImpl Reserved for future use.

So I would say:

Q: Is it actually possible for a field to be PInvoke implemented

A: No

Republicanism answered 3/6, 2015 at 13:22 Comment(1)
+1 for being the first to correctly answer the question, but I'm going to have to accept Hans' response, because of the extra insight on the ECMA specifications and the .NET source codeSpermic

© 2022 - 2024 — McMap. All rights reserved.