1

Is it possible to do something like this?

public enum Letter { A, B, C }
// Suppose I have a scope already set up for Iron Python
scope.SetVariable("Letter", Letter)

This, of course, is wrong, but it's in hopes of achieving something like this in python:

print(Letter.A) => A

I've tried this

scope.SetVariable("Letter", DynamicHelpers.GetPythonTypeFromType(typeof(Letter)))

But in python I end up have to do this:

print(Letter().A)

Which seems wrong because it looks like I'm instantiating a class. Is there a way to get it so Letter.A works?

Luca
  • 171
  • 1
  • 13

1 Answers1

1

You must be doing something else wrong that you haven't shown because it works fine for me.

var engine = Python.CreateEngine();
var scope = engine.CreateScope();
scope.SetVariable("Letter", DynamicHelpers.GetPythonTypeFromType(typeof(Letter)));
engine.Execute(@"
from System import Enum
print(Letter.A)
print(Letter.B)
print(Letter.C)
print(Enum.GetNames(Letter))
print(dir(Letter))
", scope);
A
B
C
Array[str](('A', 'B', 'C'))
['A', 'B', 'C', 'CompareTo', 'Equals', 'Format', 'GetHashCode', 'GetName', 'GetNames', 'GetType', 'GetTypeCode', 'GetUnderlyingType', 'GetValues', 'HasFlag', 'IsDefined', 'MemberwiseClone', 'Parse', 'ReferenceEquals', 'ToBoolean', 'ToByte', 'ToChar', 'ToDateTime', 'ToDecimal', 'ToDouble', 'ToInt16', 'ToInt32', 'ToInt64', 'ToObject', 'ToSByte', 'ToSingle', 'ToString', 'ToType', 'ToUInt16', 'ToUInt32', 'ToUInt64', 'TryParse', '__and__', '__class__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__invert__', '__le__', '__lt__', '__ne__', '__new__', '__nonzero__', '__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__xor__', 'value__']
Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272