Get Value Of An Enum
See original GitHub issueEnvironment
- Pythonnet version: latest
- Python version: 3.9.4
- Operating System: Windows
- .NET Runtime:
Details
- Describe what you were trying to get done.
I would like to have the ability to extract the value of a .Net Enum
via python. One can do that using MyEnum.value__
, however it seems logical that you should do it using int(MyEnum)
.
- What commands did you run to trigger this issue? If you can provide a Minimal, Complete, and Verifiable example this will help us understand the issue.
import clr
clr.AddReference("System.Windows.Forms")
import System.Windows.Forms as WinForms
enum_value = WinForms.Keys.Control
print(f"{enum_value=}")
print(f"{enum_value.value__=}")
That prints:
enum_value=<System.Windows.Forms.Keys object at 0x000001C053E07580>
enum_value.value__=131072
- If there was a crash, please include the traceback here.
No crash.
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Get int value from enum in C# - Stack Overflow
Bottom; object val = Convert.ChangeType(side, side.GetTypeCode()); Console.WriteLine(val);. This should work regardless of the underlying ...
Read more >Enum.GetValues Method (System)
The GetValues method returns an array that contains a value for each member of the enumType enumeration. If multiple members have the same...
Read more >Fetching Values From Enum In C#
This blog looks at creating an enum and fetching two different types of values from it.
Read more >How to access the keys and values of an Enum (string and ...
How to access all the keys or values of a numeric enum? ... Simply using the "Object.values()" or "Object.keys()" method will return both...
Read more >How to get an enum value from a string value in Java
To get an enum value from a string value in Java, you can use the valueOf method of the enum type. Here's an...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
So if I understand you correctly, we all agree that adding a default
__int__
method for all C# enums is a wise idea?I would have implemented this ability myself, but I have zero knowledge in C#. Let me know if I can help you somehow make this ability happen.
If I remember correctly, we removed the code, that converted any enum value to an integer implicitly when passed to Python. You could never previously get raw enum value in Python, it always just saw integers. So the corresponding type never had
__int__
implementation.