- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
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=true
的 AttributeUsageAttribute
修饰时才需要实现它。
例如,如果您使用多个 AmpVolume 属性装饰类或方法,则 TypeId 将区分这些实例。
您可以在此 MSDN link 的注释中找到有关该属性的提示对于 GetAttributes
方法。
关于c# - 语义相同的两个属性的 TypeId 应该不同还是相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8728793/
我正在制作一个 C++ 库,它严重依赖 RTTI(可自定义桥接至另一种语言)并且对字符串文字类型非常困惑。 这是我为说明问题所做的一个简单测试: std::cout direct-initializ
我正在使用关联列表来关联 TypeId事件类型的 s 和 TypeId谁想要接收它们。 当我尝试获取 TypeId 时的 Rc , 它给你相同的 TypeId (Rc 中的一个存储了一个 Any )不
答:总之使用虚函数!因此,实际上不要将其用作好的设计,但出于学习目的请阅读! 我想先说我正在使用 C++ 和 Qt我有一个形状指针 vector (基类) 编辑:doSomething() 不是基类的
我正在阅读C++ 11草案标准,关于[expr.typeid]的部分提到了以下内容(强调我的意思): [...] When typeid is applied to an expression oth
使用 gcc 4.9 我发现用字面量为复数生成的类型与通过常规方法创建的类型不同,即: typeid(complex(0.0,1.0)) != typeid(1.0i) 我在这里犯错了吗? 这是编译器
使用 gcc 4.9 我发现使用类型文字生成的复数类型与通过常规方式创建的类型不同,即: typeid(complex(0.0,1.0)) != typeid(1.0i) 我在这里犯错了吗? 这是编译
我正在观看以下 video 这里提到g++对于以下代码会报错: #include #include #include struct S { std::vector b ; }; int mai
我不想使用函数重载来对函数进行小的更改。相反,我想使用typeid()检查下面的模板化函数的传递参数。但是,如果我没有在下面的代码中注释掉该行,则会产生以下编译错误: Severity Code
示例代码: MainWindow::MainWindow(QWidget *parent) { QString strTemp = typeid(this).name();
我有以下代码: #include class Bobo {public: int member; void function() { auto lambda
根据标准,typeid 运算符是依赖于实现的,因此在由不同编译器编译的其他进程创建的对象上使用它是无稽之谈。但是提供进程被同一个编译器编译是什么情况呢? 最佳答案 它可能不会工作,因为外部对象将在它自
class A { protected: int a; public: A(); A(int); virtual void print()=0; virtual
我正在开发实体组件系统,我希望能够使用此方法从实体中检索给定类型的组件: template T* Entity::getComponent() { for( auto i = mCompon
我得到一个有两个基类指针的函数,根据子类,应该调用其他函数...... 像这样: void function(A* a, A* a2){ if (typeid(*a).name() =
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Printing derived class name in base class 我正在使用 GCC,下面
constructor 定义中的print 语句没有打印出来,是不是main 中的constructor 调用正确?我知道我在这里遗漏了一些要点,请指出。 #include #include te
我想知道 typeid 是否是一个“足够硬”的类型安全标准,可以放弃所有通常的预防措施。具体来说,请考虑以下代码片段: class storage { private: std::map ob
我在我的代码中使用了 typeid,但在我看来,如果我避免使用 typeid,代码会更清晰。 如果我们要存储类的类型,为什么我们首先要选择面向对象的语言呢? 但我一遍又一遍地看到这种模式,我不知道如何
我有以下使用 3 个不同映射的类:键始终是字符串,而值可以是字符串、整数或 float 。 class MyMaps { public: template void addKey(const
我想要一个 constexpr 函数,它将为每个 C++ 类型返回一个唯一的 id,如下所示: using typeid_t = uintptr_t; template constexpr type
我是一名优秀的程序员,十分优秀!