protected void MontaComboboxfiltros(DAL obj) { PropertyInfo[] props = obj.GetType().GetProperties(); int instancePropsCount = props.Count(); InicializaCombobox(); for (int i = 0; i < instancePropsCount; i++) { if (IsColumn(props[i])) { System.ComponentModel.DisplayNameAttribute displayName = null; try { displayName = ((System.ComponentModel.DisplayNameAttribute)(props[i].GetCustomAttributes(typeof(System.ComponentModel.DisplayNameAttribute), false)[0])); } catch { displayName = null; } if (displayName != null) { ItensComboBox item = new ItensComboBox(); item.displayName = displayName.DisplayName; item.fieldName = props[i].Name; ListaItensComboBox.Add(item); cbbFiltro.Items.Add(item.displayName); } } } cbbFiltro.SelectedIndex = 0; }
public OptionPropertyValue(PropertyInfo propertyInfo, object owner) { _propertyInfo = propertyInfo; _owner = owner; _displayName = _propertyInfo.GetCustomAttribute<DisplayNameAttribute>(); _description = _propertyInfo.GetCustomAttribute<DescriptionAttribute>(); }
public OptionPropertyValue(PropertyInfo propertyInfo, object owner, PropertyInfo defaultPropertyInfo) { this.propertyInfo = propertyInfo; this.owner = owner; displayName = this.propertyInfo.GetCustomAttribute<DisplayNameAttribute>(); description = this.propertyInfo.GetCustomAttribute<DescriptionAttribute>(); if (defaultPropertyInfo != null) DefaultValue = (string)defaultPropertyInfo.GetValue(owner); }
protected override ModelMetadata CreateMetadata(IEnumerable<Attribute> attributes, Type containerType, Func<object> modelAccessor, Type modelType, string propertyName) { var items = attributes.ToList(); for (var i = 0; i < items.Count(); i++) { if (!(items[i] is DisplayNameAttribute)) { continue; } items[i] = new DisplayNameAttribute(DataAnnotationsResource.ResourceManager.GetString(propertyName)); } return base.CreateMetadata(items, containerType, modelAccessor, modelType, propertyName); }
/// <summary> /// 获取属性说明符上的显示名称 /// </summary> /// <param name="member"></param> /// <returns></returns> public static string GetDisplayName(this Type type) { if (Attribute.IsDefined(type, typeof(ResourceDisplayNameAttribute))) { ResourceDisplayNameAttribute att = Attribute.GetCustomAttribute(type, typeof(ResourceDisplayNameAttribute)) as ResourceDisplayNameAttribute; return(att.ResourceDisplayName); } if (Attribute.IsDefined(type, typeof(System.ComponentModel.DisplayNameAttribute))) { System.ComponentModel.DisplayNameAttribute att = Attribute.GetCustomAttribute(type, typeof(System.ComponentModel.DisplayNameAttribute)) as System.ComponentModel.DisplayNameAttribute; return(att.DisplayName); } return(null); }
public int Compare(PropertyInfo x1, PropertyInfo x2) { DisplayAttribute descriptionAttribute1 = GetAttribute <DisplayAttribute>(x1); DisplayAttribute descriptionAttribute2 = GetAttribute <DisplayAttribute>(x2); #if UseDataAnnotations string s1 = descriptionAttribute1 == null ? x1.Name : descriptionAttribute1.GetName(); string s2 = descriptionAttribute2 == null ? x2.Name : descriptionAttribute2.GetName(); return(s1.CompareTo(s2)); #else string s1 = descriptionAttribute1 == null ? x1.Name : descriptionAttribute1.DisplayName; string s2 = descriptionAttribute2 == null ? x2.Name : descriptionAttribute2.DisplayName; return(s1.CompareTo(s2)); #endif }
/// <summary> /// Creates a new field instance for the specified property. /// </summary> /// <param name="property">The property name.</param> /// <param name="databaseAttribute">The database attribute</param> public DbField(PropertyInfo property, DbAttribute databaseAttribute) { // Set the property. this.property = property; // Set the database attribute. this.databaseAttribute = databaseAttribute; // Set the database name. this.databaseName = this.databaseAttribute.Name; // Get the display name attributes. object[] displayNameAttributes = property.GetCustomAttributes(typeof(DisplayNameAttribute), true); // If at least one attribute exists, set the display name attribute. if (displayNameAttributes.Length > 0) this.displayNameAttribute = displayNameAttributes[0] as DisplayNameAttribute; // Get the description attributes. object[] descriptionAttributes = property.GetCustomAttributes(typeof(DescriptionAttribute), true); // If at least one attribute exists, set the description attribute. if (descriptionAttributes.Length > 0) this.descriptionAttribute = descriptionAttributes[0] as DescriptionAttribute; }
/// <inheritdoc/> public void AddItems(IOptionBuilderContext context, Type subjectType, object subject) { PropertyInfo[] propertyInfos = SortProperties(context, FilterProperties(context, subjectType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy))); foreach (PropertyInfo info in propertyInfos) { DisplayAttribute descriptionAttribute = GetAttribute <DisplayAttribute>(info); #if UseDataAnnotations string description = descriptionAttribute == null ? info.Name : descriptionAttribute.GetName(); #else string description = descriptionAttribute == null ? info.Name : descriptionAttribute.DisplayName; #endif object value = subject == null ? null : info.GetValue(subject, null); Type type = value == null ? info.PropertyType : value.GetType(); IOptionBuilder builder = GetBuilder(context, info, subject, type); if (builder != null) { IOptionBuilderContext childContext = context.CreateChildContext(description); builder.AddItems(childContext, type, value); ConfigureItem((IOptionItem)childContext.Lookup(typeof(IOptionGroup)), info); } else { IOptionItem item = CreateItem(context, info, type, description, value); if (item != null) { context.BindItem(item, description); ConfigureItem(item, info); } } } }
private void Test() { System.ComponentModel.DisplayNameAttribute d = new System.ComponentModel.DisplayNameAttribute(); }
private INamedFacet Create(DisplayNameAttribute attribute, IFacetHolder holder) { return CreateAnnotation(attribute.DisplayName, holder); }
/// <summary> /// Creates a dynamic object that is used to hold the properties that the device contains. /// </summary> /// <returns>object</returns> private object CreateObject( ) { object obj = new object ( ); AssemblyName assemblyName = new AssemblyName ( ); assemblyName.Name = Device.Slug(); AssemblyBuilder assembly = Thread.GetDomain ( ).DefineDynamicAssembly ( assemblyName, AssemblyBuilderAccess.Run ); ModuleBuilder module = assembly.DefineDynamicModule ( string.Format ( CultureInfo.InvariantCulture, "{0}Module", Device ) ); TypeBuilder builder = module.DefineType ( "DeviceProperties", TypeAttributes.Public | TypeAttributes.Class ); foreach ( var item in Properties.Keys ) { string propertyName = item.Replace ( ".", "_" ); FieldBuilder field = builder.DefineField ( string.Format ( "_{0}", propertyName ), typeof ( string ), FieldAttributes.Private ); PropertyBuilder property = builder.DefineProperty ( propertyName, PropertyAttributes.None, typeof ( string ), new Type[ ] { typeof ( string ) } ); ConstructorInfo ci = typeof ( DisplayNameAttribute ).GetConstructor ( new Type[ ] { typeof ( string ) } ); CustomAttributeBuilder cab = new CustomAttributeBuilder ( ci, new object[ ] { item } ); property.SetCustomAttribute ( cab ); ci = typeof ( ReadOnlyAttribute ).GetConstructor ( new Type[] { typeof(bool) } ); cab = new CustomAttributeBuilder ( ci, new object[ ] { true } ); property.SetCustomAttribute ( cab ); ci = typeof ( CategoryAttribute ).GetConstructor ( new Type[ ] { typeof ( string ) } ); cab = new CustomAttributeBuilder ( ci, new object[ ] { "Device Properties" } ); property.SetCustomAttribute ( cab ); MethodAttributes getsetAttributes = MethodAttributes.Public | MethodAttributes.HideBySig; MethodBuilder getMethod = builder.DefineMethod ( "get_value", getsetAttributes, typeof ( string ), Type.EmptyTypes ); ILGenerator getIL = getMethod.GetILGenerator ( ); getIL.Emit ( OpCodes.Ldarg_0 ); getIL.Emit ( OpCodes.Ldfld, field ); getIL.Emit ( OpCodes.Ret ); MethodBuilder setMethod = builder.DefineMethod ( "set_value", getsetAttributes, null, new Type[ ] { typeof ( string ) } ); ILGenerator setIL = setMethod.GetILGenerator ( ); setIL.Emit ( OpCodes.Ldarg_0 ); setIL.Emit ( OpCodes.Ldarg_1 ); setIL.Emit ( OpCodes.Stfld, field ); setIL.Emit ( OpCodes.Ret ); property.SetGetMethod ( getMethod ); property.SetSetMethod ( setMethod ); } Type generatedType = builder.CreateType ( ); object generatedObject = Activator.CreateInstance ( generatedType ); PropertyInfo[] properties = generatedType.GetProperties ( ); foreach ( var item in properties ) { object[] attributes = item.GetCustomAttributes ( true ); DisplayNameAttribute dna = new DisplayNameAttribute ( item.Name.Replace ( "_", "." ) ); foreach ( var aitem in attributes ) { if ( aitem is DisplayNameAttribute ) { dna = aitem as DisplayNameAttribute; } } string value = Properties[ dna.DisplayName ]; item.SetValue ( generatedObject, value, null ); } return generatedObject; }
public void GetHashCodeTest () { DisplayNameAttribute dn = new DisplayNameAttribute (); Assert.AreEqual (string.Empty.GetHashCode (), dn.GetHashCode (), "#A1"); dn = new DisplayNameAttribute ("A"); Assert.AreEqual ("A".GetHashCode (), dn.GetHashCode (), "#A2"); // https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=288534 dn = new DisplayNameAttribute (null); try { dn.GetHashCode (); Assert.Fail ("#B1"); } catch (NullReferenceException) { } }
public void Equals () { DisplayNameAttribute dn = new DisplayNameAttribute (); Assert.IsTrue (dn.Equals (DisplayNameAttribute.Default), "#A1"); Assert.IsTrue (dn.Equals (new DisplayNameAttribute (string.Empty)), "#A2"); Assert.IsFalse (dn.Equals (new DisplayNameAttribute ("category")), "#A3"); Assert.IsFalse (dn.Equals (new DisplayNameAttribute (null)), "#A4"); Assert.IsFalse (dn.Equals (null), "#A5"); Assert.IsTrue (dn.Equals (dn), "#A6"); Assert.IsFalse (dn.Equals (55), "#A7"); dn = new DisplayNameAttribute ("category"); Assert.IsFalse (dn.Equals (DisplayNameAttribute.Default), "#B1"); Assert.IsFalse (dn.Equals (new DisplayNameAttribute (string.Empty)), "#B2"); Assert.IsTrue (dn.Equals (new DisplayNameAttribute ("category")), "#B3"); Assert.IsFalse (dn.Equals (new DisplayNameAttribute (null)), "#B4"); Assert.IsFalse (dn.Equals (null), "#B5"); Assert.IsTrue (dn.Equals (dn), "#B6"); Assert.IsFalse (dn.Equals (55), "#B7"); }
public void Constructor1 () { DisplayNameAttribute dn = new DisplayNameAttribute (string.Empty); Assert.IsNotNull (dn.DisplayName, "#A1"); Assert.AreEqual (string.Empty, dn.DisplayName, "#A2"); Assert.IsTrue (dn.IsDefaultAttribute (), "#A3"); dn = new DisplayNameAttribute (null); Assert.IsNull (dn.DisplayName, "#B1"); Assert.IsFalse (dn.IsDefaultAttribute (), "#B2"); dn = new DisplayNameAttribute ("category"); Assert.IsNotNull (dn.DisplayName, "#C1"); Assert.AreEqual ("category", dn.DisplayName, "#C2"); Assert.IsFalse (dn.IsDefaultAttribute (), "#C3"); }
/// <summary> /// 由类型内获取需变更属性 /// </summary> /// <param name="t">传入类型</param> /// <returns>成功返回需设置变更的信息</returns> private List <Neusoft.FrameWork.Models.NeuObject> GetProperty(Type t) { //获取类的DisplayName属性 用于类中文名称 object[] display = t.GetCustomAttributes(false); if (display != null) { foreach (object oDisplay in display) { if (oDisplay is System.ComponentModel.DisplayNameAttribute) { System.ComponentModel.DisplayNameAttribute displayAttribute = oDisplay as System.ComponentModel.DisplayNameAttribute; this.lbDescrip.Text = displayAttribute.DisplayName; break; } } } //获取类型内所有的属性Property PropertyInfo[] propertyCollection = t.GetProperties(); List <Neusoft.FrameWork.Models.NeuObject> recordList = new List <Neusoft.FrameWork.Models.NeuObject>(); //对类型内属性进行循环判断 foreach (PropertyInfo p in propertyCollection) { //对只读或只写属性不进行处理 if (p.CanRead && p.CanWrite) { string propertyID = ""; string propertyName = ""; string propertyDescrip = ""; //获取对每个属性设置的属性 (Property的Attribute) foreach (Attribute a in p.GetCustomAttributes(true)) { //属性中文名称显示 if (a is System.ComponentModel.DisplayNameAttribute) { System.ComponentModel.DisplayNameAttribute displayName = a as System.ComponentModel.DisplayNameAttribute; propertyName = displayName.DisplayName; } //属性描述 if (a is System.ComponentModel.DescriptionAttribute) { System.ComponentModel.DescriptionAttribute descrip = a as System.ComponentModel.DescriptionAttribute; propertyDescrip = descrip.Description; } } //如 存在有效数据 进行保存 ID Name不能为空 if (propertyName != "") { Neusoft.FrameWork.Models.NeuObject shiftProperty = new Neusoft.FrameWork.Models.NeuObject(); shiftProperty.ID = p.Name; shiftProperty.Name = propertyName; shiftProperty.Memo = propertyDescrip; recordList.Add(shiftProperty); } } } return(recordList); }
internal static PropertyDescriptorCollection Fix(Type type, PropertyDescriptorCollection pdc) { if (pdc == null || pdc.Count < 1) return pdc; IEntityOperate factory = EntityFactory.CreateOperate(type); // 准备字段集合 Dictionary<String, FieldItem> dic = new Dictionary<string, FieldItem>(StringComparer.OrdinalIgnoreCase); //factory.Fields.ForEach(item => dic.Add(item.Name, item)); foreach (FieldItem item in factory.Fields) { dic.Add(item.Name, item); } Boolean hasChanged = false; List<PropertyDescriptor> list = new List<PropertyDescriptor>(); foreach (PropertyDescriptor item in pdc) { // 显示名与属性名相同,并且没有DisplayName特性 if (item.Name == item.DisplayName && !ContainAttribute(item.Attributes, typeof(DisplayNameAttribute))) { // 添加一个特性 FieldItem fi = null; if (dic.TryGetValue(item.Name, out fi) && !String.IsNullOrEmpty(fi.Description)) { DisplayNameAttribute dis = new DisplayNameAttribute(fi.Description); list.Add(TypeDescriptor.CreateProperty(type, item, dis)); hasChanged = true; continue; } } list.Add(item); } if (hasChanged) pdc = new PropertyDescriptorCollection(list.ToArray()); return pdc; }
private void ShowAttribute(PropertyInfo attributeTarget, JQGridColumn col) { object[] attributes = attributeTarget.GetCustomAttributes(false); foreach (object attribute in attributes) { if (attribute is System.ComponentModel.DataAnnotations.KeyAttribute) { col.PrimaryKey = true; } if (attribute is System.ComponentModel.DataAnnotations.Schema.ColumnAttribute) { System.ComponentModel.DataAnnotations.Schema.ColumnAttribute ca = attribute as System.ComponentModel.DataAnnotations.Schema.ColumnAttribute; col.Order = ca.Order; } if (attribute is System.ComponentModel.DisplayNameAttribute) { System.ComponentModel.DisplayNameAttribute dn = attribute as System.ComponentModel.DisplayNameAttribute; col.HeaderText = dn.DisplayName; } if (attribute is System.ComponentModel.DataAnnotations.DisplayFormatAttribute) { System.ComponentModel.DataAnnotations.DisplayFormatAttribute dfa = attribute as System.ComponentModel.DataAnnotations.DisplayFormatAttribute; col.DataFormatString = dfa.DataFormatString; } if (attribute is System.ComponentModel.DataAnnotations.EditableAttribute) { System.ComponentModel.DataAnnotations.EditableAttribute ea = attribute as System.ComponentModel.DataAnnotations.EditableAttribute; col.Editable = ea.AllowEdit; } if (attribute is System.ComponentModel.DataAnnotations.RequiredAttribute) { col.EditDialogFieldSuffix = "(*)"; col.EditClientSideValidators.Add(new RequiredValidator()); } if (attribute is SearchableAttribute) { SearchableAttribute sa = attribute as SearchableAttribute; col.Searchable = sa.Searchable; } if (attribute is SearchRequiredAttribute) { SearchRequiredAttribute sa = attribute as SearchRequiredAttribute; if (sa.SearchRequired) { col.EditClientSideValidators.Add(new SearchRequiredValidator()); } } if (attribute is VisiableAttribute) { VisiableAttribute va = attribute as VisiableAttribute; col.Visible = va.Visiable; } if (attribute is EditTypeAttribute) { EditTypeAttribute ea = attribute as EditTypeAttribute; col.EditType = ea.EditType; switch (col.EditType) { case EditType.DatePicker: col.EditorControlID = "DatePicker"; break; case EditType.DateTimePicker: col.EditorControlID = "DateTimePicker"; break; case EditType.TimePicker: col.EditorControlID = "TimePicker"; break; case EditType.AutoComplete: col.EditorControlID = "AutoComplete_" + col.DataField; break; } } if (attribute is SearchTypeAttribute) { SearchTypeAttribute sa = attribute as SearchTypeAttribute; if (sa.SearchType == SearchType.CheckBox) { col.SearchType = SearchType.DropDown; List <SelectListItem> lsli = new List <SelectListItem>(); lsli.Add(new SelectListItem() { Text = "所有", Value = "" }); lsli.Add(new SelectListItem() { Text = "是", Value = "true" }); lsli.Add(new SelectListItem() { Text = "否", Value = "false" }); col.SearchList = lsli; } else { col.SearchType = sa.SearchType; } switch (col.SearchType) { case SearchType.DatePicker: col.SearchControlID = "DatePicker"; break; case SearchType.DateTimePicker: col.SearchControlID = "DateTimePicker"; break; case SearchType.TimePicker: col.SearchControlID = "TimePicker"; break; case SearchType.AutoComplete: col.SearchControlID = "AutoComplete_" + col.DataField; break; } } if (attribute is FormatterAttribute) { FormatterAttribute fa = attribute as FormatterAttribute; switch (fa.FormatterType) { case FormatterType.CheckBox: col.Formatter = new CheckBoxFormatter(); break; } } } }
public void SetDisplayName( string displayName ) { var attr = (DisplayNameAttribute)attributes.FirstOrDefault(a => a is DisplayNameAttribute); if (attr != null) { attributes.RemoveAll(a => a is DisplayNameAttribute); } attr = new DisplayNameAttribute(displayName); attributes.Add(attr); }
public void Constructor0 () { DisplayNameAttribute dn = new DisplayNameAttribute (); Assert.IsNotNull (dn.DisplayName, "#1"); Assert.AreEqual (string.Empty, dn.DisplayName, "#2"); Assert.IsTrue (dn.IsDefaultAttribute (), "#3"); }
public string Export(bool includeHeaderLine) { StringBuilder sb = new StringBuilder(); //Get properties using reflection. IList <PropertyInfo> propertyInfos = typeof(T).GetProperties(); if (includeHeaderLine) { //add header line. foreach (PropertyInfo propertyInfo in propertyInfos) { var atts = propertyInfo.GetCustomAttributes( typeof(DisplayNameAttribute), true); if (atts.Length == 0) { //Don't include anything that doens't have a custom attribute //this will get rid of IDs //sb.Append(propertyInfo.Name).Append(","); } else { bool isRandom = false; if (propertyInfo.Name.Length > 4) { if (propertyInfo.Name.Contains("dom")) { isRandom = true; } } if (false == isRandom) { string displayText = ""; System.ComponentModel.DisplayNameAttribute displayName = (System.ComponentModel.DisplayNameAttribute)atts[0]; displayText = displayName.DisplayName; displayText = displayText.Replace(":", ""); sb.Append(displayText); sb.Append(","); } } } sb.Remove(sb.Length - 1, 1).AppendLine(); } //add value for each property. foreach (T obj in Objects) { foreach (PropertyInfo propertyInfo in propertyInfos) { var atts = propertyInfo.GetCustomAttributes( typeof(DisplayNameAttribute), true); if (atts.Length == 0) { //Don't include anything that doens't have a custom attribute //this will get rid of IDs } else { //exclude random columns bool isRandom = false; if (propertyInfo.Name.Length > 4) { if (propertyInfo.Name.Contains("dom")) { isRandom = true; } } if (false == isRandom) { sb.Append(MakeValueCsvFriendly(propertyInfo.GetValue(obj, null))).Append(","); } } } sb.Remove(sb.Length - 1, 1).AppendLine(); } return(sb.ToString()); }