净DefaultValueAttribute上的属性

编程入门 行业动态 更新时间:2024-10-25 08:26:42
本文介绍了净DefaultValueAttribute上的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我得到这个code用户控件:

I got this code in a user control:

[DefaultValue(typeof(Color), "Red")] public Color MyColor { get; set; }

如何修改 MyColor 来作为它的默认值?

How can I change MyColor to be its default value?

推荐答案

这是非正式的,但你可以通过反射使用它,例如,发生在你的构造如下:

It is informal, but you can use it via reflection, for example, place in your constructor the following:

foreach (PropertyInfo p in this.GetType().GetProperties()) { foreach (Attribute attr in p.GetCustomAttributes(true)) { if (attr is DefaultValueAttribute) { DefaultValueAttribute dv = (DefaultValueAttribute)attr; p.SetValue(this, dv.Value); } } }

更多推荐

净DefaultValueAttribute上的属性

本文发布于:2023-05-25 10:42:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/226456.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   DefaultValueAttribute

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!