Python.Runtime.ModuleObject.GetAttribute C# (CSharp) Метод

GetAttribute() публичный Метод

public GetAttribute ( string name ) : ManagedType
name string
Результат ManagedType
	public ManagedType GetAttribute(string name) {
	    Object ob = this.cache[name];
	    if (ob != null) {
		return (ManagedType) ob;
	    }

	    string qname = (_namespace == String.Empty) ? name : 
		            _namespace + "." + name;

	    ModuleObject m;
	    ClassBase c;

	    // If the fully-qualified name of the requested attribute is 
	    // a namespace exported by a currently loaded assembly, return 
	    // a new ModuleObject representing that namespace.

	    if (AssemblyManager.IsValidNamespace(qname)) {
		m = new ModuleObject(qname);
		StoreAttribute(name, m);
		return (ManagedType) m;
	    }

	    // Look for a type in the current namespace. Note that this 
	    // includes types, delegates, enums, interfaces and structs.
	    // Only public namespace members are exposed to Python.

	    Type type = AssemblyManager.LookupType(qname);
	    if (type != null) {
		if (!type.IsPublic) {
		    return null;
		}
		c = ClassManager.GetClass(type);
		StoreAttribute(name, c);
		return (ManagedType) c;
	    }

	    // This is a little repetitive, but it ensures that the right
	    // thing happens with implicit assembly loading at a reasonable
	    // cost. Ask the AssemblyManager to do implicit loading for each 
	    // of the steps in the qualified name, then try it again.

	    if (AssemblyManager.LoadImplicit(qname)) {
		if (AssemblyManager.IsValidNamespace(qname)) {
		    m = new ModuleObject(qname);
		    StoreAttribute(name, m);
		    return (ManagedType) m;
		}

		type = AssemblyManager.LookupType(qname);
		if (type != null) {
		    if (!type.IsPublic) {
			return null;
		    }
		    c = ClassManager.GetClass(type);
		    StoreAttribute(name, c);
		    return (ManagedType) c;
		}
	    }

	    return null;
	}

Usage Example

Пример #1
0
        //====================================================================
        // ModuleObject __getattribute__ implementation. Module attributes
        // are always either classes or sub-modules representing subordinate
        // namespaces. CLR modules implement a lazy pattern - the sub-modules
        // and classes are created when accessed and cached for future use.
        //====================================================================

        public static IntPtr tp_getattro(IntPtr ob, IntPtr key)
        {
            ModuleObject self = (ModuleObject)GetManagedObject(ob);

            if (!Runtime.PyString_Check(key))
            {
                Exceptions.SetError(Exceptions.TypeError, "string expected");
                return(IntPtr.Zero);
            }

            IntPtr op = Runtime.PyDict_GetItem(self.dict, key);

            if (op != IntPtr.Zero)
            {
                Runtime.Incref(op);
                return(op);
            }

            string name = Runtime.GetManagedString(key);

            if (name == "__dict__")
            {
                Runtime.Incref(self.dict);
                return(self.dict);
            }

            ManagedType attr = self.GetAttribute(name, true);

            if (attr == null)
            {
                Exceptions.SetError(Exceptions.AttributeError, name);
                return(IntPtr.Zero);
            }

            // XXX - hack required to recognize exception types. These types
            // may need to be wrapped in old-style class wrappers in versions
            // of Python where new-style classes cannot be used as exceptions.

            if (Runtime.wrap_exceptions)
            {
                if (attr is ExceptionClassObject)
                {
                    ExceptionClassObject c = attr as ExceptionClassObject;
                    if (c != null)
                    {
                        IntPtr p = attr.pyHandle;
                        IntPtr r = Exceptions.GetExceptionClassWrapper(p);
                        Runtime.PyDict_SetItemString(self.dict, name, r);
                        Runtime.Incref(r);
                        return(r);
                    }
                }
            }

            Runtime.Incref(attr.pyHandle);
            return(attr.pyHandle);
        }
All Usage Examples Of Python.Runtime.ModuleObject::GetAttribute