DefaultValue 属性不适用于我的自动属性

发布于 2024-08-16 05:44:58 字数 207 浏览 4 评论 0原文

我有以下自动属性,

[DefaultValue(true)]
public bool RetrieveAllInfo { get; set; }

当我尝试在代码中使用它时, 我发现默认 false 是 false 我假设这是 bool 变量的默认值,确实任何人都知道出了什么问题!

I have the following Auto Property

[DefaultValue(true)]
public bool RetrieveAllInfo { get; set; }

when I try to use it inside the code i find the default false for is false I assume this is the default value to a bool variable, does anyone have a clue what is wrong!?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

听风念你 2024-08-23 05:44:58

DefaultValue 属性仅用于告诉 Visual Studio 设计者(例如在设计表单时)属性的默认值是什么。它不会在代码中设置属性的实际默认值。

更多信息请参见:http://support.microsoft.com/kb/311339

The DefaultValue attribute is only used to tell the Visual Studio Designers (for example when designing a form) what the default value of a property is. It doesn't set the actual default value of the attribute in code.

More info here: http://support.microsoft.com/kb/311339

情归归情 2024-08-23 05:44:58

[DefaultValue] 仅由(例如)序列化 API(如 XmlSerializer)和某些 UI 元素(如 PropertyGrid)使用。它本身不设置值;您必须为此使用构造函数:

public MyType()
{
    RetrieveAllInfo = true;
}

或手动设置字段,即不使用自动实现的属性:

private bool retrieveAllInfo = true;
[DefaultValue(true)]
public bool RetrieveAllInfo {
    get {return retrieveAllInfo; }
    set {retrieveAllInfo = value; }
}

或者,使用更新的 C# 版本(C# 6 或更高版本):

[DefaultValue(true)]
public bool RetrieveAllInfo { get; set; } = true;

[DefaultValue] is only used by (for example) serialization APIs (like XmlSerializer), and some UI elements (like PropertyGrid). It doesn't set the value itself; you must use a constructor for that:

public MyType()
{
    RetrieveAllInfo = true;
}

or set the field manually, i.e. not using an automatically implemented-property:

private bool retrieveAllInfo = true;
[DefaultValue(true)]
public bool RetrieveAllInfo {
    get {return retrieveAllInfo; }
    set {retrieveAllInfo = value; }
}

Or, with more recent C# versions (C# 6 or above):

[DefaultValue(true)]
public bool RetrieveAllInfo { get; set; } = true;
云之铃。 2024-08-23 05:44:58

对此的一种破解方法是链接。

简而言之,在构造函数的末尾调用该函数。

static public void ApplyDefaultValues(object self)
   {
        foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(self)) {
            DefaultValueAttribute attr = prop.Attributes[typeof(DefaultValueAttribute)] as DefaultValueAttribute;
            if (attr == null) continue;
            prop.SetValue(self, attr.Value);
        }
   }

One hack for this is on this link.

In short, call this function at the end of constructor.

static public void ApplyDefaultValues(object self)
   {
        foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(self)) {
            DefaultValueAttribute attr = prop.Attributes[typeof(DefaultValueAttribute)] as DefaultValueAttribute;
            if (attr == null) continue;
            prop.SetValue(self, attr.Value);
        }
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文