
1 定义一个以Attribute结尾的特性类, 特性类继承自SystemAttribute, 如下所示
[AttributeUsage(AttributeTargetsProperty, AllowMultiple=false)]
public class PrimaryKeyAttribute : SystemAttribute
{
其中AttributeTargets是一个枚举的值, 可以是: Assembly | Module | Class | Struct | Enum | Constructor | Method | Property | Field | Event | Interface | Parameter | Delegate
2 在需要使用的地方使用PrimaryKey自定义特性标签, 如下所示
[PrimaryKey(Column = "CustomerID", IsIdentity=false)]
public int ID
{
3 为了获取自定义特性的信息, 需要反射的方式获取其数据, 首先我们定义一个类来存储这些信息, 如下所示
public class PrimaryKeyModel
{
private readonly PropertyInfo propertyInfo;//外键的属性字段信息
private readonly PrimaryKeyAttribute primaryKeyAtt;//外键的特性信息
public static PrimaryKeyModel GetPrimaryKey(Type type)
{
PropertyInfo[] properties = typeGetProperties();
foreach (PropertyInfo p in properties)
{
object[] keys = pGetCustomAttributes(typeof(PrimaryKeyAttribute), true);
if (keysLength == 1)
{
return new PrimaryKeyModel(p, keys[0] as PrimaryKeyAttribute);
}
}
return null;
}
4 在Customer类中获取其特性的信息代码如下
string strReturn = stringEmpty;
//Get PrimaryKey Name
PrimaryKeyModel attribute= PrimaryKeyModelGetPrimaryKey(thisGetType());
if(attribute != null)
{
strReturn += stringFormat("PrimaryKey Name:{0} IsIdentity:{1} Column:{2}\r\n",
attributePropertyName, attributePrimaryKeyAttIsIdentity, attributePrimaryKeyAttColumn);
}
何获取自定义类所属性 及属性类型
本帖属于CocoaChina员发表转帖请写明源帖址
题目点
直接看代码
复制代码
@interface AppState : NSObject{
BOOL _passed;
int _int;
/
状态否
/
BOOL isPassed;
/
用户数据
/
NSMutableDictionary userValues;
}
@property(nonatomic ,assign) float _float;
@property(nonatomic ,assign) double _double;
@property(nonatomic ,retain) NSDate _date;
@property(nonatomic ,assign) char _char;
@property(nonatomic ,assign) int _int;;
@property(nonatomic ,assign) BOOL _passed;
@property(nonatomic ,assign) BOOL
isPassed;
我通
#import
runtimeh>
面式获取 所属性名 及属性类型
复制代码
unsigned int propertyCount = 0;
objc_property_t properties = class_copyPropertyList(klass,
&propertyCount);
for (unsigned int i = 0; i < propertyCount; ++i) {
objc_property_t property = properties[i];
const char name = property_getName(property);//获取属性名字
const char attributes = property_getAttributes(property);//获取属性类型
}
具体输所示
复制代码
输类 state T@"AppState",&,N,Vstate
输
_float Tf,N,V__float
_double Td,N,V__double
_date T@"NSDate",&,N,V__date
_char
Tc,N,V__char
前面获所需属性名称
面串让我获取类别呢
PS:V__date 面
V_属性名
1、过去某个类的某个属性:
可以通过对象调用其属性:
1)、创建对象 : 类名 对象名 = new 类名();
2)、调用属性: 对象名属性
2、获取某个对象的所有属性
数组类型[] 数组名 = 对象名getClass()getDeclaredFields();
建议你这样试试看:
如果类为静态类的话,直接类名属性,类名字段来访问如果类不是静态类的话,需要创建类的对象,然后对象属性,对象字段来访问
注意:
一般字段都是设置为private,用于内部存储数据,可通过属性来访问字段
反射可以获取到属性类型,Field类里面有个方法,getType()就是获取属性类型的。。。
下面是个示例代码。。。
public static void main(String args[]) {
People peo = new People();
Class cla = Peopleclass;
try {
Field[] fields = clagetFields();
for(Field field:fields){
Class c = fieldgetType();
if(c==Stringclass){
fieldset(peo, "EMPTY");
}else if(c==Integerclass){
fieldset(peo, 0);
}
}
} catch (SecurityException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
eprintStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
eprintStackTrace();
}
}
主要就是用Class c = fieldgetType();
这个来获取类型。。。。
以上就是关于如何获取类或属性的自定义特性全部的内容,包括:如何获取类或属性的自定义特性、安卓Xposed怎么获取自定义类的属性、java 怎么获取一个对象的属性等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)