如果两个属性,它们具有相同的语义的TypeIds不同或相同? [英] Should the TypeIds of two attributes which are semantically identical be different or the same?

查看:127
本文介绍了如果两个属性,它们具有相同的语义的TypeIds不同或相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MSDN州属性的 TYPEID 是:

  

如实施,该标识符是仅仅属性的类型。然而,意图是唯一标识符被用来标识相同类型的两个属性

是对预期的用途然而,单独属性实例的区别(例如,那些与类的不同实例相关联它们所施加到的)或具有相同的类型,但属性之间由于它们的属性值在语义上不同<? / P>

举例来说,假设我有以下几点:

 公共密封类AmpVolume:System.Attribute
{
    公众诠释MaxVolume {获得;组; }
    公共AmpVolume(INT maxvolume)
    {
        MaxVolume = maxvolume;
    }
}

[AmpVolume(11)]
公共类SpinalTapGuitarAmp
{
}

[AmpVolume(11)]
公共类SpinalTapBassAmp
{
}

[AmpVolume(10)]
公共类RegularAmp
{
}
 

我应该实现TYPEID为

  GET
        {
            返回(对象)这一点; // typeid的标识属性的每个单个实例
        }
 

  GET
        {
            返回(对象)MaxVolume; //如果我们比较两个AmpVolume属性,他们应该如果卷是相同的,正确的是一样的吗?
        }
 

解决方案

的TYPEID属性用于在同一构件上的相同属性的实例之间进行区分。意义,它实现它,只有当该属性饰以 AttributeUsageAttribute特性该声明要求的AllowMultiple = TRUE

例如,如果你愿意修饰类或方法有多个AmpVolume属性,那么TYPEID将这些实例加以区分。

您可以在此 MSDN链接找到的说明物业的提示在GetAttributes 方法。

MSDN states of the property TypeId that:

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.

Is the intended use however, to distinguish between individual attribute instances (e.g. those associated with different instances of the class to which they are applied) or between attributes which have the same type but due to their property values are semantically different?

For example, say I had the following:

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
{
}

Should I implement TypeId as

        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?
        }

解决方案

The TypeId property is used to distinguish between instances of the same attribute on the same member. Meaning, it's required to implement it only when the attribute is decorated with AttributeUsageAttribute which declares AllowMultiple=true.

For example, if you'd decorate a class or method with multiple AmpVolume attributes, then the TypeId will distinguish between those instances.

You can find a hint on the property in the note at this MSDN link for GetAttributes method.

这篇关于如果两个属性,它们具有相同的语义的TypeIds不同或相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