示例#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 (!PyLong.IsLongType(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
        public bool TryDecode <T>(PyObject pyObj, out T value)
        {
            if (!PyLong.IsLongType(pyObj))
            {
                value = default;
                return(false);
            }

            using (var pyLong = PyLong.AsLong(pyObj))
            {
                value = (T)(object)pyLong.ToBigInteger();
                return(true);
            }
        }
        public void TestIsLongTypeFalse()
        {
            var s = new PyString("Foo");

            Assert.False(PyLong.IsLongType(s));
        }
        public void TestIsIntTypeTrue()
        {
            var i = new PyLong(5);

            Assert.True(PyLong.IsLongType(i));
        }