2

Why are .NET enums type casted to int?

E.g.: I want to generate an array of Objects where the first element is an Int32, the second element is an Double and the third element is an enumeration of the type DateTimeKind. See the following code:

import clr
from System import Array, DateTimeKind, Double, Enum, Int32, Object
a = Array[Object]([None, None, None])
a[0] = Int32(127)
a[1] = Double(12.4)
a[2] = DateTimeKind.Utc

But now the third element is of type int and not DateTimeKind.

I need the array a as argument when invoking a method (reflection, see https://stackoverflow.com/a/49997219/7556646).

I'm as well aware that I can generate a b = List[DataTimeKind]() and add elements to that list, see https://stackoverflow.com/a/44088399/7556646. The list b has the right type but when I access an element of the list b[0] then it's again an int.

So what can I do to cast an int to an enum?

Wollmich
  • 1,616
  • 1
  • 18
  • 46
  • From [msdn](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/enum). "_An enumeration type (or enum type) is a value type defined by a set of named constants of the underlying integral numeric type. By default, the associated constant values of enum members are of type int; _". In C# the real type of a enum can be `byte`, `sbyte`, `short`, `ushort`, `int`, `uint`, `long` or `ulong`. – Drag and Drop Jan 20 '21 at 08:13
  • @DragandDrop: I'm aware of that. But assume I want to call a method `void Test(DateTimeKind arg)` when a second method with the same name exists e.g.: `void Test(double arg)`. I want to call the method with the enum argument. In that case pythonnet says no method match given arguments (int). – Wollmich Jan 20 '21 at 09:02
  • 1
    Sorry I'm not from the python world. I took "_Why are .NET enums type casted to int?_" to the C# tag. Given that `DateTimeKind ` is an int [ref](https://referencesource.microsoft.com/#mscorlib/system/datetimekind.cs,f7567dbc80085e11). It looked natural that it was cast to int. I was not teaching nor pendentric, nor trying to answer a domain I have no knowledge about. – Drag and Drop Jan 20 '21 at 09:12
  • 1
    The only info i have is this github issue https://github.com/pythonnet/pythonnet/issues/935, and related issue. I do not have the Py background to try or understand it tho. – Drag and Drop Jan 20 '21 at 09:13
  • @DragandDrop I saw that github issue as well. But the method I want to call has two arguments of two different types of enums and it exists with the same name and different arguments. So this workaround doesn't help. So I tried to use reflection to call the method. I can choose the right method I want but I've to pass an array of objects where some of the elements need to be of the type enum and not int. – Wollmich Jan 20 '21 at 09:17
  • 1
    that related issue as multiple argument in the title https://github.com/pythonnet/pythonnet/issues/1099 – Drag and Drop Jan 20 '21 at 09:19
  • 1
    So it looks that there is no workaround, `I think we should disable implicit .NET enum <-> Python int conversion in 3.0`. – Wollmich Jan 20 '21 at 09:28
  • 1
    Pinging/suscribing/showing interest on the github issue, may help having a path for this. For now there is no pull request solving this. Perhaps you can change the C# signature to accept int and do the converstion on C# side. – Drag and Drop Jan 20 '21 at 09:59
  • @DragandDrop The C# signature is from an external product and I would like to avoid to write a helper C# assembly. – Wollmich Jan 20 '21 at 10:02
  • Add a C# ddl that consume that part of the external product. It's either that or getting a contributor badge. Btw for that comment chain I think that this question has an answer. and quote the contributor and related issue. Then we will cleanup our comment our self. Before the next Thanos snap – Drag and Drop Jan 20 '21 at 10:09

1 Answers1

1

This not possible with pythonnet at the moment because pythonnet converts .NET enums to int, see https://github.com/pythonnet/pythonnet/issues/1220.

.NET enums should not be automatically converted to Python int and back

The only workaround would be to add a C# helper dll and call this helper dll from pythonnet.

Wollmich
  • 1,616
  • 1
  • 18
  • 46