属性名称作为属性构造函数的输入 [英] Name of a property as input to attribute constructor

查看:101
本文介绍了属性名称作为属性构造函数的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义属性,我希望它具有一个属性名称作为输入.因为名称是一个字符串,所以它是有效的输入类型(因为属性对于它们作为构造函数的输入所具有的限制非常有限). 但是我该怎么做呢?

I have a custom attribute and I would like it to have the name of a property as input. Because the name is a string it is a valid input type (as attributes are quite limited as to what they can have as input to their constructors). But how can I accomplish this?

使用以下示例代码:

public class MyCustomAttribute : Attribute {
    public MyCustomAttribute(string propertyName) {}
}

public class Foo {
    public bool MyCustomProperty { get; set; }

    [MyCustom(SomeMagicAppliedToMyCustomProperty)] // I want the attribute to receive something along the lines of "Foo.MyCustomProperty"
    public void Bar();
}

我该如何限制属性在其构造函数中可以接收的内容?

How can I accomplish this with the limitations to what an attribute can receive in its constructor?

推荐答案

这是不可能的. 这些属性只能接受常量,只需将MyCustomProperty名称用引号引起来即可.

This is not possible. The attributes can accept only constants, just put your MyCustomProperty name in quotes into the Attribute.

这篇关于属性名称作为属性构造函数的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