Just to add context what Jon Skeet said: even in IL it isn't possible. Let's say, for kicks, you were able to obtain the MetadataToken for a property, by writing your own compiler, and injecting the property's metadata token into the attribute. At runtime you wouldn't be able to ask the module, in which the property is from, to resolve the metadata token; you'd receive this message:
This API does not support PropertyInfo tokens.
A way to test this is to get the PropertyInfo
of any property, and ask the module which defines the type containing the property to ResolveMember
, which accepts an int
value of the metadata token, and feed it the PropertyInfo.MetadataToken
:
Console.WriteLine(propInfo.DeclaringType.Module.ResolveMember(propInfo.MetadataToken));
The API wasn't designed for that purpose, properties are themselves just metadata that rides along with your program to help associate methods to a common context for use with compilers and IDEs. Once the compiler takes your code and does its magic, the property never shows up once in IL.
If you could push the property's MetadataToken
into the attribute, the best thing you could do would be to also push the type's MetadataToken
into the attribute, and have it retrieve the PropertyInfo
object via means of iterating the type's properties, and the one with the matching MetadataToken
would be your target.
It's crucial to understand that MetadataToken
s are only unique within the scope of the module they exist within. I would say assembly, but multiple module assemblies kill that idea.
I only know as much as this from writing an ECMA-335 Metadata Parser.