c# - C# 中的默认值属性

标签 c# attributes

我想使用 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/

相关文章:

c# - C#中的[]是什么?

html - 如何将元素的属性值用作 css 属性? CSS3

c# - 通过 user32.dll 中的 SendInput 发送 key

c# - Entity Framework 的种子方法错误,未在代码优先模型中创建表

javascript - jQuery - 数组元素作为选择器

Python 对象属性 - 访问方法

c# - 为什么要在代码中使用 'custom attributes' (.NET)

c# - JSON.Net 为所有消息解析 Facebook Graph JSON

c# - 扩展模型以添加方法

c# - 如何在 ASP.NET Core 2.x 中存储和检索 session 状态的对象?