Exemplo n.º 1
0
        /// <summary>
        /// Ensure all properties which would default to null (except string type) have default initialised
        /// instances assigned to them (applied recursively to those instances)
        /// </summary>
        /// <param name="o">The content item</param>
        public static void InitialiseProperties(object o)
        {
            foreach (var prop in o.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty))
            {
                ScaffoldColumnAttribute sca = prop.GetCustomAttribute <ScaffoldColumnAttribute>();
                if (sca != null && !sca.Scaffold)
                {
                    continue;
                }

                if (prop.PropertyType.IsArray)
                {
                    throw new Exception("Content types may not have array properties: " + o.GetType().FullName);
                }

                if (prop.PropertyType != typeof(string) &&
                    prop.PropertyType.IsClass &&
                    prop.CanWrite &&
                    prop.GetIndexParameters().Length == 0)
                {
                    if (prop.PropertyType.GetConstructor(Type.EmptyTypes) == null)
                    {
                        throw new ArgumentException("Cannot initialise type " + prop.PropertyType.FullName + " it has no default constructor");
                    }
                    object init = Activator.CreateInstance(prop.PropertyType);
                    InitialiseProperties(init);
                    prop.SetValue(o, init);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Action to take when visiting a compound/complex property
        /// </summary>
        /// <param name="pi">property info of the property</param>
        /// <param name="val">value of the property</param>
        public virtual void Object(PropertyInfo pi, object val)
        {
            if (val == null)
            {
                return;
            }

            Type t = val.GetType();

            foreach (var prop in t.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty))
            {
                if (prop.GetIndexParameters().Length > 0)
                {
                    continue;
                }
                if (!PropertyFilter(prop))
                {
                    continue;
                }
                ScaffoldColumnAttribute sca = prop.GetCustomAttribute <ScaffoldColumnAttribute>();
                if (sca != null && !sca.Scaffold)
                {
                    continue;
                }
                JsonIgnoreAttribute jia = prop.GetCustomAttribute <JsonIgnoreAttribute>();
                if (jia != null)
                {
                    continue;
                }

                Visit(prop, val == null ? null : prop.GetValue(val));
            }
        }
Exemplo n.º 3
0
        public static IEnumerable <PropertyInfo> GetVisitableProperties(Type t)
        {
            foreach (var prop in t.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty))
            {
                if (prop.GetIndexParameters().Length > 0)
                {
                    continue;
                }
                ScaffoldColumnAttribute sca = prop.GetCustomAttribute <ScaffoldColumnAttribute>();
                if (sca != null && !sca.Scaffold)
                {
                    continue;
                }
                JsonIgnoreAttribute jia = prop.GetCustomAttribute <JsonIgnoreAttribute>();
                if (jia != null)
                {
                    continue;
                }

                yield return(prop);
            }
        }
