protected override object GetValueFromXml(XElement root, XName name, PropertyInfo prop)
        {
            var isAttribute = false;

            // Check for the DeserializeAs attribute on the property
            var options = prop.GetAttribute<DeserializeAsAttribute>();

            if (options != null)
            {
                name = options.Name ?? name;
                isAttribute = options.Attribute;
            }

            if (isAttribute)
            {
                var attributeVal = GetAttributeByName(root, name);

                if (attributeVal != null)
                {
                    return attributeVal.Value;
                }
            }

            return base.GetValueFromXml(root, name, prop);
        }
Beispiel #2
0
        private static Field GetField(PropertyInfo info, Type entityType)
        {
            if (!info.GetCustomAttributes(true).Any())
            {
                return new Field(info, entityType)
                           {
                               FieldName = info.Name,
                               FieldType = info.PropertyType,
                               IsKey = false,
                               IsIdentity = false
                           };
            }

            var dbField = info.GetAttribute<DbFieldAttribute>();
            if (dbField != null)
            {
                return new Field(info, entityType)
                           {
                               FieldName = GetFieldName(dbField, info),
                               FieldType = info.PropertyType,
                               IsKey = false,
                               IsIdentity = false
                           };
            }

            return new Field(info, entityType)
                       {
                           FieldName = GetKeyFieldName(info),
                           FieldType = info.PropertyType,
                           IsKey = IsKeyField(info),
                           IsIdentity = IsIdentityField(info)
                       };
        }
		public override string LabelForPropertyConvention(PropertyInfo propertyInfo)
		{
			if (propertyInfo.AttributeExists<LabelAttribute>())
				return propertyInfo.GetAttribute<LabelAttribute>().Label;

			return base.LabelForPropertyConvention(propertyInfo);
		}
Beispiel #4
0
        /// <summary>
        /// 指定属性元数据,初始化一个 <see cref="PropertyMapper"/> 类的新实例。
        /// </summary>
        /// <param name="typeMapper">类型的映射器。</param>
        /// <param name="property">成员的属性元数据。</param>
        public PropertyMapper(TypeMapper typeMapper, PropertyInfo property)
            : base(property)
        {
            if(typeMapper == null) throw new ArgumentNullException(nameof(typeMapper));
            this.TypeMapper = typeMapper;
            this.IsIgnore = property.GetAttribute<IgnoreAttribute>() != null;
            this._LazyTypeDefaultValue = new Lazy<object>(property.PropertyType.GetDefaultValue);

            var aliasAttr = property.GetAttribute<IAliasAttribute>();
            this.Name = aliasAttr != null && aliasAttr.Name != null
                ? aliasAttr.Name
                : property.Name;

            var keyAttr = property.GetAttribute<IKeyAttribute>();
            this.IsKey = keyAttr != null && keyAttr.IsKey;
        }
Beispiel #5
0
 /// <summary>Creates a new <see cref="Descriptor"/> instance from a given property.</summary>
 /// <param name="property">The property from which the descriptor will be created.</param>
 public Descriptor(PropertyInfo property) {
    var attr = property.GetAttribute<DescriptorAttribute>();
    if (attr != null) {
       ParseXml(attr.Markup);
    }
    Subject = property;
 }
 public override bool CanHandle(PropertyInfo propertyInfo)
 {
     if (propertyInfo.AttributeExists<DataTypeAttribute>())
     {
         return propertyInfo.GetAttribute<DataTypeAttribute>().DataType == DataType.EmailAddress;
     }
     return false;
 }
 public void AssingDefaultValue(PropertyInfo prop)
 {
     var attrib = prop.GetAttribute<SerializePropertyAttribute>();
     if (attrib.CreateNewAsDefaultValue)
         prop.SetValue(this, Activator.CreateInstance(prop.PropertyType), null);
     else if (attrib.DefaultValue != null)
         prop.SetValue(this, attrib.DefaultValue, null);
 }
Beispiel #8
0
        /// <summary>
        /// 指定属性元数据,初始化一个 <see cref="PropertyMapper"/> 类的新实例。
        /// </summary>
        /// <param name="typeMapper">类型的映射器。</param>
        /// <param name="property">成员的属性元数据。</param>
        public PropertyMapper(TypeMapper typeMapper, PropertyInfo property)
            : base(property)
        {
            if(typeMapper == null) throw new ArgumentNullException(nameof(typeMapper));
            this.TypeMapper = typeMapper;
            this.IsIgnore = property.GetAttribute<IgnoreAttribute>() != null;
            this._LazyTypeDefaultValue = new Lazy<object>(property.PropertyType.GetDefaultValue);

            var aliasAttr = property.GetAttribute<IAliasAttribute>();
            this.Name = aliasAttr != null && aliasAttr.Name != null
                ? aliasAttr.Name
                : property.Name;

            var keyAttr = property.GetAttribute<IKeyAttribute>();
            this.IsKey = (keyAttr != null && keyAttr.IsKey) || string.Equals(property.Name, DbExtensions.DefaultKeyName, StringComparison.CurrentCultureIgnoreCase);

            this.Validators = property.GetAttributes<IPropertyValidator>().ToArray();
        }
