コード例 #1
0
        public bool TryDecode <T>(PyObject pyObj, out T value)
        {
            value = default;
            if (!typeof(T).IsEnum)
            {
                return(false);
            }

            Type etype = Enum.GetUnderlyingType(typeof(T));

            if (!PyInt.IsIntType(pyObj))
            {
                return(false);
            }

            object result;

            try
            {
                result = pyObj.AsManagedObject(etype);
            }
            catch (InvalidCastException)
            {
                return(false);
            }

            if (Enum.IsDefined(typeof(T), result) || typeof(T).IsFlagsEnum())
            {
                value = (T)Enum.ToObject(typeof(T), result);
                return(true);
            }

            return(false);
        }
コード例 #2
0
 public static object ToCLI(this PyObject o)
 {
     if (PySequence.IsSequenceType(o))
     {
         var list = new List <object>();
         foreach (PyObject subo in o)
         {
             list.Add(subo.ToCLI());
         }
         return(list);
     }
     if (PyString.IsStringType(o))
     {
         return(o.As <string>());
     }
     if (PyInt.IsIntType(o))
     {
         return(o.As <long>());
     }
     if (PyLong.IsLongType(o))
     {
         return(o.As <long>());
     }
     if (PyFloat.IsFloatType(o))
     {
         return(o.As <double>());
     }
     return(o);
 }
コード例 #3
0
ファイル: TestPyInt.cs プロジェクト: mcneel/pythonnet
        public void TestIsIntTypeFalse()
        {
            var s = new PyString("Foo");

            Assert.False(PyInt.IsIntType(s));
        }
コード例 #4
0
ファイル: TestPyInt.cs プロジェクト: mcneel/pythonnet
        public void TestIsIntTypeTrue()
        {
            var i = new PyInt(5);

            Assert.True(PyInt.IsIntType(i));
        }
コード例 #5
0
ファイル: Excel.cs プロジェクト: EcmaXp/execlib
 public static bool IsInt(this PyObject obj)
 {
     return(PyInt.IsIntType(obj));
 }