Python.Runtime.Converter.ToManagedValue C# (CSharp) Метод

ToManagedValue() статический приватный Метод

static private ToManagedValue ( IntPtr value, Type obType, Object &result, bool setError ) : bool
value System.IntPtr
obType System.Type
result Object
setError bool
Результат bool
	internal static bool ToManagedValue(IntPtr value, Type obType, 
				      out Object result, bool setError) {
	    // Common case: if the Python value is a wrapped managed object
	    // instance, just return the wrapped object.
	    ManagedType mt = ManagedType.GetManagedObject(value);
	    result = null;

	    // XXX - hack to support objects wrapped in old-style classes
	    // (such as exception objects).
	    if (Runtime.wrap_exceptions) {
	    if (mt == null) {
		if (Runtime.PyObject_IsInstance(
                            value, Exceptions.Exception
		            ) > 0) {
		    IntPtr p = Runtime.PyObject_GetAttrString(value, "_inner");
		    if (p != IntPtr.Zero) {
			// This is safe because we know that the __dict__ of
			// value holds a reference to _inner.
			value = p;
			Runtime.Decref(p);
			mt = ManagedType.GetManagedObject(value);
		    }
		}
		IntPtr c = Exceptions.UnwrapExceptionClass(value);
		if ((c != IntPtr.Zero) && (c != value)) {
		    value = c;
		    Runtime.Decref(c);
		    mt = ManagedType.GetManagedObject(value);
		}
	    }
	    }

	    if (mt != null) {
		if (mt is CLRObject) {
		    object tmp = ((CLRObject)mt).inst;
		    if (obType.IsInstanceOfType(tmp)) {
			result = tmp;
			return true;
		    }
		    string err = "value cannot be converted to {0}";
		    err = String.Format(err, obType);
		    Exceptions.SetError(Exceptions.TypeError, err);
		    return false;
		}
		if (mt is ClassBase) {
		    result = ((ClassBase)mt).type;
		    return true;
		}
		// shouldnt happen
		return false;
	    }

	    if (value == Runtime.PyNone && !obType.IsValueType) {
		result = null;
		return true;
	    }

	    if (obType.IsArray) {
		return ToArray(value, obType, out result, setError);
	    }

	    if (obType.IsEnum) {
		return ToEnum(value, obType, out result, setError);
	    }

	    // Conversion to 'Object' is done based on some reasonable
	    // default conversions (Python string -> managed string, 
	    // Python int -> Int32 etc.).

	    if (obType == objectType) {
		if (Runtime.IsStringType(value)) {
		    return ToPrimitive(value, stringType, out result, 
				       setError);
		}

		else if (Runtime.PyBool_Check(value)) {
		    return ToPrimitive(value, boolType, out result, setError);
		}

		else if (Runtime.PyInt_Check(value)) {
		    return ToPrimitive(value, int32Type, out result, setError);
		}

		else if (Runtime.PyLong_Check(value)) {
		    return ToPrimitive(value, int64Type, out result, setError);
		}

		else if (Runtime.PySequence_Check(value)) {
		    return ToArray(value, typeof(object[]), out result, 
				   setError);
		}

		if (setError) {
		    Exceptions.SetError(Exceptions.TypeError,
					"value cannot be converted to Object"
					);
		}

		return false;
	    }

	    return ToPrimitive(value, obType, out result, setError);

	}

Usage Example

Пример #1
0
        private static ExceptionDispatchInfo?TryGetDispatchInfo(BorrowedReference exception)
        {
            if (exception.IsNull)
            {
                return(null);
            }

            var pyInfo = Runtime.PyObject_GetAttrString(exception, Exceptions.DispatchInfoAttribute);

            if (pyInfo.IsNull())
            {
                if (Exceptions.ExceptionMatches(Exceptions.AttributeError))
                {
                    Exceptions.Clear();
                }
                return(null);
            }

            try
            {
                if (Converter.ToManagedValue(pyInfo, typeof(ExceptionDispatchInfo), out object result, setError: false))
                {
                    return((ExceptionDispatchInfo)result);
                }

                return(null);
            }
            finally
            {
                pyInfo.Dispose();
            }
        }
All Usage Examples Of Python.Runtime.Converter::ToManagedValue