.NET : Get property name in attribute

标签 .net attributes

[MyAttribute()]
public string Name { get; set; }

MyAttribute我需要知道相关属性(property)的名称,这可能吗?

编辑:

我需要在文本格式中使用它。

最佳答案

不,这是不可能的。通常你会使用 reflection to read attributes应用于给定的属性,因此您已经知道该属性。例子:

var properties = typeof(SomeType).GetProperties();
foreach (var property in properties)
{
    var attributes = property.GetCustomAttributes(typeof(MyAttribute), true);
    if (attributes.Count > 0)
    {
        // look at property.Name here
    }
}

关于.NET : Get property name in attribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2797429/

相关文章:

c# - 如何将 Serilog 依赖注入(inject)到 .NET 控制台应用程序中我的其余类中

c# - 如何在 C# 中比较标志?

python - 使用 var() 从 dict 键创建属性名称

javascript - 没有等号的 Angular div 标签属性

c# - 如何让 C# 设计者知道 C# 中 Padding 或其他对象/结构的默认属性

c# - 将变音符号插入 MySQL 数据库

.net - 自动宽度和溢出 div

java - 每个唯一不可变一个实例的设计模式是否被认为是邪恶的?

c# - 将 Func<T, string>[] 转换成 Func<T, string[]>?

c# - 从属性的 getter 调用方法