特性
举个简单的例子:
先定义个特性
从Attribute继承,并标明用法
[AttributeUsage(AttributeTargets.Property|AttributeTargets.Class)]
public class MyAttribute:Attribute
{
}
//应用此特性
[My]
public class Entity
{
private int m_MyProperty ;
[My]
public virtual int MyProperty
{
get { return m_MyProperty; }
set { m_MyProperty = value; }
}
}
//检索此特性(在类上标的特性)
class program
{
static void Main()
{
Attribute attr = Attribute.GetCustomAttribute(typeof(Entity), typeof(MyAttribute),false);
}
}
先定义个特性
从Attribute继承,并标明用法
[AttributeUsage(AttributeTargets.Property|AttributeTargets.Class)]
public class MyAttribute:Attribute
{
}
//应用此特性
[My]
public class Entity
{
private int m_MyProperty ;
[My]
public virtual int MyProperty
{
get { return m_MyProperty; }
set { m_MyProperty = value; }
}
}
//检索此特性(在类上标的特性)
class program
{
static void Main()
{
Attribute attr = Attribute.GetCustomAttribute(typeof(Entity), typeof(MyAttribute),false);
}
}
浙公网安备 33010602011771号