question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Get Value Of An Enum

See original GitHub issue

Environment

  • 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).

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:closed
  • Created 2 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
saroad2commented, Nov 29, 2021

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.

1reaction
lostmsucommented, Nov 23, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found