c# ambiguous 属性

标签 c# attributes ambiguous

我阅读了以下重要问题:Attributes in C#并学到了很多关于属性的知识。

我正在使用一个使用多个属性的库。示例:

[State]
public class USStates
{
    [City]
    public void isNewYork() { }
    [Zip]
    public ZipCode GetZipCode() { }
}

我想创建自己的属性,并使用来自第 3 方命名空间的现有属性(State、City、Zip)对我的类执行额外的工作。例如,我正在努力实现这一目标:

using 3rdPartyNamespace;
using MyOwnSuperNamespace;

public class State : Attribute 
{ 
    // my own implementation/extension of State 
}

// create City, Zip attributes respectively

[State]
public class USStates
{
    [City]
    public void isNewYork() { }
    [Zip]
    public ZipCode GetZipCode() { }
}

我尝试了上面的方法,但在所有 3 个属性上都出现了不明确的属性错误。我可以告诉编译器直接调用两个属性而不是使用任何一个吗?我想保留现有的属性并执行额外的工作,而不必使用我创建的额外属性来标记我的类。

这可能吗?

最佳答案

ho can i tell the compiler to direct call to both attributes instead of using either one?

如果你想应用这两个属性,你需要指定这两个属性。您需要完全限定属性名称,或使用 using 别名指令,例如

using Mine = MyOwnSuperNamespace;
using Theirs = 3rdPartyNamespace;

[Mine.State]
[Theirs.State]
...

关于c# ambiguous 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10571515/

相关文章:

c# - 从 2 个不同的 API 组合/转换 2 个相似的对象类型

r - R:获取属性值作为向量

c++ - 使用 CRTP 模式时继承的模糊方法

MySQL 查询,在 where 子句中使用多个具有相同名称的表字段

c# - 无法加载文件或程序集 'System.Web.Mvc, Version=3.0.0.0' 或其依赖项之一

c# - 将 ASP.NET MVC 3 应用程序部署到 IIS

c# - 使用 LINQ 或 lambda 表达式获取该值的值和计数

c# - 强制枚举将数字识别为 C# 中的字符串

attributes - 删除Nifi中的多个属性

c++ - 如何解决 ISO C++ 中不明确的运算符