-
-
Notifications
You must be signed in to change notification settings - Fork 18
Added ToShortNames and DisplayShortNames dictionary #139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
ac0afb0
cdd39b8
20b447e
259f7a5
d3328d7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -21,9 +21,9 @@ namespace Console.Test.Benchmark; | |||||
[EnumGenerator] | ||||||
public enum UserType | ||||||
{ | ||||||
[Display(Name = "مرد", Description = "men")] Men, | ||||||
[Display(Name = "مرد", Description = "men", ShortName = "M")] Men, | ||||||
|
||||||
[Display(Name = "زن", Description = "women")] Women, | ||||||
[Display(Name = "زن", Description = "women", ShortName = "W")] Women, | ||||||
|
||||||
//[Display(Name = "نامشخص")] | ||||||
None | ||||||
|
@@ -97,6 +97,18 @@ public string FastToDisplay() | |||||
return UserType.Men.ToDisplayFast(); | ||||||
} | ||||||
|
||||||
[Benchmark] | ||||||
public string NativeToShortName() | ||||||
{ | ||||||
return UserType.Men.ToShortNameNative(); | ||||||
} | ||||||
|
||||||
[Benchmark] | ||||||
public string FastToShortName() | ||||||
{ | ||||||
return UserType.Men.ToShortNameFast(); | ||||||
} | ||||||
|
||||||
[Benchmark] | ||||||
public string NativeToDescription() | ||||||
{ | ||||||
|
@@ -173,6 +185,19 @@ public static string ToDisplayNative(this Enum value) | |||||
|
||||||
return propValue?.ToString(); | ||||||
} | ||||||
|
||||||
public static string ToShortNameNative(this Enum value) | ||||||
{ | ||||||
if (value is null) | ||||||
throw new ArgumentNullException(nameof(value)); | ||||||
var attribute = value.GetType().GetField(value.ToString())? | ||||||
.GetCustomAttributes<DisplayAttribute>(false).FirstOrDefault(); | ||||||
if (attribute == null) | ||||||
return value.ToString(); | ||||||
var propValue = attribute.GetType().GetProperty("ShortName")?.GetValue(attribute, null); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider extracting the string literal "ShortName" into a constant to improve maintainability and avoid potential typos.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This suggestion does not match the conventions of the tests around it |
||||||
return propValue?.ToString(); | ||||||
} | ||||||
|
||||||
public static string ToDescriptionNative(this Enum value) | ||||||
{ | ||||||
if (value is null) | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider extracting the string literal "ShortName" into a constant to improve maintainability and avoid potential typos.
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This suggestion does not match the rest of the conventions in the if statements around it