Beispiel #9
0
 public void CopyFromModel(PropertyInfo vm, object model, PropertyInfo[] modelProps)
 {
     var ckf = vm.GetAttribute<FieldInfoAttribute>().CheckboxField;
     var ckpi = modelProps.Single(ss => ss.Name == ckf);
     var ck = ckpi.GetValue(model, null) as bool?;
     var m = modelProps.FirstOrDefault(mm => mm.Name == vm.Name);
     Number = ((string)m.GetValue(model, null)).FmtFone();
     ReceiveText = ck ?? false;
 }
Beispiel #10
0
 public void CopyFromModel(PropertyInfo vm, object model, PropertyInfo[] modelProps)
 {
     var ckf = vm.GetAttribute<FieldInfoAttribute>().CheckboxField;
     var ckpi = modelProps.Single(ss => ss.Name == ckf);
     var ck = ckpi.GetValue(model, null) as bool?;
     var m = modelProps.FirstOrDefault(mm => mm.Name == vm.Name);
     Debug.Assert(m != null, "m != null");
     Address = ((string)m.GetValue(model, null));
     Send = ck ?? false;
 }
Beispiel #11
0
        public static object GetDefaultValue(this PropertyInfo property)
        {
            var att = property.GetAttribute <DefaultValueAttribute>(false);

            if (null != att)
            {
                return(att.Value);
            }
            return(string.Empty);
        }
        public object GetInitialValue(PropertyInfo accessor)
        {
            var defaultAttrib = accessor.GetAttribute<DefaultValueAttribute>();

            if (defaultAttrib != null)
            {
                return defaultAttrib.Value;
            }

            return accessor.PropertyType.GetDefaultValue();
        }
        public static dynamic GetDefaultValue(this PropertyInfo property)
        {
            System.ComponentModel.DefaultValueAttribute defaultValue = property.GetAttribute <System.ComponentModel.DefaultValueAttribute>();

            if (defaultValue != null)
            {
                return(defaultValue.Value);
            }

            return(property.PropertyType.GetDefaultValue());
        }
        private static object GetDefaultValue(PropertyInfo member)
        {
            object defaultValue = null;
            var defaultAttribute = member.GetAttribute<DefaultAttribute>();
            if (defaultAttribute != null)
            {
                defaultValue = defaultAttribute.Default();
            }

            if (defaultValue == null)
            {
                var attribute = member.GetAttribute<DefaultValueAttribute>();
                if (attribute != null)
                {
                    defaultValue = attribute.Value;
                }
            }

            return defaultValue;
        }
Beispiel #15
0
        protected static bool MustBeHidden(PropertyInfo propertyInfo)
        {
            var hiddenInputAttribute = propertyInfo.GetAttribute<HiddenInputAttribute>();
            return (hiddenInputAttribute != null && !hiddenInputAttribute.DisplayValue);
            //return true;

            //var displayAttribute = propertyInfo.GetAttribute<DisplayAttribute>();
            //if (displayAttribute == null)
            //    return false;

            //return false;
        }
Beispiel #16
0
        public static Parameter createParameter(PropertyInfo propertyInfo, IRouteDefinition route)
        {
            var parameter = new Parameter
                                {
                                    name = propertyInfo.Name,
                                    dataType = propertyInfo.PropertyType.Name,
                                    paramType = "post",
                                    allowMultiple = false,
                                    required = propertyInfo.HasAttribute<RequiredAttribute>(),
                                    description = propertyInfo.GetAttribute<DescriptionAttribute>(a => a.Description),
                                    defaultValue = propertyInfo.GetAttribute<DefaultValueAttribute>(a => a.Value.ToString()),
                                    allowableValues = getAllowableValues(propertyInfo)
                                };

            if (route.Input.RouteParameters.Any(r => r.Name == propertyInfo.Name))
                parameter.paramType = "path";

            if (route.Input.QueryParameters.Any(r => r.Name == propertyInfo.Name))
                parameter.paramType = "query";

            return parameter;
        }
