将App.Config中的值分配给属性的属性 [英] Assign a value from App.Config to a Attribute of a Property
问题描述
我们想要从app.config设置属性值并使用以下代码,它会给出错误
We want to set a attribute value from app.config and using following code, it gives error
"属性参数必须是常量表达式,typeof表达式或数组创建表达式属性参数类型"
"An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type"
public class A
{
[JsonProperty(ConfigurationManager.AppSettings["Application:ExplicitUserTypeAttribute"]]
public string ExplicitUserTypeAttribute { get; set; }
}
我明白这有是一个可以在编译时计算的常量,但这是我们的要求,我想知道是否有任何工作可以让它工作。
I understand this has to be a constant that can be computed at compile time but this is our requirement and I'm wondering if there is any work around to get it working.
谢谢!
推荐答案
在这种情况下
您的类型还需要动态已创建类型(一旦创建了C#中的类型,其上的属性将不会更改。)
In that case your type will also need to be dynamically created type (once a Type in C# is created, the attributes on it won't change).
链接中的示例创建类型 myType 动态然后将属性应用于类型本身和.HelloWorld()方法。
The example in the link creates a Type myType dynamically and then applies the attribute to the type itself and .HelloWorld() method.
这篇关于将App.Config中的值分配给属性的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!