开发者

DefaultValue Attribute in C#

I want to use on DefaultValue Attribute to define default value for custom class that I write in my App. the class gives in his constractor a string. I write the follow:

[DefaultValue(Type.GetType("MyClass"),"hello world")] 

but when I try to run this App. I give error:

"An attribute argument must be a constant expression, typeof 开发者_开发技巧expression or array creation expression of an attribute parameter type".

can anyone explain me what the problem?


You're using Type.GetType("MyClass") where you should have typeof(MyClass).


I suspect its the Type.GetType("MyClass");

can you try typeof(MyClass) instead, passing the type and not a string?


Type.GetType() is a method (i.e. not a constant expression), as the others said, use typeof.

[DefaultValue(typeof(MyClass),"Convertible String")]

Edit: To enable the conversion of the string to your custom class you need to associate a TypeConverter with it, see the examples-section of this documentation to get an idea of how to do so.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