Beispiel #17
0
        public static void UpdateTextBlobProperty(object element, PropertyInfo relationshipProperty)
        {
            var type = element.GetType();
            var relationshipType = relationshipProperty.PropertyType;

            Debug.Assert(relationshipType != typeof(string), "TextBlob property is already a string");

            var textblobAttribute = relationshipProperty.GetAttribute<TextBlobAttribute>();
            var textProperty = type.GetRuntimeProperty(textblobAttribute.TextProperty);
            Debug.Assert(textProperty != null && textProperty.PropertyType == typeof(string), "Text property for TextBlob relationship not found");

            var value = relationshipProperty.GetValue(element, null);
            var textValue = value != null ? GetTextSerializer().Serialize(value) : null;

            textProperty.SetValue(element, textValue, null);
        }
Beispiel #18
0
        public static void GetTextBlobChild(object element, PropertyInfo relationshipProperty)
        {
            var type = element.GetType();
            var relationshipType = relationshipProperty.PropertyType;

            Debug.Assert(relationshipType != typeof(string), "TextBlob property is already a string");

            var textblobAttribute = relationshipProperty.GetAttribute<TextBlobAttribute>();
            var textProperty = type.GetProperty(textblobAttribute.TextProperty, typeof (string));
            Debug.Assert(textProperty != null, "Text property for TextBlob relationship not found");

            var textValue = (string)textProperty.GetValue(element, null);
            var value = textValue != null ? GetTextSerializer().Deserialize(textValue, relationshipType) : null;

            relationshipProperty.SetValue(element, value, null);
        }
Beispiel #19
0
 public List<ChangeDetail> CopyToModel(PropertyInfo vm, object model, PropertyInfo[] modelProps, bool track)
 {
     var changes = new List<ChangeDetail>();
     var ckf = vm.GetAttribute<FieldInfoAttribute>().CheckboxField;
     var ckpi = modelProps.Single(mm => mm.Name == ckf);
     if (track)
     {
         model.UpdateValue(changes, vm.Name, Number.GetDigits());
         model.UpdateValue(changes, ckf, ReceiveText);
         return changes;
     }
     var ci = modelProps.FirstOrDefault(ss => ss.Name == vm.Name);
     Debug.Assert(ci != null, "ci != null");
     ci.SetValue(model, Number.GetDigits(), null);
     ckpi.SetValue(model, ReceiveText, null);
     return changes;
 }
Beispiel #20
0
        public string RenderDropDownForEnum(HtmlHelper htmlHelper, PropertyInfo property, object value)
        {
            var items = GetDropDownItems(property);

            var dropDownHtml = htmlHelper
                .DropDownList(property.Name, items, UiTexts.Editor_DropDown_EmptyValue, new { @class = "form-control" })
                .ToHtmlString();

            dropDownHtml = this.SelectProperItem(dropDownHtml, value);

            if (property.GetAttribute<RequiredAttribute>() == null)
            {
                dropDownHtml = dropDownHtml.Replace("data-val=\"true\"", string.Empty);
            }

            var validationMessage = htmlHelper.ValidationMessage(property.Name, string.Empty, new { @class = "text-danger" });
            if (validationMessage != null)
            {
                dropDownHtml += validationMessage.ToHtmlString();
            }

            return dropDownHtml;
        }
 private static byte GetColumnScale(PropertyInfo propertyInfo)
 {
     var scaleAttribute = propertyInfo.GetAttribute<ScaleAttribute>();
     Byte scale = (null == scaleAttribute)
         ? (byte)DefaultDecimalParameterScale
         : scaleAttribute.Value;
     return scale;
 }
 private static byte GetColumnPrecision(PropertyInfo propertyInfo)
 {
     var precisionAttribute = propertyInfo.GetAttribute<PrecisionAttribute>();
     Byte precision = (null == precisionAttribute)
         ? (byte)DefaultDecimalParameterPrecision
         : precisionAttribute.Value;
     return precision;
 }
Beispiel #23
0
 /// <summary>
 /// Asserts whether the PropertyInfo has a certain Attribute of T (Generic)
 /// </summary>
 /// <typeparam name="T">Attribute</typeparam>
 /// <param name="prop">The PropertyInfo being probed</param>
 /// <returns>Returns bool (true/false)</returns>
 public static bool HasAttribute <T>(this PropertyInfo prop) where T : Attribute
 {
     return(prop.GetAttribute <T>() != null);
 }
        private static string GetFormatString(PropertyInfo property)
        {
            DisplayFormatAttribute att = property.GetAttribute<DisplayFormatAttribute>(true);
            if (att == null || att.DataFormatString.IsNullEmptyOrWhiteSpace())
            {
                MetadataTypeAttribute metaAtt = property.DeclaringType.GetAttribute<MetadataTypeAttribute>(true);
                if (metaAtt != null && metaAtt.MetadataClassType.GetProperty(property.Name) != null)
                    return GetFormatString(metaAtt.MetadataClassType.GetProperty(property.Name));

                return null;
            }
            return att.DataFormatString;
        }
