private TagBuilder ApplyTrColumnHtml(Action <KeyValuePair <string, PropertyInfo>, DisplayAttribute, List <TagBuilder>, object> action, object value) { TagBuilder trBuilder = new TagBuilder("tr"); List <TagBuilder> listColumn = new List <TagBuilder>(); typeof(TModel).GetSortedProperties().ToDictionary(x => x.Name, x => x).ForEach(pair => { DisplayAttribute display = pair.Value.GetCustomAttribute <DisplayAttribute>(); HiddenInputAttribute hidden = pair.Value.GetCustomAttribute <HiddenInputAttribute>(); RadioStateAttribute state = pair.Value.GetCustomAttribute <RadioStateAttribute>(); if (state == null) { if (hidden == null || hidden.DisplayValue == true) { if (display == null || display.GetAutoGenerateField() == null || display.AutoGenerateField == true) { action(pair, display, listColumn, value); } } } }); listColumn.ForEach(d => trBuilder.InnerHtml += d); return(trBuilder); }
public void AutoGenerateField_Get_NotSet_ThrowsInvalidOperationException() { DisplayAttribute attribute = new DisplayAttribute(); Assert.Throws <InvalidOperationException>(() => attribute.AutoGenerateField); Assert.Null(attribute.GetAutoGenerateField()); }
public void AutoGenerateField_Get_Set(bool value) { DisplayAttribute attribute = new DisplayAttribute(); attribute.AutoGenerateField = value; Assert.Equal(value, attribute.AutoGenerateField); Assert.Equal(value, attribute.GetAutoGenerateField()); }
public DataTable ExtendTableBaseClassFirst(DataTable table, Type type) { if ((type.BaseType != null)) { table = ExtendTableBaseClassFirst(table, type.BaseType); } foreach (FieldInfo f in type.GetFields()) { if ((!_useMap.ContainsKey(f.Name))) { DataColumn dc = default(DataColumn); dc = table.Columns.Contains(f.Name) ? table.Columns[f.Name] : table.Columns.Add(f.Name, f.FieldType); _ordinalMap.Add(f.Name, dc.Ordinal); _ordinalNameMap.Add(f.Name, f.Name); _useMap.Add(f.Name, true); } } foreach (PropertyInfo p in type.GetProperties()) { if (!_useMap.ContainsKey(p.Name)) { Type colType = p.PropertyType; if ((colType.IsGenericType) && (object.ReferenceEquals(colType.GetGenericTypeDefinition(), typeof(Nullable <>)))) { colType = colType.GetGenericArguments()[0]; } var name = p.Name; var showColumn = true; DisplayNameAttribute attr = p.GetCustomAttributes(typeof(DisplayNameAttribute), true).SingleOrDefault() as DisplayNameAttribute; if ((attr != null)) { name = attr.DisplayName; } DisplayAttribute attr2 = p.GetCustomAttributes(typeof(DisplayAttribute), true).SingleOrDefault() as DisplayAttribute; if ((attr2 != null)) { name = attr2.Name; var sc = attr2.GetAutoGenerateField(); showColumn = !sc.HasValue || sc.Value; } if (showColumn) { DataColumn dc = (table.Columns.Contains(p.Name) ? table.Columns[p.Name] : table.Columns.Add(p.Name, colType)); _ordinalMap.Add(p.Name, dc.Ordinal); _ordinalNameMap.Add(p.Name, name); } _useMap.Add(p.Name, showColumn); } } return(table); }
private void EnsureDisplay() { DisplayAttribute customAttribute = GetCustomAttribute <DisplayAttribute>(); if (customAttribute == null) { return; } meta.ShouldAdd = customAttribute.GetAutoGenerateField(); meta.GroupDisplayName = customAttribute.GetGroupName(); meta.HeaderText = customAttribute.GetName(); meta.Watermark = customAttribute.GetPrompt(); }
public void DisplayAttribute_AutoGenerateField_Can_Be_Set_And_Retrieved() { DisplayAttribute attr = new DisplayAttribute { AutoGenerateField = true }; Assert.IsTrue(attr.AutoGenerateField, "AutoGenerateField should be true after setting it to true"); Assert.IsTrue(attr.GetAutoGenerateField().Value, "GetAutoGenerateField should be true after setting it to true"); attr = new DisplayAttribute { AutoGenerateField = false }; Assert.IsFalse(attr.AutoGenerateField, "AutoGenerateField should be false after setting it to false"); Assert.IsFalse(attr.GetAutoGenerateField().Value, "GetAutoGenerateField should be false after setting it to false"); }
public void OrderAndAutoGenerateProperties_Success() { var display = new DisplayAttribute() { Order = 1, AutoGenerateField = true, AutoGenerateFilter = false }; Assert.AreEqual(1, display.Order); Assert.AreEqual(1, display.GetOrder()); Assert.AreEqual(true, display.AutoGenerateField); Assert.AreEqual(true, display.GetAutoGenerateField()); Assert.AreEqual(false, display.AutoGenerateFilter); Assert.AreEqual(false, display.GetAutoGenerateFilter()); }
internal static IEnumerable <Tuple <PropertyInfo, object> > GetDisplayAttributePropertyValuePairs(Attribute attributeBase) { DisplayAttribute attribute = (DisplayAttribute)attributeBase; List <Tuple <PropertyInfo, object> > result = new List <Tuple <PropertyInfo, object> >(); if (attribute.GetOrder() != null) { result.Add(GetPropertyValuePair(attribute, x => x.Order)); } if (attribute.GetAutoGenerateField() != null) { result.Add(GetPropertyValuePair(attribute, x => x.AutoGenerateField)); } result.Add(GetPropertyValuePair(attribute, x => x.Name)); result.Add(GetPropertyValuePair(attribute, x => x.ShortName)); result.Add(GetPropertyValuePair(attribute, x => x.Description)); return(result); }
public static DisplayAttribute Copy(this DisplayAttribute srcAttribute) { if (srcAttribute == null) { return(null); } var copy = new DisplayAttribute { Name = srcAttribute.Name, GroupName = srcAttribute.GroupName, Description = srcAttribute.Description, ResourceType = srcAttribute.ResourceType, ShortName = srcAttribute.ShortName, Prompt = srcAttribute.Prompt }; var order = srcAttribute.GetOrder(); if (order != null) { copy.Order = order.Value; } var autoGenerateField = srcAttribute.GetAutoGenerateField(); if (autoGenerateField != null) { copy.AutoGenerateField = autoGenerateField.Value; } var autoGenerateFilter = srcAttribute.GetAutoGenerateFilter(); if (autoGenerateFilter != null) { copy.AutoGenerateFilter = autoGenerateFilter.Value; } return(copy); }
public static DisplayAttribute Copy(this DisplayAttribute attribute) { if (attribute == null) { return(null); } // DisplayAttribute is sealed, so it's safe to copy. var copy = new DisplayAttribute { Name = attribute.Name, GroupName = attribute.GroupName, Description = attribute.Description, ResourceType = attribute.ResourceType, ShortName = attribute.ShortName, Prompt = attribute.Prompt, Order = attribute.GetOrder() ?? 10000, AutoGenerateField = attribute.GetAutoGenerateField() ?? true, AutoGenerateFilter = attribute.GetAutoGenerateFilter() ?? true }; return(copy); }
/// <exclude/> public TableBuilderT(string id = null, GridTreeDataModel model = null, string url = null, TablePaginationOption sidePagination = TablePaginationOption.none, object htmlAttributes = null) : base(id, model, url, sidePagination, htmlAttributes) { typeof(TModel).GetSortedProperties().ToDictionary(x => x.Name, x => x).ForEach(pair => { DisplayAttribute display = pair.Value.GetCustomAttribute <DisplayAttribute>(); HiddenInputAttribute hidden = pair.Value.GetCustomAttribute <HiddenInputAttribute>(); if (hidden == null || hidden.DisplayValue == true) { if (display == null || display.GetAutoGenerateField() == null || display.AutoGenerateField == true) { if (display != null && !string.IsNullOrEmpty(display.GetName())) { Column(display.GetName(), pair.Key); } else { Column(pair.Key.SplitCamelCase(), pair.Key); } } } }); }
public bool?GetAutoGenerateField() { return(_innerAttribute.GetAutoGenerateField()); }
public override AttributeDeclaration GetAttributeDeclaration(Attribute attribute) { DisplayAttribute displayAttribute = (DisplayAttribute)attribute; AttributeDeclaration attributeDeclaration = new AttributeDeclaration(typeof(DisplayAttribute)); // By convention, the attribute parameters are not validated until an attempt is made to // access the resources. We do the following probe merely to trigger this validation process. // An InvalidOperationException will be thrown if the attribute is ill-formed. Type attributeType = attribute.GetType(); try { displayAttribute.GetName(); } catch (InvalidOperationException ex) { throw new AttributeBuilderException(ex, attributeType, "Name"); } try { displayAttribute.GetShortName(); } catch (InvalidOperationException ex) { throw new AttributeBuilderException(ex, attributeType, "ShortName"); } try { displayAttribute.GetDescription(); } catch (InvalidOperationException ex) { throw new AttributeBuilderException(ex, attributeType, "Description"); } try { displayAttribute.GetPrompt(); } catch (InvalidOperationException ex) { throw new AttributeBuilderException(ex, attributeType, "Prompt"); } // Add AutoGenerateField if (displayAttribute.GetAutoGenerateField().HasValue) { attributeDeclaration.NamedParameters.Add("AutoGenerateField", displayAttribute.AutoGenerateField); } // Add AutoGenerateFilter if (displayAttribute.GetAutoGenerateFilter().HasValue) { attributeDeclaration.NamedParameters.Add("AutoGenerateFilter", displayAttribute.AutoGenerateFilter); } // Add Description if (!string.IsNullOrEmpty(displayAttribute.Description)) { attributeDeclaration.NamedParameters.Add("Description", displayAttribute.Description); } // Add GroupName if (!string.IsNullOrEmpty(displayAttribute.GroupName)) { attributeDeclaration.NamedParameters.Add("GroupName", displayAttribute.GroupName); } // Add Name if (!string.IsNullOrEmpty(displayAttribute.Name)) { attributeDeclaration.NamedParameters.Add("Name", displayAttribute.Name); } // Add Order if (displayAttribute.GetOrder().HasValue) { attributeDeclaration.NamedParameters.Add("Order", displayAttribute.Order); } // Add Prompt if (!string.IsNullOrEmpty(displayAttribute.Prompt)) { attributeDeclaration.NamedParameters.Add("Prompt", displayAttribute.Prompt); } // Add ResourceType if (displayAttribute.ResourceType != null) { attributeDeclaration.NamedParameters.Add("ResourceType", displayAttribute.ResourceType); } // Add ShortName if (!string.IsNullOrEmpty(displayAttribute.ShortName)) { attributeDeclaration.NamedParameters.Add("ShortName", displayAttribute.ShortName); } return(attributeDeclaration); }
public void DisplayAttribute_AutoGenerateField_Can_Be_Set_And_Retrieved() { DisplayAttribute attr = new DisplayAttribute { AutoGenerateField = true }; Assert.IsTrue(attr.AutoGenerateField, "AutoGenerateField should be true after setting it to true"); Assert.IsTrue(attr.GetAutoGenerateField().Value, "GetAutoGenerateField should be true after setting it to true"); attr = new DisplayAttribute { AutoGenerateField = false }; Assert.IsFalse(attr.AutoGenerateField, "AutoGenerateField should be false after setting it to false"); Assert.IsFalse(attr.GetAutoGenerateField().Value, "GetAutoGenerateField should be false after setting it to false"); }
private void DiscoverObject() { this.displays.Clear(); this.properties.Clear(); this.bindables.Clear(); this.bindings.Clear(); #if !SILVERLIGHT this.rules.Clear(); #endif this.controls.Clear(); this.validations.Clear(); if (this.CurrentItem == null) { return; } Type dataType = this.CurrentItem.GetType(); PropertyInfo[] properties; //Code for dynamic Types.... /*var meta = this.CurrentItem as IDynamicMetaObjectProvider; * if (meta != null) * { * List<PropertyInfo> tmpLst = new List<PropertyInfo>(); * var metaObject = meta.GetMetaObject(System.Linq.Expressions.Expression.Constant(this.CurrentItem)); * var membernames = metaObject.GetDynamicMemberNames(); * foreach (var membername in membernames) * { * var binder = Microsoft.CSharp.RuntimeBinder.Binder.GetMember(CSharpBinderFlags.None, membername, dataType, new[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) }); * var callsite = CallSite<Func<CallSite, object, object>>.Create(binder); * var item = callsite.Target(callsite, this.CurrentItem); * } * } * else*/ { properties = GetPropertyInfos(dataType); } BindableAttribute bindable = ((BindableAttribute[])dataType.GetCustomAttributes(typeof(System.ComponentModel.BindableAttribute), true)).FirstOrDefault(); foreach (PropertyInfo property in properties) { BindableAttribute propBindable = ((BindableAttribute[])property.GetCustomAttributes(typeof(System.ComponentModel.BindableAttribute), true)).FirstOrDefault(); // Check if Readable if (!property.CanRead) { continue; } // Check if Bindable if ((bindable != null && !bindable.Bindable && propBindable == null) || (bindable != null && !bindable.Bindable && propBindable != null && !propBindable.Bindable) || (propBindable != null && !propBindable.Bindable)) { continue; } DisplayAttribute propDisplay = ((DisplayAttribute[])Attribute.GetCustomAttributes(property, typeof(DisplayAttribute), true)).FirstOrDefault(); EditableAttribute propEditable = ((EditableAttribute[])Attribute.GetCustomAttributes(property, typeof(EditableAttribute), true)).FirstOrDefault(); List <ValidationAttribute> validations = new List <ValidationAttribute>((ValidationAttribute[])Attribute.GetCustomAttributes(property, typeof(ValidationAttribute), true)); if (propDisplay == null) { propDisplay = new DisplayAttribute() { AutoGenerateField = true, Name = property.Name, ShortName = property.Name, Order = 10000, Prompt = null, GroupName = null, Description = null } } ; if (propEditable == null) { propEditable = new EditableAttribute(!this.DefaultReadOnly) { AllowInitialValue = true } } ; bool setPrivate = true; if (property.GetSetMethod(true) != null) { setPrivate = !property.GetSetMethod(true).IsPublic; } if (propDisplay.GetAutoGenerateField().HasValue&& !propDisplay.AutoGenerateField) { continue; } if (PropertyDisplayInformations != null) { var prpDisplay = PropertyDisplayInformations.FirstOrDefault(x => x.Name == property.Name); if (prpDisplay == null || !prpDisplay.Visible) { continue; } propDisplay.Order = prpDisplay.Order; } if (bindable != null && propBindable == null) { if ((!property.CanWrite || !propEditable.AllowEdit || setPrivate) && bindable.Direction == BindingDirection.TwoWay) { this.bindables.Add(property.Name, new BindableAttribute(true, BindingDirection.OneWay)); } else { this.bindables.Add(property.Name, new BindableAttribute(true, bindable.Direction)); } } else if (propBindable != null) { if ((!property.CanWrite || !propEditable.AllowEdit || setPrivate) && propBindable.Direction == BindingDirection.TwoWay) { this.bindables.Add(property.Name, new BindableAttribute(true, BindingDirection.OneWay)); } else { this.bindables.Add(property.Name, new BindableAttribute(true, propBindable.Direction)); } } else { if (!this.bindables.ContainsKey(property.Name)) { if (!property.CanWrite || !propEditable.AllowEdit || setPrivate) { this.bindables.Add(property.Name, new BindableAttribute(true, BindingDirection.OneWay)); } else { this.bindables.Add(property.Name, new BindableAttribute(true, BindingDirection.TwoWay)); } } } if (!this.validations.ContainsKey(property.Name)) { this.validations.Add(property.Name, validations); this.displays.Add(property.Name, propDisplay); this.properties.Add(property.Name, property); } } }
public void AutoGenerateField_Get_NotSet_ThrowsInvalidOperationException() { DisplayAttribute attribute = new DisplayAttribute(); Assert.Throws<InvalidOperationException>(() => attribute.AutoGenerateField); Assert.Null(attribute.GetAutoGenerateField()); }
public void AutoGenerateField_Get_Set(bool value) { DisplayAttribute attribute = new DisplayAttribute(); attribute.AutoGenerateField = value; Assert.Equal(value, attribute.AutoGenerateField); Assert.Equal(value, attribute.GetAutoGenerateField()); }