Why is there no .NET RuntimePropertyHandle and PropertyInfo.GetPropertyFromHandle?

56 views Asked by At

I've seen the following .NET types and functions for Type, FieldInfo, and MethodInfo:

RuntimeTypeHandle th = ...;
Type t = Type.GetTypeFromHandle(th);

RuntimeFieldHandle fh = ...;
FieldInfo f = FieldInfo.GetFieldFromHandle(fh);

RuntimeMethodHandle mh = ...;
MethodInfo m = MethodBase.GetMethodFromHandle(mh);

However, there doesn't appear to be a corresponding handle type and Get...FromHandle for properties. Is there one I'm not seeing, or is there some good reason why these don't exist?

It seems very inefficient to have to lookup the PropertyInfo using Type.GetProperty when I already have access to property tokens for the properties I'm interested in.

1

There are 1 answers

0
IS4 On BEST ANSWER

As coreclr shows, properties and events have no handles, therefore no Runtime...Handle or Get...FromHandle for them. Thinking of a "handle" is simply invalid in their context.

As to why, I guess the reason is that properties and events are purely metadata things, while methods and fields are much more. There are CIL opcodes for accessing methods, fields and types, but none for properties or events. Using those is compiled to CIL as using one of their respective accessor methods. They are literally nothing more than pieces of metadata for the runtime, so they need no handles.

I have myself asked a similar question, and there are really limited number of resolving methods for properties and events. As for your underlying question, I suppose enumerating over all properties and looking for MetadataToken might be more efficient than using GetProperty, but I'd rather test that.