Exemplo n.º 4
0
        private static void InitialiseProperties(object o, List <Type> parentTypes)
        {
            foreach (var prop in o.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty))
            {
                ScaffoldColumnAttribute sca = prop.GetCustomAttribute <ScaffoldColumnAttribute>();
                if (sca != null && !sca.Scaffold)
                {
                    continue;
                }

                if (parentTypes.Contains(o.GetType())) // avoid recursion
                {
                    throw new Exception("InitialiseProperties encountered a self-referential loop on type " + o.GetType().FullName);
                }

                if (prop.PropertyType.IsArray)
                {
                    throw new Exception("Content types may not have array properties: " + o.GetType().FullName);
                }

                if (prop.PropertyType != typeof(string) &&
                    prop.PropertyType.IsClass() &&
                    prop.CanWrite &&
                    prop.GetIndexParameters().Length == 0)
                {
                    if (prop.PropertyType.GetConstructor(Type.EmptyTypes) == null)
                    {
                        throw new ArgumentException("Cannot initialise type " + prop.PropertyType.FullName + " it has no default constructor");
                    }
                    object init = Activator.CreateInstance(prop.PropertyType);
                    InitialiseProperties(init, parentTypes.Concat(new List <Type> {
                        o.GetType()
                    }).ToList());
                    prop.SetValue(o, init);
                }
            }
        }
 private static IHiddenFacet Create(ScaffoldColumnAttribute attribute, ISpecification holder)
 {
     return(attribute == null ? null : new HiddenFacet(attribute.Scaffold ? WhenTo.Never : WhenTo.Always, holder));
 }
        protected override ModelMetadata CreateMetadata(IEnumerable <Attribute> attributes, Type containerType, Func <object> modelAccessor, Type modelType, string propertyName)
        {
            List <Attribute>             attributeList          = new List <Attribute>(attributes);
            DisplayColumnAttribute       displayColumnAttribute = attributeList.OfType <DisplayColumnAttribute>().FirstOrDefault();
            DataAnnotationsModelMetadata result = new DataAnnotationsModelMetadata(this, containerType, modelAccessor, modelType, propertyName, displayColumnAttribute);

            // Do [HiddenInput] before [UIHint], so you can override the template hint
            HiddenInputAttribute hiddenInputAttribute = attributeList.OfType <HiddenInputAttribute>().FirstOrDefault();

            if (hiddenInputAttribute != null)
            {
                result.TemplateHint        = "HiddenInput";
                result.HideSurroundingHtml = !hiddenInputAttribute.DisplayValue;
            }

            // We prefer [UIHint("...", PresentationLayer = "MVC")] but will fall back to [UIHint("...")]
            IEnumerable <UIHintAttribute> uiHintAttributes = attributeList.OfType <UIHintAttribute>();
            UIHintAttribute uiHintAttribute = uiHintAttributes.FirstOrDefault(a => String.Equals(a.PresentationLayer, "MVC", StringComparison.OrdinalIgnoreCase))
                                              ?? uiHintAttributes.FirstOrDefault(a => String.IsNullOrEmpty(a.PresentationLayer));

            if (uiHintAttribute != null)
            {
                result.TemplateHint = uiHintAttribute.UIHint;
            }

            DataTypeAttribute dataTypeAttribute = attributeList.OfType <DataTypeAttribute>().FirstOrDefault();

            if (dataTypeAttribute != null)
            {
                result.DataTypeName = dataTypeAttribute.GetDataTypeName();
            }

            ReadOnlyAttribute readOnlyAttribute = attributeList.OfType <ReadOnlyAttribute>().FirstOrDefault();

            if (readOnlyAttribute != null)
            {
                result.IsReadOnly = readOnlyAttribute.IsReadOnly;
            }

            DisplayFormatAttribute displayFormatAttribute = attributeList.OfType <DisplayFormatAttribute>().FirstOrDefault();

            if (displayFormatAttribute == null && dataTypeAttribute != null)
            {
                displayFormatAttribute = dataTypeAttribute.DisplayFormat;
            }
            if (displayFormatAttribute != null)
            {
                result.NullDisplayText          = displayFormatAttribute.NullDisplayText;
                result.DisplayFormatString      = displayFormatAttribute.DataFormatString;
                result.ConvertEmptyStringToNull = displayFormatAttribute.ConvertEmptyStringToNull;

                if (displayFormatAttribute.ApplyFormatInEditMode)
                {
                    result.EditFormatString = displayFormatAttribute.DataFormatString;
                }
            }

            ScaffoldColumnAttribute scaffoldColumnAttribute = attributeList.OfType <ScaffoldColumnAttribute>().FirstOrDefault();

            if (scaffoldColumnAttribute != null)
            {
                result.ShowForDisplay = result.ShowForEdit = scaffoldColumnAttribute.Scaffold;
            }

            DisplayNameAttribute displayNameAttribute = attributeList.OfType <DisplayNameAttribute>().FirstOrDefault();

            if (displayNameAttribute != null)
            {
                result.DisplayName = displayNameAttribute.DisplayName;
            }

            RequiredAttribute requiredAttribute = attributeList.OfType <RequiredAttribute>().FirstOrDefault();

            if (requiredAttribute != null)
            {
                result.IsRequired = true;
            }

            return(result);
        }
