Conversation
|
Can you please make the format similar to Python's >>> from enum import Enum
>>> class Color(Enum):
... RED = 1
... GREEN = 2
...
>>> repr(Color.GREEN)
'<Color.GREEN: 2>'I suggest to use decimal values when enum is a regular enum, and either binary or hexadecimal when it has P.S. Disregard the Mac and ARM failures, they are caused by our test infra. |
|
@lostmsu Done - but I'm not really that happy with the Flags case. |
|
@koubaa both of course. |
|
@lostmsu done. The switch case isn't so pretty, but I couldn't think of a way to improve it. here's a minimal compiler explorer link with that logic if anyone wants to try and see if they can improve it: https://godbolt.org/z/dYG6sdnKo |
|
@koubaa try using Convert.ChangeType string ConvertFlags(Enum value) {
Type primitiveType = value.GetType().GetEnumUnderlyingType();
string format = "X" + (Marshal.SizeOf(primitiveType) * 2).ToString(CultureInfo.InvariantCulture);
var primitive = (IFormattable)Convert.ChangeType(value, primitiveType);
return primitive.ToString(format, null);
}
string ConvertValue(Enum value) {
Type primitiveType = value.GetType().GetEnumUnderlyingType();
return Convert.ChangeType(value, primitiveType).ToString()!;
} |
mirror of upstream PR: pythonnet/pythonnet#2239
What does this implement/fix? Explain your changes.
Proposal to fix #2238
Before the changes:
IronPython (as a reference):
With these changes:
Checklist
AUTHORSCHANGELOG