GetMember by MetadataToken

2019-09-04 06:03发布

问题:

What is the proper way of getting a member of a type using a metadata token of that member (with reflection)?

I've tried:

MemberInfo mi = type.GetMembers(bindingFlags).FirstOrDefault(m => m.MetadataToken == metadataToken);

回答1:

The Module class provides a set of Resolve methods that can find the member, e.g.

MemberInfo mi = type.Module.ResolveMember(metadataToken);

Edit: ResolveMember cannot resolve properties and events, so the originally suggested approach has to be used.



标签: c# reflection