⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 classdescriptor.cs

📁 Perst开源实时数据库
💻 CS
📖 第 1 页 / 共 2 页
字号:
		
        public static FieldType getTypeCode(Type c)
        {
            FieldType type;
            if (c.Equals(typeof(byte)))
            {
                type = FieldType.tpByte;
            }
            else if (c.Equals(typeof(sbyte)))
            {
                type = FieldType.tpSByte;
            }
            else if (c.Equals(typeof(short)))
            {
                type = FieldType.tpShort;
            }
            else if (c.Equals(typeof(ushort)))
            {
                type = FieldType.tpUShort;
            }
            else if (c.Equals(typeof(char)))
            {
                type = FieldType.tpChar;
            }
            else if (c.Equals(typeof(int)))
            {
                type = FieldType.tpInt;
            }
            else if (c.Equals(typeof(uint)))
            {
                type = FieldType.tpUInt;
            }
            else if (c.Equals(typeof(long)))
            {
                type = FieldType.tpLong;
            }
            else if (c.Equals(typeof(ulong)))
            {
                type = FieldType.tpULong;
            }
            else if (c.Equals(typeof(float)))
            {
                type = FieldType.tpFloat;
            }
            else if (c.Equals(typeof(double)))
            {
                type = FieldType.tpDouble;
            }
            else if (c.Equals(typeof(System.String)))
            {
                type = FieldType.tpString;
            }
            else if (c.Equals(typeof(bool)))
            {
                type = FieldType.tpBoolean;
            }
            else if (c.Equals(typeof(System.DateTime)))
            {
                type = FieldType.tpDate;
            }
            else if (c.IsEnum) 
            { 
                type = FieldType.tpEnum;
            }
            else if (c.Equals(typeof(decimal)))
            { 
                type = FieldType.tpDecimal;
            }
            else if (c.Equals(typeof(Guid))) 
            { 
                type = FieldType.tpGuid;
            }
            else if (typeof(IPersistent).IsAssignableFrom(c))
            {
                type = FieldType.tpObject;
            }
            else if (typeof(ValueType).IsAssignableFrom(c))
            {
                type = FieldType.tpValue;
            }
            else if (typeof(GenericPArray).IsAssignableFrom(c))
            {
                type = FieldType.tpArrayOfOid;
            }
            else if (typeof(GenericLink).IsAssignableFrom(c))
            {
                type = FieldType.tpLink;
            }
            else if (typeof(CustomSerializable).IsAssignableFrom(c))
            {
                type = FieldType.tpCustom;
            }
            else if (c.IsArray)
            {
                type = getTypeCode(c.GetElementType());
                if ((int)type >= (int)FieldType.tpLink)
                {
                    throw new StorageError(StorageError.ErrorCode.UNSUPPORTED_TYPE, c);
                }
                type = (FieldType)((int)type + (int)FieldType.tpArrayOfBoolean);
            }
            else
            {
#if SUPPORT_RAW_TYPE
                if (serializeNonPersistentObjects || c == typeof(object) || c == typeof(IComparable) || c == typeof(Array)) 
                {
                    type = FieldType.tpRaw;
                } 
                else 
                { 
                    throw new StorageError(StorageError.ErrorCode.UNSUPPORTED_TYPE, c);
                }
#else
                throw new StorageError(StorageError.ErrorCode.UNSUPPORTED_TYPE, c);
#endif
            }
            return type;
        }
		
        internal ClassDescriptor()
        {
        }
		
        internal ClassDescriptor(StorageImpl storage, Type cls)
        {
            this.cls = cls;
            name = getTypeName(cls);
            ArrayList list = new ArrayList();
            buildFieldList(storage, cls, list);
            allFields = (FieldDescriptor[]) list.ToArray(typeof(FieldDescriptor));
            defaultConstructor = cls.GetConstructor(BindingFlags.Instance|BindingFlags.Instance|BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.DeclaredOnly, null, defaultConstructorProfile, null);
            if (defaultConstructor == null && !cls.IsInterface && !typeof(ValueType).IsAssignableFrom(cls)) 
            { 
                throw new StorageError(StorageError.ErrorCode.DESCRIPTOR_FAILURE, cls);
            }
            resolved = true;
        }
		
        internal static Type lookup(Storage storage, String name)
        {
            Hashtable resolvedTypes = ((StorageImpl)storage).resolvedTypes;
            lock (resolvedTypes)
            { 
                Type cls = (Type)resolvedTypes[name];
                if (cls != null)
                { 
                    return cls;
                }
                ClassLoader loader = storage.Loader;
                if (loader != null) 
                { 
                    cls = loader.LoadClass(name);
                    if (cls != null) 
                    { 
                        resolvedTypes[name] = cls;
                        return cls;
                    }
                }
                Module last = lastModule;
                if (last != null) 
                {
                    cls = last.GetType(name);
                    if (cls != null) 
                    {
                        resolvedTypes[name] = cls;
                        return cls;
                    }
                }
#if USE_GENERICS
                int p = name.IndexOf('=');
                if (p >= 0)
                { 
                    Type genericType = lookup(storage, name.Substring(0, p));
                    Type[] genericParams = new Type[genericType.GetGenericArguments().Length];
                    int nest = 0;
                    int i = p += 2; 
                    int n = 0;

                    while (true)
                    {   
                        switch (name[i++])
                        {
                        case '[': 
                            nest += 1;
                            break;
                        case ']': 
                            if (--nest < 0) 
                            { 
                                genericParams[n++] = lookup(storage, name.Substring(p, i-p-1));
                                Debug.Assert(n == genericParams.Length);
                                cls = genericType.MakeGenericType(genericParams);
                                if (cls == null)
                                { 
                                    throw new StorageError(StorageError.ErrorCode.CLASS_NOT_FOUND, name);
                                }
                                resolvedTypes[name] = cls;
                                return cls;
                            }   
                            break;
                        case ',':
                            if (nest == 0) 
                            {
                                genericParams[n++] = lookup(storage, name.Substring(p, i-p-1));
                                p = i;
                            }
                            break;
                        }
                    }
                }
#endif

#if COMPACT_NET_FRAMEWORK
                foreach (Assembly ass in StorageImpl.assemblies) 
#else
                foreach (Assembly ass in AppDomain.CurrentDomain.GetAssemblies()) 
#endif
                { 
                    foreach (Module mod in ass.GetModules()) 
                    { 
                        Type t = mod.GetType(name);
                        if (t != null && t != cls) 
                        {
                            if (cls != null) 
                            { 
#if FRAMEWORK_2
                                if (Assembly.GetAssembly(cls) != ass)
#endif
                                {
                                    throw new StorageError(StorageError.ErrorCode.AMBIGUITY_CLASS, name);
                                }

                            } 
                            else 
                            { 
                                lastModule = mod;
                                cls = t;
                            }
                        }
                    }
                }
#if !COMPACT_NET_FRAMEWORK
                if (cls == null && name.EndsWith("Wrapper")) 
                {
                    Type originalType = lookup(storage, name.Substring(0, name.Length-7));
                    lock (storage) 
                    {
                        cls = ((StorageImpl)storage).getWrapper(originalType);
                    }
                }
#endif
                if (cls == null) 
                {
                    throw new StorageError(StorageError.ErrorCode.CLASS_NOT_FOUND, name);
                }
                resolvedTypes[name] = cls;
                return cls;
            }
        }


        internal static FieldInfo lookupField(Type type, string name) 
        {
            do { 
                FieldInfo f = type.GetField(name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.DeclaredOnly);
                if (f != null) 
                { 
                    return f;
                }
            } while ((type = type.BaseType) != null);
            return null;
        }
             
        internal static MemberInfo lookupComponent(Type cls, string name, out Type compType) 
        {
            FieldInfo fld = lookupField(cls, name);
            compType = null;
            if (fld != null) 
            {
                compType =  fld.FieldType;
                return fld;
            }
            PropertyInfo prop = cls.GetProperty(name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); 
            if (prop != null) 
            {
                compType = prop.PropertyType;
                return prop;
            } 
            return null;
        }
            

        public override void OnLoad()
        {
            cls = lookup(Storage, name);
            int n = allFields.Length;
#if !COMPACT_NET_FRAMEWORK
            bool hasTransparentAttribute = cls.GetCustomAttributes(typeof(TransparentPersistenceAttribute), true).Length != 0;
#else
            bool hasTransparentAttribute = false;
#endif
            for (int i = n; --i >= 0;) 
            { 
                FieldDescriptor fd = allFields[i];
                fd.Load();
                fd.field = lookupField(cls, fd.fieldName);
                if (hasTransparentAttribute && fd.type == FieldType.tpObject && isPerstInternalType(fd.field.FieldType)) 
                {
                    fd.recursiveLoading = true;
                }                
#if USE_GENERICS
                switch (fd.type)
                {
                case FieldType.tpArrayOfOid:
                    fd.constructor = GetConstructor(fd.field, "ConstructArray");
                    break;
                case FieldType.tpLink:
                    fd.constructor = GetConstructor(fd.field, "ConstructLink");
                    break;
                default:
                    break;
                }
#endif
            }
            defaultConstructor = cls.GetConstructor(BindingFlags.Instance|BindingFlags.Instance|BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.DeclaredOnly, null, defaultConstructorProfile, null);
            if (defaultConstructor == null && !cls.IsInterface && !typeof(ValueType).IsAssignableFrom(cls)) 
            { 
                throw new StorageError(StorageError.ErrorCode.DESCRIPTOR_FAILURE, cls);
            }
            StorageImpl s = (StorageImpl)Storage;
            if (!s.classDescMap.Contains(cls)) 
            {
                ((StorageImpl)Storage).classDescMap.Add(cls, this);
            }
        }

        internal void resolve() 
        {
            if (!resolved) 
            { 
                StorageImpl classStorage = (StorageImpl)Storage;
                ClassDescriptor desc = new ClassDescriptor(classStorage, cls);
                resolved = true;
                if (!desc.equals(this)) 
                { 
                    classStorage.registerClassDescriptor(desc);
                }
            }
        }            

        public override bool RecursiveLoading()
        {
            return false;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -