Replies: 1 comment 2 replies
-
Wouldn't it be more performant to just cache the relevant data in something like a You could consider proposing a new reflection API that gets the |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Currently, in order to get an attribute from an enum value you have to use some relatively arcane code that harks back to the days of .NET Framework 2.0 (example from https://stackoverflow.com/a/9276348/70345):
with usage:
While this can be refactored to be somewhat more concise using newer C# features like the null-propagating operator, as far as I'm aware the underlying mechanism for accomplishing this remains the same with the latest version of C#:
MemberInfo
pointing to the enum value by converting that value to a stringGetCustomAttributes
on theMemberInfo
, passing in the type of the desired attributenull
if noneWhile this is not particularly onerous, I'm also pretty sure that almost every company in the world with a C# codebase has some variant of the above method in it somewhere. It definitely feels like functionality that should be in the BCL, not just because of the commonality of the need for something like this, but also because my assumption is that the method as presented is not particularly performant (which is called out in an answer on the same SO question). And calling
ToString
to lookup aMemberInfo
just feels yucky.Anyhow, is the provided code still the simplest way to accomplish this functionality today, or are there new features of C# that could make it simpler, more concise and more performant?
Beta Was this translation helpful? Give feedback.
All reactions