gpt4 book ai didi

c# - 语义相同的两个属性的 TypeId 应该不同还是相同?

转载 作者:太空狗 更新时间:2023-10-29 23:17:13 25 4
gpt4 key购买 nike

MSDN states属性 TypeId 的:

As implemented, this identifier is merely the Type of the attribute. However, it is intended that the unique identifier be used to identify two attributes of the same type.

然而,预期用途是区分各个属性实例(例如,与应用它们的类的不同实例相关联的属性实例)还是具有相同类型但由于其属性值在语义上不同的属性?

例如,假设我有以下内容:

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
}

或者

        get
{
return (object)MaxVolume; //If we compare two AmpVolume attributes, they should be the same if the volume is the same, right?
}

最佳答案

TypeId 属性用于区分同一成员上同一属性的实例。意思是,只有当属性被声明为 AllowMultiple=trueAttributeUsageAttribute 修饰时才需要实现它。

例如,如果您使用多个 AmpVolume 属性装饰类或方法,则 TypeId 将区分这些实例。

您可以在此 MSDN link 的注释中找到有关该属性的提示对于 GetAttributes 方法。

关于c# - 语义相同的两个属性的 TypeId 应该不同还是相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8728793/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com