public static string ToDisplay <T, TProp>(Expression <Func <T, TProp> > propertyExpression, DisplayProperty property = DisplayProperty.Name) { PropertyInfo propInfo = propertyExpression.GetPropInfo(); DisplayNameAttribute attr = propInfo.GetAttribute <DisplayNameAttribute>(false); return(attr.GetType().GetProperty(property.ToString()).GetValue(attr, null) as string ?? propInfo.Name); }
protected void SetTitle(string property, string value) { PropertyDescriptor descriptor = TypeDescriptor.GetProperties(this.GetType())[property]; DisplayNameAttribute attribute = (DisplayNameAttribute)descriptor.Attributes[typeof(DisplayNameAttribute)]; FieldInfo fieldToChange = attribute.GetType().GetField("_displayName", BindingFlags.NonPublic | BindingFlags.Instance); fieldToChange.SetValue(attribute, value); }
private string GetPropertyDisplayName(string property) { PropertyDescriptor descriptor = TypeDescriptor.GetProperties(this.GetType())[property]; DisplayNameAttribute attrib = (DisplayNameAttribute)descriptor.Attributes[typeof(DisplayNameAttribute)]; return((string)attrib.GetType().GetField("_displayName", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(attrib)); }
protected void SetTitle(string property, string value) { PropertyDescriptor descriptor = TypeDescriptor.GetProperties(this.GetType())[property]; DisplayNameAttribute attribute = (DisplayNameAttribute)descriptor.Attributes[typeof(DisplayNameAttribute)]; FieldInfo[] fields = attribute.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Instance); Debug.Assert(fields.Length == 1); FieldInfo fieldToChange = fields[0]; fieldToChange.SetValue(attribute, value); }
private void ChangePropertyName(string property, string name) { PropertyDescriptor descriptor = TypeDescriptor.GetProperties(this.GetType())[property]; DisplayNameAttribute attrib = (DisplayNameAttribute)descriptor.Attributes[typeof(DisplayNameAttribute)]; FieldInfo isBrow = attrib.GetType().GetField("_displayName", BindingFlags.NonPublic | BindingFlags.Instance); if (isBrow != null) { isBrow.SetValue(attrib, name); } }
private void RenameProperty(PropertyDescriptor property, string newName) { DisplayNameAttribute attribute = (DisplayNameAttribute) property.Attributes[typeof(DisplayNameAttribute)]; System.Reflection.FieldInfo fieldToChange = attribute.GetType().GetField("_displayName", System.Reflection.BindingFlags.IgnoreCase | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); if (fieldToChange != null) { fieldToChange.SetValue(attribute, newName); } }