MSDN 指出 http://msdn.microsoft.com/en-us/library/system.attribute.typeid%28v=vs.71%29.aspx财产的TypeId
that:
在实现时,该标识符仅仅是属性的类型。然而,唯一标识符旨在用于标识相同类型的两个属性。
然而,预期用途是区分各个属性实例(例如,与它们所应用的类的不同实例相关联的属性实例)还是区分具有相同类型但由于其属性值在语义上不同的属性?
例如,假设我有以下内容:
public sealed class AmpVolume : System.Attribute
{
public int MaxVolume { get; set; }
public AmpVolume(int maxvolume)
{
MaxVolume = maxvolume;
}
}
[AmpVolume(11)]
public class SpinalTapGuitarAmp
{
}
[AmpVolume(11)]
public class SpinalTapBassAmp
{
}
[AmpVolume(10)]
public class RegularAmp
{
}
我应该将 TypeId 实现为
get
{
return (object)this; //TypeId identifies every individual instance of the attribute
}
Or
get
{
return (object)MaxVolume; //If we compare two AmpVolume attributes, they should be the same if the volume is the same, right?
}
TypeId 属性用于区分同一成员上相同属性的实例。意思是,只有当属性被修饰时才需要实现它AttributeUsageAttribute
其中声明AllowMultiple=true
.
例如,如果您使用多个 AmpVolume 属性装饰一个类或方法,则 TypeId 将区分这些实例。
您可以在此处的注释中找到有关该属性的提示MSDN链接 http://msdn.microsoft.com/en-us/library/6w3a7b50.aspx for GetAttributes
method.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)