我想使用 DefaultValue 属性来为我在应用程序中编写的自定义类定义默认值。该类在他的构造函数中给出了一个字符串。 我写了以下内容:
[DefaultValue(Type.GetType("MyClass"),"hello world")]
但是当我尝试运行这个应用程序时。我给出错误:
"An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type".
谁能给我解释一下这是什么问题?
最佳答案
您正在使用 Type.GetType("MyClass")
,而您应该使用 typeof(MyClass)
。
关于c# - C# 中的默认值属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4713641/