人家说的不是你们说的property,是Attribute.
Attribute是从System.Attribute继承来的一个类,我觉得它主要是产生自描述信息。
[red]不好意思写的不够全面,修改一下。[/red]
Inside C#中有一个例子,(具体可以参见原文第八章,下面的内容在书中全可以找到):
Attributes可以分为Class Attributes,Method Attributes和 Field Attributes 三种。
1.Class Attributes
////////////////////////////////////////////////////////////////////////////
using System;
public enum RemoteServers
{
JEANVALJEAN,
JAVERT,
COSETTE
}
//下面实现了一个Attribute的子类RemoteObjectAttribute
public class RemoteObjectAttribute : Attribute
{
public RemoteObjectAttribute(RemoteServers Server)
{
this.server = Server;
}
protected RemoteServers server;
public string Server
{
get
{
return RemoteServers.GetName(typeof(RemoteServers),
this.server);
}
}
}
//这里应用RemoteObjectAttribute
[RemoteObjectAttribute(RemoteServers.COSETTE)]
class MyRemotableClass
{
}
////////////////////////////////////////////////////////////////////////////
//下面是怎样‘query’ RemoteObjectAttribute
class ClassAttrApp
{
public static void Main()
{
Type type = typeof(MyRemotableClass);
foreach (Attribute attr in type.GetCustomAttributes())
{
RemoteObjectAttribute remoteAttr =
attr as RemoteObjectAttribute;
if (null != remoteAttr)
{
Console.WriteLine("Create this object on {0}.",
remoteAttr.Server);
//这里输出‘Create this object on COSETTE.’
}
}
}
}
========================================
2.Method Attributes
////////////////////////////////////////////////////////////////////////////
using System;
using System.Reflection;
public class TransactionableAttribute : Attribute
{
public TransactionableAttribute()
{
}
}
class TestClass
{
[Transactionable]
public void Foo()
{}
public void Bar()
{}
[Transactionable]
public void Baz()
{}
}
class MethodAttrApp
{
public static void Main()
{
Type type = Type.GetType("TestClass");
foreach(MethodInfo method in type.GetMethods())
{
foreach (Attribute attr in
method.GetCustomAttributes())
{
if (attr is TransactionableAttribute)
{
Console.WriteLine("{0} is transactionable.",
method.Name);
//这里输出‘Foo is transactionable.
// Baz is transactionable.’
}
}
}
}
}
========================================
3.Field Attributes
////////////////////////////////////////////////////////////////////////////
using System;
using System.Reflection;
public enum RegistryHives
{
HKEY_CLASSES_ROOT,
HKEY_CURRENT_USER,
HKEY_LOCAL_MACHINE,
HKEY_USERS,
HKEY_CURRENT_CONFIG
}
public class RegistryKeyAttribute : Attribute
{
public RegistryKeyAttribute(RegistryHives Hive, String ValueName)
{
this.Hive = Hive;
this.ValueName = ValueName;
}
protected RegistryHives hive;
public RegistryHives Hive
{
get { return hive;
}
set { hive = value;
}
}
protected String valueName;
public String ValueName
{
get { return valueName;
}
set { valueName = value;
}
}
}
class TestClass
{
[RegistryKey(RegistryHives.HKEY_CURRENT_USER, "Foo")]
public int Foo;
public int Bar;
}
class FieldAttrApp
{
public static void Main()
{
Type type = Type.GetType("TestClass");
foreach(FieldInfo field in type.GetFields())
{
foreach (Attribute attr in field.GetCustomAttributes())
{
RegistryKeyAttribute registryKeyAttr =
attr as RegistryKeyAttribute;
if (null != registryKeyAttr)
{
Console.WriteLine
("{0} will be saved in {1}////{2}",
field.Name,
registryKeyAttr.Hive,
registryKeyAttr.ValueName);
}
}
}
}
}
++++++++++++++++++++++++++++++++++++++++
另外Attribute的参数还可以缺省,就像delphi的一样,具体请参看原文;
还有一个注意的就是可以利用AttributeUsage 来规定Attribute的用法,比如默认情况下同一个Attribute
不可以出现两次,
public class SingleUseAttribute : Attribute
{
public SingleUseAttribute(String str)
{
}
}
// ERROR: This results in a "duplicate attribute" compiler error.出错了
[SingleUse("abc")]
[SingleUse("def")]
class MyClass
{
}
如果改成下面的样子就没错了
[AttributeUsage(AttributeTargets.All, AllowMultiple=true)]
public class SingleUseAttribute : Attribute
{
public SingleUseAttribute(String str)
{
}
}
[SingleUse("abc")]
[SingleUse("def")]
class MyClass
{
}
+++++++++++++++++++++++++++++++++++++++++
最后一个介绍的是Attribute Identifiers。
class MyClass
{
[HRESULT]
public long Foo();
}
上面这种情况你知道Attribute 是描述Foo方法呢,还是描述返回值呢?为了明确描述的对象,可以这样:
class MyClass
{
[return:HRESULT]
public long Foo();
}
return是属于Attribute Identifiers(包括assembly,module ,type ,method ,property ,event ,field , param, return)中的其中一个。