Beispiel #25
0
        public static IValueProvider CreateValueProvider(object editableObject, PropertyInfo propertyInfo)
        {
            IValueProvider result = null;

            if (propertyInfo.HasAttribute<PropertyGridCustomValueProvider>())
            {
                var attribute = propertyInfo.GetAttribute<PropertyGridCustomValueProvider>();
                var derivedType = attribute.CustomValueProviderType;
                var instance = (PropertyValue)Activator.CreateInstance(derivedType);
                instance.Property = propertyInfo;
                instance.Parent = editableObject;
                instance.Type = propertyInfo.PropertyType;
                return instance;
            }
            else if (propertyInfo.PropertyType.HasInterface<IValueProvider>()
                && (result = (IValueProvider)propertyInfo.GetValue(editableObject, null)) != null)
            {
                return result;
            }
            else
            {
                return new PropertyValue(propertyInfo, editableObject);
            }
        }
        public object GetValue(IContentData contentData, PropertyInfo property)
        {
            // define if property is required
            var requiredAnnotation = property.GetAttribute<RequiredAttribute>();

            // get annotation attribute
            var annotation = property.GetAnnotation<CmsReferenceAttribute>();

            // get reference property name set by attribute or default
            var referencePropertyName = annotation.LinkFieldName ?? property.Name + "Link";

            // lookup reference property
            var referenceProperty = contentData.Property[referencePropertyName] as PropertyPageReference;

            if (referenceProperty != null)
            // if reference property found
            {
                // get link to a referenced page
                var link = referenceProperty.ContentLink;

                if (!ContentReference.IsNullOrEmpty(link))
                // and if it's not empty
                {
                    // load referenced page and cast it to the target property type.
                    var result = ContentLoader.Get<PageData>(link).Cast(property.PropertyType);

                    if (result != null)
                    {
                        return result;
                    }
                }
            }

            if (requiredAnnotation == null)
            // if property is not marked as required
            {
                return null;
            }

            // otherwise get error message
            string errorMessageFormat;

            if (!string.IsNullOrEmpty(requiredAnnotation.ErrorMessageResourceName))
            {
                errorMessageFormat = LocalizationService.GetString(requiredAnnotation.ErrorMessageResourceName);
            }
            else if (!string.IsNullOrEmpty(requiredAnnotation.ErrorMessage))
            {
                errorMessageFormat = requiredAnnotation.ErrorMessage;
            }
            else
            {
                errorMessageFormat = LocalizationService.GetString("EPiProperties/PropertyRequiredFormat", "Required property '{0}' is not properly set on the page #{1} of type '{2}'. ");
            }

            var content = contentData as IContent;

            if (content != null)
            {
                var contentType = ContentTypeRepository.Load(content.ContentTypeID);

                var errorMessage = string.Format(errorMessageFormat,
                    property.Name,
                    content.ContentLink.ID,
                    contentType.DisplayName);

                throw new ApplicationException(errorMessage);
            }
            else
            {
                throw new ApplicationException(string.Format(errorMessageFormat,
                    property.Name,
                    "?",
                    "?")); // TODO
            }
        }
Beispiel #27
0
        public List<ChangeDetail> CopyToModel(PropertyInfo vm, object model, PropertyInfo[] modelProps, bool track)
        {
            var changes = new List<ChangeDetail>();
            string altname = vm.Name + "Id";
            var attr = vm.GetAttribute<FieldInfoAttribute>();
            if (attr != null && attr.IdField.HasValue())
                altname = attr.IdField;
            var mid = modelProps.FirstOrDefault(mm => mm.Name == altname || mm.Name == vm.Name);

            if (mid == null)
                return changes;

            if (mid.PropertyType.Name == "CodeInfo")
            {
                Util.SetPropertyEx(model, vm.Name + ".Value", Value == "0" ? null : Value);
            }
            else
            {
                if (track)
                {
                    if (mid.PropertyType == typeof(int?) || mid.PropertyType == typeof(int))
                        if (Value == "0")
                            model.UpdateValue(changes, altname, null);
                        else
                            model.UpdateValue(changes, altname, Value.ToInt());
                    return changes;
                }
                mid.SetPropertyFromText(model, Value == "0" ? null : Value);
            }

            return changes;
        }
