首页编程propertydescriptor Java里PropertyDescriptor 的用法

propertydescriptor Java里PropertyDescriptor 的用法

编程之家2023-11-05255次浏览

其实propertydescriptor的问题并不复杂,但是又很多的朋友都不太了解Java里PropertyDescriptor 的用法,因此呢,今天小编就来为大家分享propertydescriptor的一些知识,希望可以帮助到大家,下面我们一起来看看这个问题的分析吧!

propertydescriptor Java里PropertyDescriptor 的用法

java.beans.propertydescriptor在哪个jar包里

一、软件包 java.beans

包含与开发 beans有关的类,即基于 JavaBeansTM架构的组件

二、PropertyDescriptor

propertydescriptor Java里PropertyDescriptor 的用法

PropertyDescriptor描述 Java Bean通过一对存储器方法导出的一个属性

Java代码

public class PropertyDescriptor extends FeatureDescriptor

{

//构造方法

propertydescriptor Java里PropertyDescriptor 的用法

//通过调用 getFoo和 setFoo存取方法,为符合标准 Java约定的属性构造一个 PropertyDescriptor

public PropertyDescriptor(String propertyName,

Class<?> beanClass)

throws IntrospectionException{}

//获得属性的 Class对象

public Class<?> getPropertyType(){}

//获得应该用于读取属性值的方法

public Method getReadMethod(){}

//获得应该用于写入属性值的方法

public Method getWriteMethod(){}

Object的几个方法(defineProperty,hasOwnProperty...)

使用Object.defineProperty()定义对象属性时,如已设置 set或 get,就不能设置 writable和 value中的任何一个了,不然会报错。

Object的hasOwnProperty()方法返回一个布尔值,判断对象是否包含特定的自身(非继承)属性。

同defineProperty

getPrototypeOf此方法可以获取指定对象的原型对象

isPrototypeOf方法用于测试一个对象是否存在于另一个对象的原型链上。

列出一个由指定对象的所有自身属性的属性名(包括不可枚举属性但不包括Symbol值作为名称的属性)组成的数组。

区别于Object.keys()。

getOwnPropertyDescriptors方法用来获取一个对象的所有自身属性的描述符。

getOwnPropertyDescriptor方法用来获取一个对象的指定属性的描述符。

propertyGrid自定义排序以及[...]按钮的实现等

我明白你的第一个问题,但是后两个不好意思没明白

第一个问题是这样

//

//摘要:

//使用该集合的默认排序(通常为字母顺序)对集合中的成员进行排序。

public virtual PropertyDescriptorCollection Sort();

//

//摘要:

//使用指定的 System.Collections.IComparer对此集合中的成员排序。

public virtual PropertyDescriptorCollection Sort(IComparer comparer);

//

//摘要:

//对此集合中的成员排序。首先应用指定的顺序,然后应用此集合的默认排序,后者通常为字母顺序。

public virtual PropertyDescriptorCollection Sort(string[] names);

//

//摘要:

//对此集合中的成员排序。首先应用指定的顺序,然后使用指定的 System.Collections.IComparer进行排序。

public virtual PropertyDescriptorCollection Sort(string[] names, IComparer comparer);

///<summary>

///返回此类型说明符所表示的对象的已筛选属性描述符的集合。

///</summary>

///<param name="attributes">用作筛选器的属性数组。它可以是 null。</param>

///<returns>

///一个<see cref="T:System.ComponentModel.PropertyDescriptorCollection"></see>,包含此类型说明符所表示的对象的属性说明。默认为<see cref="F:System.ComponentModel.PropertyDescriptorCollection.Empty"></see>。

///</returns>

public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)

{

int i= 0;

//parameterList是外面传入的待显示数据。

PropertyDescriptor[] newProps= new PropertyDescriptor[parameterList.Count];

foreach(ConfigParameter parameter in parameterList)

{

//由ConfigParameter,你自己定义的类型,转成PropertyDescriptor类型:

newProps[i++]= new SystemOptionsPropertyDescriptor(parameter, true, attributes);

}

//取得了PropertyDescriptor[] newProps;现在可以对它排序,否则就按照newProps的原有顺序显示;

//1.直接返回

PropertyDescriptorCollection tmpPDC= new PropertyDescriptorCollection(newProps);

return tmpPDC;

//2.默认字母顺序

PropertyDescriptorCollection tmpPDC= new PropertyDescriptorCollection(newProps);

return tmpPDC.Sort();

//3.数组的顺序:sortName就是属性的顺序,内容就是各个属性名。

string[] sortName= new string[]{"a","b","c","d"};

return tmpPDC.Sort(sortName);

//4.comparer规则定义的排序方式

ParameterListComparer myComp= new ParameterListComparer();//ParameterListComparer: IComparer

return tmpPDC.Sort(myComp);

//5.先数组,后comparer

return tmpPDC.Sort(sortName,myComp);

//而我采用的是把数据传入之前就排序完毕的方法:即调用之前,把数据parameterList排好顺序,再 1.直接返回。

}

//对于数组排序方法,可以声明称一个事件,由外部传入属性的实现顺序:

public delegate string[] SortEventHandler();

public class CustomTypeDescriptorExtend: CustomTypeDescriptor

{

public SortEventHandler OnSort;

//......其它代码

public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)

{

string[] sortName= OnSort();

PropertyDescriptorCollection tmpPDC= TypeDescriptor.GetProperties(typeof(CustomTypeDescriptorExtend), attributes);

return tmpPDC.Sort(sortName);

}

}

//使用时:

public MyMethod()

{

CustomTypeDescriptorExtend s= new CustomTypeDescriptorExtend();

s.OnSort+= new SortEventHandler(SetSortors);

PropertyGrid1.SelectedObject= s;

PropertyGrid1.PropertySort= PropertySort.Categorized;

}

public string[] SetSortors()

{

return new string[]{"B","A","C"};

}

当然这也不是我写的,给你个原始链接吧,呵呵

http://hi.baidu.com/yangyuhang/blog/item/b2c8e3d3f88eb0dba9ec9add.html

Java里PropertyDescriptor 的用法

public class PropertyDescriptor extends Feature Descriptor PropertyDescriptor描述 Java Bean通过一对存储器方法导出的一个属性。

构造方法摘要

PropertyDescriptor(String propertyName, Class<?> beanClass)

通过调用 getFoo和 setFoo存取方法,为符合标准 Java约定的属性构造一个 PropertyDescriptor。

PropertyDescriptor(String propertyName, Class<?> beanClass, String readMethodName, String writeMethodName)

此构造方法带有一个简单属性的名称和用于读写属性的方法名称。

PropertyDescriptor(String propertyName, Method readMethod, Method writeMethod)

此构造方法带有某一简单属性的名称,以及用来读取和写入属性的 Method对象。

非常感谢您的阅读!我们希望本文对于解决您关于propertydescriptor的问题提供了一些有价值的信息。如果您还有其他疑问,我们将很乐意为您提供进一步的帮助。

福州建网站(福州建材行业网站建设大概需要多少费用)初等函数(常见的初等函数有哪些)