Beispiel #1
0
        static void FillAttributes(Type type, CachedPropertyInfo info)
        {
            // Allow marking with a pretty name
            var lbl = info.GetCustomAttribute <DisplayNameAttribute>();

            if (lbl != null)
            {
                info.DisplayName = lbl.DisplayName;
            }
            else // or just split camel case
            {
                info.DisplayName = info.AccessName.SplitCamelCase();
            }

            // Allow the type to override the name whenever its meaning may change
            PropertyData.OverrideNameAttribute[] nameOverrides = type.GetCustomAttributes <PropertyData.OverrideNameAttribute>().ToArray();
            if (nameOverrides != null)
            {
                for (int j = 0; j < nameOverrides.Length; ++j)
                {
                    if (info.AccessName.Equals(nameOverrides[j].Target))
                    {
                        info.DisplayName = nameOverrides[j].NewName;
                    }
                }
            }

            EditorAttribute[] editors = info.GetCustomAttributes <EditorAttribute>().ToArray();
            if (editors != null)
            {
                for (int j = 0; j < editors.Length; ++j)
                {
                    info.EditType = (PropertyData.EditorType)Enum.Parse(typeof(PropertyData.EditorType), editors[j].EditorTypeName);
                }
                //info.EditorKeys.Add(editors[j].EditorTypeName);
            }

            // Mark out fields that are advanced
            EditorBrowsableAttribute browser = info.GetCustomAttribute <EditorBrowsableAttribute>();

            if (browser != null)
            {
                info.IsAdvanced = browser.State == EditorBrowsableState.Advanced;
            }

            // Grab enum names if necessary
            if (info.Type.IsEnum)
            {
                info.enumNames = Enum.GetNames(info.Type);
            }

            var tip = info.GetCustomAttribute <DescriptionAttribute>();

            if (tip != null)
            {
                info.Tip = tip.Description;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Gets the property information.
        /// </summary>
        /// <param name="containingType">Type of the containing.</param>
        /// <returns>CachedPropertyInfo.</returns>
        public CachedPropertyInfo GetPropertyInfo(Type containingType)
        {
            Argument.IsNotNull("containingType", containingType);

            if (!_updatedCachedPropertyInfo)
            {
                _updatedCachedPropertyInfo = true;

                var propertyInfo = containingType.GetPropertyEx(Name, BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
                _cachedPropertyInfo = (propertyInfo is null) ? null : new CachedPropertyInfo(propertyInfo);
            }

            return(_cachedPropertyInfo);
        }
Beispiel #3
0
        public List <CachedPropertyInfo> GetAlphabetical(Type type)
        {
            if (AlphabeticalCache.ContainsKey(type))
            {
                return(AlphabeticalCache[type]);
            }

            List <CachedPropertyInfo> ret = new List <CachedPropertyInfo>();

            if (scanProperties_)
            {
                PropertyInfo[] infos = type.GetProperties();
                foreach (var val in infos)
                {
                    CachedPropertyInfo info = new CachedPropertyInfo {
                        Property = val, AccessName = val.Name, Type = val.PropertyType
                    };
                    ret.Add(info);
                }
            }
            if (scanFields_)
            {
                FieldInfo[] infos = type.GetFields();
                foreach (var val in infos)
                {
                    CachedPropertyInfo info = new CachedPropertyInfo {
                        Field = val, Type = val.FieldType, AccessName = val.Name
                    };
                    ret.Add(info);
                }
            }

            ret = Filter(type, ret);

            for (int i = 0; i < ret.Count; ++i)
            {
                FillAttributes(type, ret[i]);
            }

            var r = ret.OrderBy(o => o.DisplayName).ToList();

            AlphabeticalCache[type] = r;
            return(r);
        }
Beispiel #4
0
        private void AssignProperty(CachedPropertyInfo cachedPropertyInfo, object o)
        {
            _console.WriteLine(string.Format(_translationProvider.Translate("AssignValueOfType"), cachedPropertyInfo.InnerMetaData.Name, cachedPropertyInfo.InnerMetaData.PropertyType.FullName));

            var value = _console.ReadLine();

            if (cachedPropertyInfo.InnerMetaData.PropertyType == typeof(string))
            {
                cachedPropertyInfo.InnerMetaData.SetValue(o, value);
            }
            else if (cachedPropertyInfo.InnerMetaData.PropertyType.IsPrimitive)
            {
                var parseMethod = GetParseMethod(cachedPropertyInfo.InnerMetaData.PropertyType);

                object parsedValue = null;

                while (true)
                {
                    try
                    {
                        parsedValue = parseMethod.InnerMetaData.Invoke(null, new object[] { value });
                        break;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.ToString());
                    }
                }

                cachedPropertyInfo.InnerMetaData.SetValue(o, parsedValue);
            }
            else if (cachedPropertyInfo.InnerMetaData.PropertyType.IsClass || cachedPropertyInfo.InnerMetaData.PropertyType.IsInterface)
            {
                EditObject(cachedPropertyInfo.InnerMetaData.PropertyType, o, out o);
            }
        }
Beispiel #5
0
        public List <CachedPropertyInfo> GetOrdered(Type type)
        {
            if (Cache.ContainsKey(type))
            {
                return(Cache[type]);
            }

            List <CachedPropertyInfo> ret = new List <CachedPropertyInfo>();

            if (scanProperties_)
            {
                PropertyInfo[] infos = type.GetProperties();
                foreach (var val in infos)
                {
                    CachedPropertyInfo info = new CachedPropertyInfo {
                        Property = val, AccessName = val.Name, Type = val.PropertyType
                    };
                    ret.Add(info);
                }
            }
            if (scanFields_)
            {
                FieldInfo[] infos = type.GetFields();
                foreach (var val in infos)
                {
                    CachedPropertyInfo info = new CachedPropertyInfo {
                        Field = val, Type = val.FieldType, AccessName = val.Name
                    };
                    ret.Add(info);
                }
            }

            ret = Filter(type, ret);

            for (int i = 0; i < ret.Count; ++i)
            {
                FillAttributes(type, ret[i]);
            }

            ret.Sort((lhs, rhs) => {
                var lhsAttr = lhs.GetCustomAttribute <PropertyData.PropertyPriorityAttribute>();
                var rhsAttr = rhs.GetCustomAttribute <PropertyData.PropertyPriorityAttribute>();
                if (lhsAttr != null && rhsAttr == null)
                {
                    return(-1);
                }
                else if (lhsAttr == null && rhsAttr != null)
                {
                    return(1);
                }
                else if (lhsAttr != null && rhsAttr != null)
                {
                    if (lhsAttr.Level == rhsAttr.Level)
                    {
                        return(0);
                    }
                    return(lhsAttr.Level < rhsAttr.Level ? -1 : 1);
                }
                return(1);
            });

            Cache[type] = ret;
            return(ret);
        }