Python.Runtime.ManagedType.GetManagedObject C# (CSharp) Method

GetManagedObject() static private method

static private GetManagedObject ( IntPtr ob ) : ManagedType
ob System.IntPtr
return ManagedType
	internal static ManagedType GetManagedObject(IntPtr ob) {
	    if (ob != IntPtr.Zero) {
		IntPtr tp = Runtime.PyObject_TYPE(ob);
		if (tp == Runtime.PyTypeType || tp == Runtime.PyCLRMetaType) {
		    tp = ob;
		}
		
		int flags = (int)Marshal.ReadIntPtr(tp, TypeOffset.tp_flags);
		if ((flags & TypeFlags.Managed) != 0) {
		    IntPtr op = (tp == ob) ?
			Marshal.ReadIntPtr(tp, TypeOffset.magic()) :
			Marshal.ReadIntPtr(ob, ObjectOffset.magic());
		    GCHandle gc = (GCHandle)op;
		    return (ManagedType)gc.Target;
		}

		// In certain situations, we need to recognize a wrapped
		// exception class and be willing to unwrap the class :(

		if (Runtime.wrap_exceptions) {
		    IntPtr e = Exceptions.UnwrapExceptionClass(ob);
		    if ((e != IntPtr.Zero) && (e != ob)) {
			ManagedType m = GetManagedObject(e);
			Runtime.Decref(e);
			return m;
		    }
		}
	    }
	    return null;
	}

Usage Example

Example #1
0
        public new static int tp_setattro(IntPtr ob, IntPtr key, IntPtr val)
        {
            var managedKey = Runtime.GetManagedString(key);

            if ((settableAttributes.Contains(managedKey)) ||
                (ManagedType.GetManagedObject(val)?.GetType() == typeof(ModuleObject)))
            {
                var self = (ModuleObject)ManagedType.GetManagedObject(ob);
                return(Runtime.PyDict_SetItem(self.dict, key, val));
            }

            return(ExtensionType.tp_setattro(ob, key, val));
        }
All Usage Examples Of Python.Runtime.ManagedType::GetManagedObject