Exemplo n.º 7
0
        public static Metadata Create(MemberInfo memberInfo)
        {
            if (memberInfo == null)
            {
                throw new ArgumentNullException("memberInfo");
            }

            Metadata metadata = new Metadata();

            // Name
            metadata.Name = memberInfo.Name;

            // DisplayAttribute
            DisplayAttribute displayAttribute = memberInfo.GetCustomAttribute <DisplayAttribute>();

            if (displayAttribute != null)
            {
                metadata.DisplayName = displayAttribute.GetName();
                metadata.ShortName   = displayAttribute.GetShortName();
                metadata.GroupName   = displayAttribute.GetGroupName();
                metadata.Description = displayAttribute.GetDescription();

                int?order = displayAttribute.GetOrder();
                if (order != null)
                {
                    metadata.Order = order.Value;
                }
            }

            if (metadata.DisplayName == null)
            {
                metadata.DisplayName = ConvertUtilities.Decamelize(metadata.Name);
            }

            // DataType
            DataTypeAttribute dataTypeAttribute = memberInfo.GetCustomAttribute <DataTypeAttribute>();

            if (dataTypeAttribute != null)
            {
                metadata.DataType = dataTypeAttribute.GetDataTypeName();
                Fill(metadata, dataTypeAttribute.DisplayFormat);
            }
            if (metadata.DataType == null)
            {
                PropertyInfo pi = memberInfo as PropertyInfo;
                if (pi != null)
                {
                    metadata.DataType = pi.PropertyType.AssemblyQualifiedName;
                }
                else
                {
                    FieldInfo fi = memberInfo as FieldInfo;
                    if (fi != null)
                    {
                        metadata.DataType = fi.FieldType.AssemblyQualifiedName;
                    }
                }
            }

            // DisplayFormat
            DisplayFormatAttribute displayFormatAttribute = memberInfo.GetCustomAttribute <DisplayFormatAttribute>();

            if (displayFormatAttribute != null)
            {
                Fill(metadata, displayFormatAttribute);
            }

            // ScaffoldColumnAttribute
            ScaffoldColumnAttribute scaffoldColumnAttribute = memberInfo.GetCustomAttribute <ScaffoldColumnAttribute>();

            if (scaffoldColumnAttribute != null)
            {
                metadata.Hidden = scaffoldColumnAttribute.Scaffold;
            }

            return(metadata);
        }
 public void Ctor_Bool(bool value)
 {
     var attribute = new ScaffoldColumnAttribute(value);
     Assert.Equal(value, attribute.Scaffold);
 }
        //public override IEnumerable<ModelMetadata> GetMetadataForProperties(object container, Type containerType)
        //{
        //  return base.GetMetadataForProperties(container, containerType);
        //}

        //public override ModelMetadata GetMetadataForType(Func<object> modelAccessor, Type modelType)
        //{
        //  return base.GetMetadataForType(modelAccessor, modelType);
        //}

        //public override ModelMetadata GetMetadataForProperty(Func<object> modelAccessor, Type containerType, string propertyName)
        //{
        //  return base.GetMetadataForProperty(modelAccessor, containerType, propertyName);
        //}

        //protected override ModelMetadata GetMetadataForProperty(Func<object> modelAccessor, Type containerType, System.ComponentModel.PropertyDescriptor propertyDescriptor)
        //{
        //  return base.GetMetadataForProperty(modelAccessor, containerType, propertyDescriptor);
        //}
        protected override ModelMetadata CreateMetadata(IEnumerable <Attribute> attributes, Type containerType, Func <object> modelAccessor, Type modelType, string propertyName)
        {
            //Reflected from base class
            List <Attribute>       list = new List <Attribute>(attributes);
            DisplayColumnAttribute displayColumnAttribute = Enumerable.FirstOrDefault <DisplayColumnAttribute>(Enumerable.OfType <DisplayColumnAttribute>((IEnumerable)list));

            DataAnnotationsModelMetadata annotationsModelMetadata = null;

            //brl additions
            //if this is a BOV backed object
            if (containerType != null && Attribute.IsDefined(containerType, typeof(DryLogicObjectAttribute)) && propertyName != "OI")
            {
                Object         container = null;
                ObjectInstance oi        = null;
                //By having this code here instead of in GetMetadataForProperty, some of the normal features like ui hint will work
                if (modelAccessor != null)
                {
                    var rootModelType = modelAccessor.Target.GetType();
                    var field         = rootModelType.GetField("container");
                    if (field != null)
                    {
                        container = field.GetValue(modelAccessor.Target);
                        //if we don't have a reference to the container yet...
                        if (container.GetType() != containerType)
                        {
                            //...then try to break down the expression to get it
                            //get the expression as text, ie "model.EmployeeViewModel.MyEmployee" and split it
                            var expressionParts = ((LambdaExpression)rootModelType.GetField("expression").GetValue(modelAccessor.Target)).Body.ToString().Split('.');
                            //var expressionParts = new string[] { };

                            //loop thru the parts in the middle
                            for (int i = 1; i < expressionParts.Length - 1; i++)
                            {
                                container = container.GetType().GetProperty(expressionParts[i]).GetValue(container);
                            }
                        }
                        //could use an attribute instead to identify the object instance
                        oi = ObjectInstance.GetObjectInstance(container);

                        if (oi != null)//not really sure how this woudl fail at this point
                        {
                            annotationsModelMetadata           = new PropertyValueMetadata(this, containerType, modelAccessor, modelType, propertyName, displayColumnAttribute);
                            annotationsModelMetadata.Container = container;
                            //internally, setting model wipes out modelAccessor (caching of sorts)
                            annotationsModelMetadata.Model        = oi.PropertyValues[propertyName];
                            annotationsModelMetadata.TemplateHint = "PropertyValue";
                        }
                    }
                }
            }
            if (annotationsModelMetadata == null)
            {
                annotationsModelMetadata = new DataAnnotationsModelMetadata(this, containerType, modelAccessor, modelType, propertyName, displayColumnAttribute);
            }


            HiddenInputAttribute hiddenInputAttribute = Enumerable.FirstOrDefault <HiddenInputAttribute>(Enumerable.OfType <HiddenInputAttribute>((IEnumerable)list));

            if (hiddenInputAttribute != null)
            {
                annotationsModelMetadata.TemplateHint        = "HiddenInput";
                annotationsModelMetadata.HideSurroundingHtml = !hiddenInputAttribute.DisplayValue;
            }
            IEnumerable <UIHintAttribute> source = Enumerable.OfType <UIHintAttribute>((IEnumerable)list);
            UIHintAttribute uiHintAttribute      = Enumerable.FirstOrDefault <UIHintAttribute>(source, (Func <UIHintAttribute, bool>)(a => string.Equals(a.PresentationLayer, "MVC", StringComparison.OrdinalIgnoreCase))) ?? Enumerable.FirstOrDefault <UIHintAttribute>(source, (Func <UIHintAttribute, bool>)(a => string.IsNullOrEmpty(a.PresentationLayer)));

            if (uiHintAttribute != null)
            {
                annotationsModelMetadata.TemplateHint = uiHintAttribute.UIHint;
            }
            DataTypeAttribute attribute = Enumerable.FirstOrDefault <DataTypeAttribute>(Enumerable.OfType <DataTypeAttribute>((IEnumerable)list));

            if (attribute != null)
            {
                annotationsModelMetadata.DataTypeName = DataTypeUtil.ToDataTypeName(attribute, (Func <DataTypeAttribute, bool>)null);
            }
            EditableAttribute editableAttribute = Enumerable.FirstOrDefault <EditableAttribute>(Enumerable.OfType <EditableAttribute>((IEnumerable)attributes));

            if (editableAttribute != null)
            {
                annotationsModelMetadata.IsReadOnly = !editableAttribute.AllowEdit;
            }
            else
            {
                ReadOnlyAttribute readOnlyAttribute = Enumerable.FirstOrDefault <ReadOnlyAttribute>(Enumerable.OfType <ReadOnlyAttribute>((IEnumerable)list));
                if (readOnlyAttribute != null)
                {
                    annotationsModelMetadata.IsReadOnly = readOnlyAttribute.IsReadOnly;
                }
            }
            DisplayFormatAttribute displayFormatAttribute = Enumerable.FirstOrDefault <DisplayFormatAttribute>(Enumerable.OfType <DisplayFormatAttribute>((IEnumerable)list));

            if (displayFormatAttribute == null && attribute != null)
            {
                displayFormatAttribute = attribute.DisplayFormat;
            }
            if (displayFormatAttribute != null)
            {
                annotationsModelMetadata.NullDisplayText          = displayFormatAttribute.NullDisplayText;
                annotationsModelMetadata.DisplayFormatString      = displayFormatAttribute.DataFormatString;
                annotationsModelMetadata.ConvertEmptyStringToNull = displayFormatAttribute.ConvertEmptyStringToNull;
                if (displayFormatAttribute.ApplyFormatInEditMode)
                {
                    annotationsModelMetadata.EditFormatString = displayFormatAttribute.DataFormatString;
                }
                if (!displayFormatAttribute.HtmlEncode && string.IsNullOrWhiteSpace(annotationsModelMetadata.DataTypeName))
                {
                    annotationsModelMetadata.DataTypeName = DataTypeUtil.HtmlTypeName;
                }
            }
            ScaffoldColumnAttribute scaffoldColumnAttribute = Enumerable.FirstOrDefault <ScaffoldColumnAttribute>(Enumerable.OfType <ScaffoldColumnAttribute>((IEnumerable)list));

            if (scaffoldColumnAttribute != null)
            {
                annotationsModelMetadata.ShowForDisplay = annotationsModelMetadata.ShowForEdit = scaffoldColumnAttribute.Scaffold;
            }
            DisplayAttribute displayAttribute = Enumerable.FirstOrDefault <DisplayAttribute>(Enumerable.OfType <DisplayAttribute>((IEnumerable)attributes));
            string           str = (string)null;

            if (displayAttribute != null)
            {
                annotationsModelMetadata.Description      = displayAttribute.GetDescription();
                annotationsModelMetadata.ShortDisplayName = displayAttribute.GetShortName();
                annotationsModelMetadata.Watermark        = displayAttribute.GetPrompt();
                annotationsModelMetadata.Order            = displayAttribute.GetOrder() ?? 10000;
                str = displayAttribute.GetName();
            }
            if (str != null)
            {
                annotationsModelMetadata.DisplayName = str;
            }
            else
            {
                DisplayNameAttribute displayNameAttribute = Enumerable.FirstOrDefault <DisplayNameAttribute>(Enumerable.OfType <DisplayNameAttribute>((IEnumerable)list));
                if (displayNameAttribute != null)
                {
                    annotationsModelMetadata.DisplayName = displayNameAttribute.DisplayName;
                }
            }
            if (Enumerable.FirstOrDefault <RequiredAttribute>(Enumerable.OfType <RequiredAttribute>((IEnumerable)list)) != null)
            {
                annotationsModelMetadata.IsRequired = true;
            }
            return((ModelMetadata)annotationsModelMetadata);
        }
