You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, ModuleDefinition.LookupMember is a very generic function that returns a IMetadataMember when the call is successful. If the type of the member is already known (e.g., because the provided metadata token has a known table index), this often requires extra type-casts or type-checks at the call-site. This can make code less readable / concise:
if(module.TryLookupMember(token,outvar member)&& member is TypeDefinition type){/* ... Use `type` ...*}
Proposal
Add generic overloads LookupMember<T>(MetadataToken) and TryLookupMember<T>(MetadataToken, out T), that also try to cast the resolved member to the specified type parameter, such that the type inference of the C# compiler can take over, replacing the explicit or safe type-casts:
if(module.TryLookupMember(token,out TypeDefinition type)){/* ... Use `type` ...*}
Alternatives
C# Pattern matching is pretty decent nowadays and could be combined with the original versions of LookupMember, but this still is not as concise as it could be.
Additional Context
This is extra nice if it is used in a chain of calls, avoiding many parentheses. For example:
Problem Description
Currently,
ModuleDefinition.LookupMember
is a very generic function that returns aIMetadataMember
when the call is successful. If the type of the member is already known (e.g., because the provided metadata token has a known table index), this often requires extra type-casts or type-checks at the call-site. This can make code less readable / concise:Proposal
Add generic overloads
LookupMember<T>(MetadataToken)
andTryLookupMember<T>(MetadataToken, out T)
, that also try to cast the resolved member to the specified type parameter, such that the type inference of the C# compiler can take over, replacing the explicit or safe type-casts:Alternatives
C# Pattern matching is pretty decent nowadays and could be combined with the original versions of
LookupMember
, but this still is not as concise as it could be.Additional Context
This is extra nice if it is used in a chain of calls, avoiding many parentheses. For example:
versus:
The text was updated successfully, but these errors were encountered: