Esempio n. 1
0
 private void CollectProperties(object instance, EditorCommon.CustomPropertyDescriptor descriptor, List <Property> propertyCollection, bool noCategory,
                                bool alphabetical, bool automaticlyExpandObjects, string filter, Property parentProperty, bool parentIsValueType, PropertyGrid hostGrid, bool mergeMultiValue, bool check = true)
 {
     if (descriptor.Attributes[typeof(FlatAttribute)] == null && check)
     {
         if (descriptor.IsBrowsable)
         {
             if (mergeMultiValue)
             {
                 CollectPropertiesWithInstance(instance, descriptor, propertyCollection, noCategory, alphabetical, automaticlyExpandObjects, filter, parentProperty, parentIsValueType, hostGrid);
             }
             else
             {
                 var insType            = instance.GetType();
                 var enumrableInterface = insType.GetInterface(typeof(IEnumerable).FullName, false);
                 if (enumrableInterface != null)
                 {
                     foreach (var objIns in (IEnumerable)instance)
                     {
                         CollectPropertiesWithInstance(objIns, descriptor, propertyCollection, noCategory, alphabetical, automaticlyExpandObjects, filter, parentProperty, parentIsValueType, hostGrid);
                     }
                 }
                 else
                 {
                     CollectPropertiesWithInstance(instance, descriptor, propertyCollection, noCategory, alphabetical, automaticlyExpandObjects, filter, parentProperty, parentIsValueType, hostGrid);
                 }
             }
         }
     }
     else
     {
         instance = descriptor.GetValue(instance);
         if (instance == null)
         {
             return;
         }
         var insType            = instance.GetType();
         var enumrableInterface = insType.GetInterface(typeof(IEnumerable).FullName, false);
         if (enumrableInterface != null)
         {
             if (mergeMultiValue)
             {
                 PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(instance);
                 foreach (EditorCommon.CustomPropertyDescriptor propertyDescriptor in properties)
                 {
                     CollectProperties(instance, propertyDescriptor, propertyCollection, noCategory, alphabetical, automaticlyExpandObjects, filter, parentProperty, parentIsValueType, hostGrid, false);
                 }
             }
             else
             {
                 foreach (var objIns in (IEnumerable)instance)
                 {
                     PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(instance);
                     foreach (EditorCommon.CustomPropertyDescriptor propertyDescriptor in properties)
                     {
                         CollectProperties(objIns, propertyDescriptor, propertyCollection, noCategory, alphabetical, automaticlyExpandObjects, filter, parentProperty, parentIsValueType, hostGrid, false);
                     }
                 }
             }
         }
         else
         {
             var properties = new EditorCommon.CustomPropertyDescriptorCollection(insType, TypeDescriptor.GetProperties(instance), parentIsValueType);
             foreach (EditorCommon.CustomPropertyDescriptor propertyDescriptor in properties)
             {
                 CollectProperties(instance, propertyDescriptor, propertyCollection, noCategory, alphabetical, automaticlyExpandObjects, filter, parentProperty, parentIsValueType, hostGrid, false);
             }
         }
     }
 }
Esempio n. 2
0
        public PropertyCollection(object instance, bool noCategory, bool alphabetical, bool automaticlyExpandObjects, string filter, Property parentProperty, bool parentIsValueType, PropertyGrid hostGrid, bool mergeMultiValue = false)
        {
            //bool useCustomTypeConverter = false;
            List <EditorCommon.CustomPropertyDescriptorCollection> propertyCollections = new List <EditorCommon.CustomPropertyDescriptorCollection>();
            List <EditorCommon.CustomPropertyDescriptorCollection> fieldCollections    = new List <EditorCommon.CustomPropertyDescriptorCollection>();

            EditorCommon.CustomPropertyDescriptorCollection properties = null;
            EditorCommon.CustomPropertyDescriptorCollection fields     = null;
            System.Reflection.BindingFlags getFieldsFlag = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic;
            if (instance != null)
            {
                var enumrableInterface = instance.GetType().GetInterface(typeof(IEnumerable).FullName, false);
                if (enumrableInterface != null)
                {
                    // 显示多个对象
                    foreach (var objIns in (IEnumerable)instance)
                    {
                        if (objIns == null)
                        {
                            continue;
                        }
                        var objType = objIns.GetType();
                        EditorCommon.CustomPropertyDescriptorCollection tempProperties;
                        if (!mPropertyDesColDic.TryGetValue(objType, out tempProperties))
                        {
                            PropertyDescriptorCollection pros;
                            var tc = TypeDescriptor.GetConverter(objIns);
                            if (tc == null || !tc.GetPropertiesSupported())
                            {
                                if (objIns is ICustomTypeDescriptor)
                                {
                                    pros = ((ICustomTypeDescriptor)instance).GetProperties();
                                }
                                else
                                {
                                    pros = TypeDescriptor.GetProperties(objIns);
                                }
                            }
                            else
                            {
                                pros = tc.GetProperties(objIns);
                            }
                            tempProperties = new EditorCommon.CustomPropertyDescriptorCollection(objType, pros, parentIsValueType);
                            mPropertyDesColDic[objType] = tempProperties;
                            if (objType.Name == "CGfxMeshImportOption")
                            {
                                int s = 0;
                                s = 1;
                            }
                        }
                        if (objType.Name == "CGfxMeshImportOption")
                        {
                            int s = 0;
                            s = 1;
                        }
                        if (mergeMultiValue)
                        {
                            if (properties == null)
                            {
                                var pros = new EditorCommon.CustomPropertyDescriptor[tempProperties.Count];
                                tempProperties.CopyTo(pros, 0);
                                properties = new EditorCommon.CustomPropertyDescriptorCollection(pros);
                                //properties = tempProperties;
                            }
                            else if (properties.Properties == tempProperties.Properties)
                            {
                                for (int i = properties.Count - 1; i >= 0; i--)
                                {
                                    var idx = tempProperties.IndexOf(properties[i]);
                                    if (idx >= 0)
                                    {
                                        var item = tempProperties[idx];
                                        properties[i].Add(item);
                                    }
                                    else
                                    {
                                        properties.RemoveAt(i);
                                    }
                                }
                            }
                        }
                        else
                        {
                            propertyCollections.Add(tempProperties);
                        }


                        EditorCommon.CustomPropertyDescriptorCollection tempFields;
                        if (!mFieldInfosDic.TryGetValue(objType, out tempFields))
                        {
                            var fls = objType.GetFields(getFieldsFlag);
                            tempFields = new EditorCommon.CustomPropertyDescriptorCollection(objType, fls, parentIsValueType);
                            mFieldInfosDic[objType] = tempFields;
                        }
                        if (mergeMultiValue)
                        {
                            if (fields == null)
                            {
                                fields = tempFields;
                            }
                            else
                            {
                                for (int i = fields.Count - 1; i >= 0; i--)
                                {
                                    var idx = tempFields.IndexOf(fields[i]);
                                    if (idx >= 0)
                                    {
                                        var item = tempFields[idx];
                                        fields[i].Add(item);
                                    }
                                    else
                                    {
                                        fields.RemoveAt(i);
                                    }
                                }
                            }
                        }
                        else
                        {
                            fieldCollections.Add(tempFields);
                        }
                    }
                }
                else
                {
                    // 显示单个对象
                    var insType = instance.GetType();
                    if (!mPropertyDesColDic.TryGetValue(insType, out properties))
                    {
                        PropertyDescriptorCollection pros;
                        TypeConverter tc = TypeDescriptor.GetConverter(instance);
                        if (tc == null || !tc.GetPropertiesSupported())
                        {
                            if (instance is ICustomTypeDescriptor)
                            {
                                pros = ((ICustomTypeDescriptor)instance).GetProperties();
                            }
                            else
                            {
                                pros = TypeDescriptor.GetProperties(insType);  //I changed here from instance to instance.GetType, so that only the Direct Properties are shown!
                            }
                        }
                        else
                        {
                            pros = tc.GetProperties(instance);
                            //useCustomTypeConverter = true;
                        }
                        properties = new EditorCommon.CustomPropertyDescriptorCollection(insType, pros, parentIsValueType);
                        mPropertyDesColDic[insType] = properties;
                        if (insType.Name == "CGfxMeshImportOption")
                        {
                            int s = 0;
                            s = 1;
                        }
                    }

                    if (!mFieldInfosDic.TryGetValue(insType, out fields))
                    {
                        var flds = insType.GetFields(getFieldsFlag);
                        fields = new EditorCommon.CustomPropertyDescriptorCollection(insType, flds, parentIsValueType);
                        mFieldInfosDic[insType] = fields;
                    }
                }
            }

            if (properties == null)
            {
                properties = new EditorCommon.CustomPropertyDescriptorCollection(new EditorCommon.CustomPropertyDescriptor[] { });
            }
            if (fields == null)
            {
                fields = new EditorCommon.CustomPropertyDescriptorCollection(new EditorCommon.CustomPropertyDescriptor[] { });
            }

            if (mergeMultiValue || instance == null)
            {
                List <Property> propertyCollection           = new List <Property>();
                Dictionary <string, PropertyCategory> groups = new Dictionary <string, PropertyCategory>();
                foreach (EditorCommon.CustomPropertyDescriptor propertyDescriptor in properties)
                {
                    CollectProperties(instance, propertyDescriptor, propertyCollection, noCategory, alphabetical, automaticlyExpandObjects, filter, parentProperty, parentIsValueType, hostGrid, mergeMultiValue);
                }
                foreach (EditorCommon.CustomPropertyDescriptor propertyDescriptor in fields)
                {
                    CollectProperties(instance, propertyDescriptor, propertyCollection, noCategory, alphabetical, automaticlyExpandObjects, filter, parentProperty, parentIsValueType, hostGrid, mergeMultiValue);
                }

                ProcessPropertyCollection(parentProperty, propertyCollection, noCategory, alphabetical, filter, groups, Items);
            }
            else
            {
                var enumrableInterface = instance.GetType().GetInterface(typeof(IEnumerable).FullName, false);
                if (enumrableInterface != null)
                {
                    int idxShow = 1;  // 从1开始算
                    foreach (var objIns in (IEnumerable)instance)
                    {
                        Dictionary <string, PropertyCategory> groups = new Dictionary <string, PropertyCategory>();
                        List <Property> tempProCollection            = new List <Property>();
                        if (propertyCollections.Count >= idxShow)
                        {
                            var pros = propertyCollections[idxShow - 1];
                            foreach (EditorCommon.CustomPropertyDescriptor propertyDescriptor in pros)
                            {
                                CollectProperties(objIns, propertyDescriptor, tempProCollection, noCategory, alphabetical, automaticlyExpandObjects, filter, parentProperty, parentIsValueType, hostGrid, mergeMultiValue);
                            }
                            var field = fieldCollections[idxShow - 1];
                            foreach (EditorCommon.CustomPropertyDescriptor fieldDescriptor in field)
                            {
                                CollectProperties(objIns, fieldDescriptor, tempProCollection, noCategory, alphabetical, automaticlyExpandObjects, filter, parentProperty, parentIsValueType, hostGrid, mergeMultiValue);
                            }
                            var atts       = objIns.GetType().GetCustomAttributes(typeof(EngineNS.Editor.Editor_DisplayNameInEnumerable), true);
                            var nameString = idxShow.ToString();
                            if (atts.Length > 0)
                            {
                                var att = atts[0] as EngineNS.Editor.Editor_DisplayNameInEnumerable;
                                nameString += " " + att.DisplayName;
                            }
                            var category = new PropertyCategory(nameString);
                            ProcessPropertyCollection(parentProperty, tempProCollection, noCategory, alphabetical, filter, groups, category.Items);
                            Items.Add(category);
                            idxShow++;
                        }
                    }
                }
                else
                {
                    List <Property> propertyCollection           = new List <Property>();
                    Dictionary <string, PropertyCategory> groups = new Dictionary <string, PropertyCategory>();
                    foreach (EditorCommon.CustomPropertyDescriptor propertyDescriptor in properties)
                    {
                        CollectProperties(instance, propertyDescriptor, propertyCollection, noCategory, alphabetical, automaticlyExpandObjects, filter, parentProperty, parentIsValueType, hostGrid, mergeMultiValue);
                    }
                    foreach (EditorCommon.CustomPropertyDescriptor propertyDescriptor in fields)
                    {
                        CollectProperties(instance, propertyDescriptor, propertyCollection, noCategory, alphabetical, automaticlyExpandObjects, filter, parentProperty, parentIsValueType, hostGrid, mergeMultiValue);
                    }

                    ProcessPropertyCollection(parentProperty, propertyCollection, noCategory, alphabetical, filter, groups, Items);
                }
            }
        }