c# - 你能在 C# 中将一个属性应用于多个字段吗?

标签 c# custom-attributes

这似乎不可能,但我还是要问...在 C# 中是否可以同时将单个属性应用于多个字段?

public class MyClass {
     [SomeAttribute]
     public int m_nVar1;
     [SomeAttribute]
     public int m_nVar2;
     public int m_nVar3;
}

是否有一种简写方法可以将“SomeAttribute”放在 m_Var1 和 m_Var2 上,而不放在 m_nVar3 上?目前,我们将属性放在每个字段之前,但最好将所有使用属性的字段放在一个 block 中。

最佳答案

是的,这是可能的:

[SomeAttribute]
public int m_nVar1, m_nVar2;

(但显然只有类型相同时)

关于c# - 你能在 C# 中将一个属性应用于多个字段吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10725998/

相关文章:

c# - VS2005/VS2008 数据集设计器,将一行插入到具有自动生成的 guid 列的表中

c# - 通过自定义属性 (json.net) 从序列化中排除属性

c# 4.0 使用(指定的属性和 attribute.data)获取类属性

c# - 以编程方式更改 winforms 按钮文本颜色不起作用

c# - Web API 路由 - 找到多个与请求匹配的操作

reactjs - React Typescript - 添加自定义属性

symfony1 - 为 sfDoctrineGuard 插件的 sfGuardUser 模型添加 avatar 字段

c# - 在升级的MVC项目中使用ExceptionFilterAttribute

c# - 无法从 Windows 服务访问驱动器映射网络共享上的文件

c# - 如何以正确的方式加入 firebase 查询的结果