Exemple #1
0
        public static EditableValue Create(object o, System.Reflection.PropertyInfo info)
        {
            var ret = new EditableValue();

            ret.Value = o;

            var p          = info;
            var attributes = p.GetCustomAttributes(false);

            var undo = attributes.Where(_ => _.GetType() == typeof(UndoAttribute)).FirstOrDefault() as UndoAttribute;

            if (undo != null && !undo.Undo)
            {
                ret.IsUndoEnabled = false;
            }
            else
            {
                ret.IsUndoEnabled = true;
            }

            var shown = attributes.Where(_ => _.GetType() == typeof(ShownAttribute)).FirstOrDefault() as ShownAttribute;

            if (shown != null && !shown.Shown)
            {
                ret.IsShown = false;
            }

            var selector_attribute = (from a in attributes where a is Data.SelectorAttribute select a).FirstOrDefault() as Data.SelectorAttribute;

            if (selector_attribute != null)
            {
                ret.SelfSelectorID = selector_attribute.ID;
            }

            // collect selected values
            var selectedAttributes = attributes.OfType <Data.SelectedAttribute>();

            if (selectedAttributes.Count() > 0)
            {
                if (selectedAttributes.Select(_ => _.ID).Distinct().Count() > 1)
                {
                    throw new Exception("Same IDs are required.");
                }

                ret.TargetSelectorID       = selectedAttributes.First().ID;
                ret.RequiredSelectorValues = selectedAttributes.Select(_ => _.Value).ToArray();
            }

            var nameKey = NameAttribute.GetKey(attributes);

            if (string.IsNullOrEmpty(nameKey))
            {
                nameKey = info.ReflectedType.Name + "_" + info.Name + "_Name";
            }

            if (MultiLanguageTextProvider.HasKey(nameKey))
            {
                ret.Title = new MultiLanguageString(nameKey);
            }
            else
            {
                ret.Title = NameAttribute.GetName(attributes);
            }

            var descKey = DescriptionAttribute.GetKey(attributes);

            if (string.IsNullOrEmpty(descKey))
            {
                descKey = info.ReflectedType.Name + "_" + info.Name + "_Desc";
            }

            if (MultiLanguageTextProvider.HasKey(descKey))
            {
                ret.Description = new MultiLanguageString(descKey);
            }
            else
            {
                ret.Description = DescriptionAttribute.GetDescription(attributes);
            }

            return(ret);
        }