Exemplo n.º 10
0
        protected override ModelMetadata CreateMetadata(IEnumerable <Attribute> attributes, Type containerType, Func <object> modelAccessor, Type modelType, string propertyName)
        {
            List <Attribute>             attributeList          = new List <Attribute>(attributes);
            DisplayColumnAttribute       displayColumnAttribute = attributeList.OfType <DisplayColumnAttribute>().FirstOrDefault();
            DataAnnotationsModelMetadata result = new DataAnnotationsModelMetadata(this, containerType, modelAccessor, modelType, propertyName, displayColumnAttribute);

            // Do [HiddenInput] before [UIHint], so you can override the template hint
            HiddenInputAttribute hiddenInputAttribute = attributeList.OfType <HiddenInputAttribute>().FirstOrDefault();

            if (hiddenInputAttribute != null)
            {
                result.TemplateHint        = "HiddenInput";
                result.HideSurroundingHtml = !hiddenInputAttribute.DisplayValue;
            }

            // We prefer [UIHint("...", PresentationLayer = "MVC")] but will fall back to [UIHint("...")]
            IEnumerable <UIHintAttribute> uiHintAttributes = attributeList.OfType <UIHintAttribute>();
            UIHintAttribute uiHintAttribute = uiHintAttributes.FirstOrDefault(a => String.Equals(a.PresentationLayer, "MVC", StringComparison.OrdinalIgnoreCase))
                                              ?? uiHintAttributes.FirstOrDefault(a => String.IsNullOrEmpty(a.PresentationLayer));

            if (uiHintAttribute != null)
            {
                result.TemplateHint = uiHintAttribute.UIHint;
            }

            EditableAttribute editable = attributes.OfType <EditableAttribute>().FirstOrDefault();

            if (editable != null)
            {
                result.IsReadOnly = !editable.AllowEdit;
            }
            else
            {
                ReadOnlyAttribute readOnlyAttribute = attributeList.OfType <ReadOnlyAttribute>().FirstOrDefault();
                if (readOnlyAttribute != null)
                {
                    result.IsReadOnly = readOnlyAttribute.IsReadOnly;
                }
            }

            DataTypeAttribute      dataTypeAttribute      = attributeList.OfType <DataTypeAttribute>().FirstOrDefault();
            DisplayFormatAttribute displayFormatAttribute = attributeList.OfType <DisplayFormatAttribute>().FirstOrDefault();

            SetFromDataTypeAndDisplayAttributes(result, dataTypeAttribute, displayFormatAttribute);

            ScaffoldColumnAttribute scaffoldColumnAttribute = attributeList.OfType <ScaffoldColumnAttribute>().FirstOrDefault();

            if (scaffoldColumnAttribute != null)
            {
                result.ShowForDisplay = result.ShowForEdit = scaffoldColumnAttribute.Scaffold;
            }

            DisplayAttribute display = attributes.OfType <DisplayAttribute>().FirstOrDefault();
            string           name    = null;

            if (display != null)
            {
                result.Description      = display.GetDescription();
                result.ShortDisplayName = display.GetShortName();
                result.Watermark        = display.GetPrompt();
                result.Order            = display.GetOrder() ?? ModelMetadata.DefaultOrder;

                name = display.GetName();
            }

            if (name != null)
            {
                result.DisplayName = name;
            }
            else
            {
                DisplayNameAttribute displayNameAttribute = attributeList.OfType <DisplayNameAttribute>().FirstOrDefault();
                if (displayNameAttribute != null)
                {
                    result.DisplayName = displayNameAttribute.DisplayName;
                }
            }

            RequiredAttribute requiredAttribute = attributeList.OfType <RequiredAttribute>().FirstOrDefault();

            if (requiredAttribute != null)
            {
                result.IsRequired = true;
            }

            return(result);
        }
Exemplo n.º 11
0
        public void Ctor_Bool(bool value)
        {
            var attribute = new ScaffoldColumnAttribute(value);

            Assert.Equal(value, attribute.Scaffold);
        }