Beispiel #28
0
 public void CopyFromModel(PropertyInfo vm, object existing, object model, PropertyInfo[] modelProps)
 {
     string altname = vm.Name + "Id";
     var fiattr = vm.GetAttribute<FieldInfoAttribute>();
     if (fiattr != null && fiattr.IdField.HasValue())
         altname = fiattr.IdField;
     var mid = modelProps.FirstOrDefault(mm => mm.Name == altname || mm.Name == vm.Name);
     var midvalue = mid.GetValue(model, null);
     if (mid.PropertyType == typeof(int?) || mid.PropertyType == typeof(int))
         Value = midvalue.ToInt().ToString();
     else if (midvalue == null)
         Value = null;
     else if (mid.PropertyType.Name == "CodeInfo")
         Value = ((CodeInfo) midvalue).Value;
     else
         Value = midvalue.ToString();
     var cinfo = midvalue as CodeInfo;
     if (cinfo == null && existing != null)
         cinfo = existing as CodeInfo;
     if (items == null && cinfo != null && cinfo.items != null)
         items = cinfo.items;
     Name = vm.Name;
 }
 private UiaPropertyInfoHelper GetPropertyHelper(PropertyInfo pInfo)
 {
     var propertyAttr = pInfo.GetAttribute<PatternPropertyAttribute>(); // can'be null as otherwise it wouldn't get into this method
     var guid = propertyAttr.Guid;
     var programmaticName = pInfo.Name;
     var uiaType = UiaTypesHelper.TypeToAutomationType(pInfo.PropertyType);
     return new UiaPropertyInfoHelper(guid, programmaticName, uiaType, pInfo.GetPropertyGetter());
 }
 private static int GetColumnSize(PropertyInfo propertyInfo)
 {
     var sizeAttribute = propertyInfo.GetAttribute<SizeAttribute>();
     int size = (null == sizeAttribute) ? DefaultSizeAttribute : sizeAttribute.Value;
     return size;
 }
Beispiel #31
0
            private static object ConvertValue(PropertyInfo property, object obj)
            {
                var value = property.GetValue(obj, null);
                var attribute = property.GetAttribute<DataMemberAttribute>(false);
                if (attribute != null && value.IsNull() && attribute.EmitDefaultValue) {
                    return property.PropertyType.GetDefault();
                }

                return value;
            }
Beispiel #32
0
            private static string ConvertKey(PropertyInfo property)
            {
                var attribute = property.GetAttribute<DataMemberAttribute>(false);
                if (attribute != null && !string.IsNullOrEmpty(attribute.Name)) {
                    return attribute.Name;
                }

                return property.Name;
            }
Beispiel #33
0
        /// <summary>
        /// Initializes a new instance of the <see cref="InspectedProperty"/> class.
        /// </summary>
        /// <param name="type">
        /// The inspected type.
        /// </param>
        /// <param name="propertyInfo">
        /// The property info.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="type"/> is null.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="propertyInfo"/> is null.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// If <paramref name="propertyInfo"/> 's declaring type has not been set.
        /// </exception>
        internal InspectedProperty(Type type, PropertyInfo propertyInfo)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            if (propertyInfo == null)
            {
                throw new ArgumentNullException("propertyInfo");
            }

            if (propertyInfo.DeclaringType == null)
            {
                throw new ArgumentException("fieldInfo declaring type has not been set");
            }

            this.Name = propertyInfo.SerializableName();
            this.InspectedType = type;
            this.PropertyType = propertyInfo.PropertyType;
            this.IsNotNullableValueType = this.PropertyType.IsNotNullableValueType();

            if (propertyInfo.DeclaringType != propertyInfo.ReflectedType)
            {
                propertyInfo = propertyInfo.DeclaringType.GetProperty(propertyInfo.Name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
            }

            try
            {
                this.Getter = DelegateFactory.CreateGetter(propertyInfo);
                this.Setter = DelegateFactory.CreateSetter(propertyInfo);
            }
            catch (Exception exception)
            {
                Logging.TraceException(exception);
            }

            this.Member = propertyInfo;

#if!HT4O_SERIALIZATION

            this.IdAttribute = propertyInfo.GetAttribute<IdAttribute>();

#endif

            this.IsTransient = propertyInfo.HasAttribute<TransientAttribute>() || propertyInfo.HasAttribute<IgnoreDataMemberAttribute>();
            this.Ignore = propertyInfo.HasAttribute<IgnoreAttribute>();
        }