c# - 如何获取我们的属性设置的属性名称?

标签 c# reflection attributes

我将在不向属性传递任何参数的情况下执行此操作!可能吗?

class MyAtt : Attribute {
    string NameOfSettedProperty() {
        //How do this? (Would be MyProp for example)
    }
}

class MyCls {
    [MyAtt]
    int MyProp { get { return 10; } }
}

最佳答案

使用 CallerMemberNameAttribute来自 .NET 4.5:

public CustomAttribute([CallerMemberName] string propertyName = null)
{
    // ...
}

关于c# - 如何获取我们的属性设置的属性名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4606973/

相关文章:

reflection - TypeScript:检查派生类是否实现了方法

ruby - ruby 类中的可继承属性

c# - Unity Animator Inspector 中的错误。看不到任何值

c# - 在 C# 中使用事件处理程序进行顶级异常处理

c - 在 C 中在运行时获取变量的类型

java - newInstance() 找不到存在的构造函数

android - 如何在 subview 组上设置属性android

javascript - 从 getElementsByTagName() 获取属性的最佳方法?

c# - 使用静态类的 2 个单元测试 c# 之间的冲突

c# - 使用 Windows 应用商店应用获取 CPU 使用率(等)