C# .Net DefaultValueAttribute 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/705553/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 14:08:52  来源:igfitidea点击:

.Net DefaultValueAttribute on Properties

c#.net

提问by Melursus

I got this code in a user control:

我在用户控件中得到了这个代码:

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

How can I change MyColorto be its default value?

如何更改MyColor为其默认值?

采纳答案by nothrow

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);
        }
    }
}

回答by OregonGhost

The DefaultValueAttributedoes not set the property to the value, it is purely informational. The Visual Studio designer will display this value as non-bold and other values as bold (changed), but you'll still have to set the property to the value in the constructor.

DefaultValueAttribute不设置该属性的值,它是纯粹的信息。Visual Studio 设计器会将此值显示为非粗体,将其他值显示为粗体(已更改),但您仍必须将该属性设置为构造函数中的值。

The designer will generate code for the property if the value was set by the user, but you can remove that code by right clicking on the property and clicking Reset.

如果该值是由用户设置的,设计器将为该属性生成代码,但您可以通过右键单击该属性并单击 来删除该代码Reset

回答by lc.

Are you initializing MyColorin your constructor?

MyColor在你的构造函数中初始化吗?

The DefaultValueattribute does not actually set any values. It simply instructs the designer for which value to not generate code and will also show the default value non-bold to reflect this.

DefaultValue属性实际上并未设置任何值。它只是指示设计人员不要为哪个值生成代码,并且还将显示非粗体的默认值以反映这一点。

回答by Timothy Khouri

The "DefaultValue" attribute does not write code for you... but rather it is used for you to tell people (such as Mr Property Grid, or Mr Serializer Guy) that youplan to set the default value to Red.

“DefaultValue”属性不会为您编写代码……而是用于告诉人们(例如 Mr Property Grid 或 Mr Serializer Guy)打算将默认值设置为 Red。

This is useful for things like the PropertyGrid... as it will BOLDany color other than Red... also for serialization, people may choose to omit sending that value, because you informed them that it's the default :)

这对于诸如 PropertyGrid 之类的东西很有用......因为它会将红色以外的任何颜色加粗......对于序列化,人们可能会选择省略发送该值,因为您告诉他们这是默认值:)

回答by Marc Gravell

DefaultValueAttributeis not used by the compiler, and (perhaps confusingly) it doesn't set the initial value. You need to do this your self in the constructor. Places that douse DefaultValueAttributeinclude:

DefaultValueAttribute编译器不使用它,并且(可能令人困惑)它不设置初始值。您需要在构造函数中自己执行此操作。那个地方应用DefaultValueAttribute包括:

  • PropertyDescriptor- provides ShouldSerializeValue(used by PropertyGridetc)
  • XmlSerializer/ DataContractSerializer/ etc (serialization frameworks) - for deciding whether it needs to be included
  • PropertyDescriptor- 提供ShouldSerializeValue(由PropertyGrid等使用)
  • XmlSerializer// DataContractSerializeretc(序列化框架)——用于决定是否需要包含

Instead, add a constructor:

相反,添加一个构造函数:

public MyType() {
  MyColor = Color.Red;
}

(if it is a structwith a custom constructor, you need to call :base()first)

(如果是struct带有自定义构造函数的,需要先调用:base()

回答by infabo

I adapted the answer of Yossarian:

我改编了 Yossarian 的回答:

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