属性是自定义的元数据的标记和表现形式。 属性的使用一般是被动的。 两个部分组成: 属性的颁发者和属性的使用者。属性的颁发者声明了某种意义。要求属性的使用者把符合该意义的构造型(类,成员,,)标记上它。 那么属性的颁发者,就能通过根据它们是否有标记属性,和属性的数据进行某种操作了。其中属性的颁发者是主动地,而属性的使用者是被动的。对于平时,我们和我们创建的类只做为属性的使用者。例如对于可序列化的对象,我们只是定义: [Serilizable] public class NameInfo { private string n="Undefined"; public string Name { get { return n; } set { n=value; } } } 又例如在WebService里用到的WebServiceAttribute和WebMethodAttribute什么时候我们才做属性的颁布者,实现自己的属性?对于一般的应用,属性是用不上的。通常的扩展都使用虚拟继承来实现。 只有当你做的东西有自己或其他人进行扩展时,才考虑使用自定义属性。我这里没法说得明白。下面放个容易理解为什么使用属性的例子吧: (这个例子需要些反射的知识)using System; using System.Reflection;using 定义; using 颁发者; using 使用者; using 程序;namespace 定义 { public interface IJob { void Work(); } }namespace 颁发者 { //定义一个Slow的属性。它只能使用在方法上 [AttributeUsage(AttributeTargets.Method)] public class SlowAttribute:Attribute { string desc; public SlowAttribute(string waitdesc) { desc=waitdesc; } public string WaitDescription { get { return desc; } } } }namespace 使用者 { public class 学习DotNet : IJob { public void Work() { Console.WriteLine("-->学习完毕"); } } public class 精通DotNet : IJob { [Slow("很久")] public void Work() { System.Threading.Thread.Sleep(3000); Console.WriteLine("........................精通完毕"); } } }namespace 程序 { public interface IJobExecutor { void Execute(IJob job); } class Util { static public MethodInfo FindJobWork(IJob job) { InterfaceMapping map=job.GetType().GetInterfaceMap(typeof(IJob)); return map.TargetMethods[0]; } } public class 浮躁的人 : IJobExecutor { public void Execute(IJob job) { MethodInfo mi=Util.FindJobWork(job); bool isslow=mi.IsDefined(typeof(SlowAttribute),false); if(!isslow) { job.Work(); Console.WriteLine("这么点小东西还难得到我?"); } else { Console.WriteLine("靠,这么麻烦,老子不学了!!"); } } } public class 耐心的人 : IJobExecutor { public void Execute(IJob job) { MethodInfo mi=Util.FindJobWork(job); bool isslow=mi.IsDefined(typeof(SlowAttribute),false); if(!isslow) { job.Work(); Console.WriteLine("努力,加油。"); } else { SlowAttribute sa=(SlowAttribute)mi.GetCustomAttributes(typeof(SlowAttribute),false)[0]; Console.WriteLine("虽然做这个需要"+sa.WaitDescription+",但是我要把它完成。"); job.Work(); Console.WriteLine("哈哈。OK了"); } } } }namespace ConsoleApplication1 { class Class1 { [STAThread] static void Main(string[] args) { IJob j1=new 学习DotNet(); IJob j2=new 精通DotNet(); IJobExecutor exec1=new 浮躁的人(); IJobExecutor exec2=new 耐心的人(); Console.WriteLine(); Console.WriteLine("广播:浮躁的人的:"); Console.WriteLine(); Console.WriteLine("广播:学习:"); exec1.Execute(j1); Console.WriteLine("广播:精通:"); exec1.Execute(j2); Console.WriteLine(); Console.WriteLine("广播:耐心的人的学习:"); Console.WriteLine(); Console.WriteLine("广播:学习:"); exec2.Execute(j1); Console.WriteLine("广播:精通:"); exec2.Execute(j2); } } }
请问楼上的朋友,你在定义时属性描述为:[Slow("很久")],但是你定义的是[AttributeUsage(AttributeTargets.Method)]public class SlowAttribute:Attribute{},该属性类类名明明是SlowAttribute,是怎么把【[Slow("很久")]class 精通DotNet】与mi.IsDefined(typeof(SlowAttribute),false);联系起来的?
将值编辑器关联到 GUI 框架中的特定类型(通过 ValueEditor 属性)。
除一个完整的示例外,本教程还包括以下主题: 声明属性类 您必须能够做的第一件事是声明属性。
使用属性类 创建属性后,接着应将属性与特定程序元素相关联。
通过反射访问属性 属性已与某程序元素关联后,可使用反射来查询属性存在及其值。
具体看看帮助:
ms-help://MS.NETFrameworkSDK.CHS/csref/html/vcwlkattributestutorial.htm
属性的使用一般是被动的。
两个部分组成:
属性的颁发者和属性的使用者。属性的颁发者声明了某种意义。要求属性的使用者把符合该意义的构造型(类,成员,,)标记上它。
那么属性的颁发者,就能通过根据它们是否有标记属性,和属性的数据进行某种操作了。其中属性的颁发者是主动地,而属性的使用者是被动的。对于平时,我们和我们创建的类只做为属性的使用者。例如对于可序列化的对象,我们只是定义:
[Serilizable] public class NameInfo
{
private string n="Undefined";
public string Name
{
get
{
return n;
}
set
{
n=value;
}
}
}
又例如在WebService里用到的WebServiceAttribute和WebMethodAttribute什么时候我们才做属性的颁布者,实现自己的属性?对于一般的应用,属性是用不上的。通常的扩展都使用虚拟继承来实现。
只有当你做的东西有自己或其他人进行扩展时,才考虑使用自定义属性。我这里没法说得明白。下面放个容易理解为什么使用属性的例子吧:
(这个例子需要些反射的知识)using System;
using System.Reflection;using 定义;
using 颁发者;
using 使用者;
using 程序;namespace 定义
{
public interface IJob
{
void Work();
}
}namespace 颁发者
{
//定义一个Slow的属性。它只能使用在方法上 [AttributeUsage(AttributeTargets.Method)]
public class SlowAttribute:Attribute
{
string desc;
public SlowAttribute(string waitdesc)
{
desc=waitdesc;
}
public string WaitDescription
{
get
{
return desc;
}
}
}
}namespace 使用者
{
public class 学习DotNet : IJob
{
public void Work()
{
Console.WriteLine("-->学习完毕");
}
}
public class 精通DotNet : IJob
{
[Slow("很久")]
public void Work()
{
System.Threading.Thread.Sleep(3000);
Console.WriteLine("........................精通完毕");
}
}
}namespace 程序
{
public interface IJobExecutor
{
void Execute(IJob job);
} class Util
{
static public MethodInfo FindJobWork(IJob job)
{
InterfaceMapping map=job.GetType().GetInterfaceMap(typeof(IJob));
return map.TargetMethods[0];
}
} public class 浮躁的人 : IJobExecutor
{
public void Execute(IJob job)
{
MethodInfo mi=Util.FindJobWork(job);
bool isslow=mi.IsDefined(typeof(SlowAttribute),false);
if(!isslow)
{
job.Work();
Console.WriteLine("这么点小东西还难得到我?");
}
else
{
Console.WriteLine("靠,这么麻烦,老子不学了!!");
}
}
}
public class 耐心的人 : IJobExecutor
{
public void Execute(IJob job)
{
MethodInfo mi=Util.FindJobWork(job);
bool isslow=mi.IsDefined(typeof(SlowAttribute),false);
if(!isslow)
{
job.Work();
Console.WriteLine("努力,加油。");
}
else
{
SlowAttribute sa=(SlowAttribute)mi.GetCustomAttributes(typeof(SlowAttribute),false)[0];
Console.WriteLine("虽然做这个需要"+sa.WaitDescription+",但是我要把它完成。");
job.Work();
Console.WriteLine("哈哈。OK了");
}
}
}
}namespace ConsoleApplication1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
IJob j1=new 学习DotNet();
IJob j2=new 精通DotNet(); IJobExecutor exec1=new 浮躁的人();
IJobExecutor exec2=new 耐心的人(); Console.WriteLine();
Console.WriteLine("广播:浮躁的人的:");
Console.WriteLine(); Console.WriteLine("广播:学习:");
exec1.Execute(j1);
Console.WriteLine("广播:精通:");
exec1.Execute(j2); Console.WriteLine();
Console.WriteLine("广播:耐心的人的学习:");
Console.WriteLine(); Console.WriteLine("广播:学习:");
exec2.Execute(j1);
Console.WriteLine("广播:精通:");
exec2.Execute(j2);
}
}
}