动态填充/修改类属性的 DisplayNameAttribute 属性

话说 PropertyDescriptor 的许多属性都是只读的。包括 DisplayName 属性,最初的时候,用Reflector 反编译然后查看里面的私有字段和属性,然后用反射机制强行设置,虽然 PropertyDescriptor 的 DisplayName 属性已经显示正确了,但 Html.LabelFor 生成的 HTML 代码还是不显示 DisplayName 的内容。。

利用 Reflector 继续搜索各个类之间的关系,发现PropertyDescriptor 的父类 MemberDescriptor 的 AttributeArray 属性中包含有全部属性的原始实例。后来将其 反射出来,再设置回去,果然成功。Html.LabelFor 中显示的内容也正确了。

 1、

    // 摘要:
    //     表示一个类成员,例如某个属性或事件。这是一个抽象基类。
    [ComVisible(true)]
    public abstract class MemberDescriptor
    {

        // 摘要:
        //     用指定的成员名称和特性数组初始化 System.ComponentModel.MemberDescriptor 类的新实例。
        //
        // 参数:
        //   name:
        //     成员名。
        //
        //   attributes:
        //     包含成员特性的类型 System.Attribute 的数组。
        //
        // 异常:
        //   System.ArgumentException:
        //     该名称为空字符串 ("") 或 null。
        protected MemberDescriptor(string name, Attribute[] attributes);  //初始化

        // 摘要:
        //     获取或设置特性数组。
        //
        // 返回结果:
        //     包含成员特性的类型 System.Attribute 的数组。
        protected virtual Attribute[] AttributeArray { get; set; }  //可写,重写Attribute使DisplayName 属性值可写

        //
        // 摘要:
        //     获取可以显示在窗口(如“属性”窗口)中的名称。
        //
        // 返回结果:
        //     为该成员显示的名称。
        public virtual string DisplayName { get; }  //只读
        //
        // 摘要:
        //     获取成员的说明,如 System.ComponentModel.DescriptionAttribute 中所指定的。
        //
        // 返回结果:
        //     成员的说明。如果没有 System.ComponentModel.DescriptionAttribute,属性值被设置为默认值,它是一个空字符串
        //     ("")。
        public virtual string Description { get; }

}

 2、

子类中覆盖一下属性DisplayName:

    public class PropertyStub : PropertyDescriptor
    {
        PropertyInfo info;

        public PropertyStub(PropertyInfo propertyInfo, Attribute[] attrs)
            : base(propertyInfo.Name, attrs)
        {
            this.info = propertyInfo;
        }

      //通过重写DisplayName,可以将属性在PropertyGrid中的显示设置成中文 
        public override string DisplayName
        {
            get
            {
                if (info != null)
                {
                    MyControlAttibute uicontrolattibute = (MyControlAttibute)Attribute.GetCustomAttribute(info, typeof(MyControlAttibute));
                    if (uicontrolattibute != null)
                        return uicontrolattibute.PropertyName;
                    else
                    {
                        return info.Name;
                    }
                }
                else
                    return "";
            }
        } 

}

3、

//重写Attribute的属性

public class MyControlAttibute : Attribute
    {
        private string _PropertyName;
        private string _PropertyDescription;
        private object _DefaultValue;

        public MyControlAttibute(string Name, string Description, object DefalutValue)
        {
            this._PropertyName = Name;
            this._PropertyDescription = Description;
            this._DefaultValue = DefalutValue;
        }

        public MyControlAttibute(string Name, string Description)
        {
            this._PropertyName = Name;
            this._PropertyDescription = Description;
            this._DefaultValue = "";
        }

        public MyControlAttibute(string Name)
        {
            this._PropertyName = Name;
            this._PropertyDescription = "";
            this._DefaultValue = "";
        }

        public string PropertyName
        {
            get { return this._PropertyName; }
        }

        public string PropertyDescription
        {
            get { return this._PropertyDescription; }
        }

        public object DefaultValue
        {
            get { return this._DefaultValue; }
        }
    }

如果你想使用Java将Excel数据导入到嵌套对象中,可以使用Apache POI和Java反射。 以下是一个示例代码,演示了如何动态填充嵌套对象的属性值: 假设我们有以下两个: ```java public class Address { private String street; private String city; private String state; private String zipCode; // getters and setters } public class Person { private String name; private int age; private Address address; // getters and setters } ``` 假设我们已经将Excel表格中的数据读入到了一个List<List<String>>对象中,其中每个List<String>对象表示一行Excel数据。下面的代码展示了如何使用反射和Apache POI将数据填充到Person对象中: ```java import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.Row; import java.lang.reflect.Field; import java.util.List; public class ExcelReader { public static void main(String[] args) { // 假设我们已经将Excel表格中的数据读入到了一个List<List<String>>对象中 List<List<String>> excelData = getExcelData(); // 创建一个名为"Sheet1"的工作表 Sheet sheet = workbook.getSheet("Sheet1"); // 获取工作表中的数据行 for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) { Row row = sheet.getRow(i); // 创建Person对象 Person person = new Person(); // 使用反射获取Person对象中的所有属性 Field[] fields = Person.class.getDeclaredFields(); // 遍历所有属性 for (int j = 0; j < fields.length; j++) { Field field = fields[j]; // 获取属性String fieldName = field.getName(); // 获取属性值 Cell cell = row.getCell(j); Object cellValue = null; if (cell != null) { CellType cellType = cell.getCellType(); if (cellType == CellType.STRING) { cellValue = cell.getStringCellValue(); } else if (cellType == CellType.NUMERIC) { cellValue = cell.getNumericCellValue(); } } // 如果属性值为null,则跳过 if (cellValue == null) { continue; } // 如果属性名为address,则填充Address对象的属性值 if (fieldName.equals("address")) { // 创建Address对象 Address address = new Address(); // 获取Address对象中的所有属性 Field[] addressFields = Address.class.getDeclaredFields(); // 遍历所有属性 for (int k = 0; k < addressFields.length; k++) { Field addressField = addressFields[k]; // 获取属性String addressFieldName = addressField.getName(); // 获取属性值 Cell addressCell = row.getCell(j + k + 1); Object addressCellValue = null; if (addressCell != null) { CellType addressCellType = addressCell.getCellType(); if (addressCellType == CellType.STRING) { addressCellValue = addressCell.getStringCellValue(); } else if (addressCellType == CellType.NUMERIC) { addressCellValue = addressCell.getNumericCellValue(); } } // 如果属性值为null,则跳过 if (addressCellValue == null) { continue; } // 使用反射设置Address对象的属性值 try { Field addressField = Address.class.getDeclaredField(addressFieldName); addressField.setAccessible(true); addressField.set(address, addressCellValue); } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); } } // 设置Person对象的address属性值 person.setAddress(address); } else { // 使用反射设置Person对象的属性值 try { field.setAccessible(true); field.set(person, cellValue); } catch (IllegalAccessException e) { e.printStackTrace(); } } } // 打印Person对象的属性值 System.out.println(person); } } } ``` 这个示例代码中,我们首先使用反射获取了Person中的所有属性,然后遍历了Excel表格中的数据行,并使用反射和Apache POI将数据填充到Person对象中。当属性名为address时,我们创建了一个新
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值