将默认值设置为 c# 上的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13054056/
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-10 01:29:29  来源:igfitidea点击:

Setting Default Value to a property on c#

c#

提问by jcvegan

Possible Duplicate:
Setting the default value of a DateTime Property to DateTime.Now inside the System.ComponentModel Default Value Attrbute

可能的重复:
在 System.ComponentModel 默认值属性中将 DateTime 属性的默认值设置为 DateTime.Now

Im creating a control like a calendar, and I want user to set de year they want to view. But If they don't, the property of SelectedYear must be the actual year.

我创建了一个像日历一样的控件,我希望用户设置他们想要查看的年份。但如果他们不这样做, SelectedYear 的属性必须是实际年份。

I try setting this Default Value like this:

我尝试像这样设置这个默认值:

[DefaultValue(DateTime.Today.Year)]
public int SelectedYear{
    get{
        return _selYear;
    }
    set{
        _selYear = value;
        UpdateCalendar();
    }
}

And I got the following error

我收到以下错误

An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type

采纳答案by JaredPar

The Yearproperty of DateTimeis a calculated value but you're trying to embed it into an attribute which is an immutable value stored in the binary. These two concepts aren't compatible.

Year属性DateTime是一个计算值,但您试图将其嵌入到一个属性中,该属性是存储在二进制文件中的不可变值。这两个概念不兼容。

What you want to do here is have a marker to note that the value isn't set yet and to return the current Yearin that case. This is a good place to use a nullable

你想要在这里做的是有一个标记来注意该值尚未设置并Year在这种情况下返回当前值。这是使用可空值的好地方

int? _selYear;
public int SelectedYear {
  get { return _selYear ?? DateTime.Today.Year; }
  set { 
    _selYear = value;
    UpdateCalendar();
  }
}

回答by Ed S.

Well, the error is pretty clear:

好吧,错误很明显:

An attribute argument must be a constant expression...

属性参数必须是常量表达式...

So, if that didwork, what would the default value actually be? Well, it would be the year in which you compiled the code. Probably not what you want.

那么,如果这确实有效,那么默认值实际上是多少?好吧,那将是您编译代码的年份。可能不是你想要的。

If you really want to return the current year by default then simply initialize it to that in your constructor.

如果您真的想默认返回当前年份,那么只需在构造函数中将其初始化为该年份即可。

回答by Rohit Vats

Why not setthe backing fieldinstead like this in constructor -

为什么不setbacking field,而不是像这样的构造-

_selYear = DateTime.Today.Year

It will then always be defaultvalue if user doesn't set it.

default如果用户没有设置它,它将始终是有价值的。

回答by Francis P

DateTime.Today.Yearis not a constant value, since it changes every year. You could check whether the _selYearhas been specified or not using this:

DateTime.Today.Year不是一个常数值,因为它每年都在变化。您可以使用以下命令检查是否_selYear已指定:

public int SelectedYear{
    get{
        if (_selYear.Equals(0)){
            return  DateTime.Today.Year;
        }
        else{
            return _selYear;
        }
    }
    set{
        _selYear = value;
        UpdateCalendar();
    }
}

This is considering you initiated _selYear to 0.

这是考虑到您将 _selYear 初始化为 0。

回答by Kundan Singh Chouhan

This is because the DefaultValueonly accepts constant values. You can do this via another approach some thing like below

这是因为DefaultValue只接受常量值。您可以通过另一种方法执行此操作,如下所示

public int SelectedYear{
    get{
        return _selYear == 0 ? DateTime.Now.Year : _selYear;
    }
    set{
        _selYear = value;
        UpdateCalendar();
    }
}

Hope this will help !!

希望这会有所帮助!

回答by Viacheslav Smityukh

You should initialize a value of a property explicitly. The DefaultValue attribute does not initialize property by the value it just provide metadata which value is default.

您应该显式初始化属性的值。DefaultValue 属性不会通过它只提供元数据的值来初始化属性,该值是默认值。

You can initialize it in ctor of a type or you can write a custom code which will reflect a type and initialize properties by the values from the type metadata.

您可以在类型的 ctor 中对其进行初始化,也可以编写自定义代码,该代码将反映类型并通过类型元数据中的值初始化属性。

回答by Miguel Angelo

Use AmbientValueAttributeto indicate that when you set the property to the default value, it actually means to get the current date.

使用AmbientValueAttribute表明,当你的属性设置为默认值,实际上就意味着获得当前的日期。

int _selYear = 0;

[AmbientValue(0)]
[DefaultValue(0)]
public int SelectedYear{
    get{
        if (_selYear == 0)
            return DateTime.Today.Year;
        return _selYear;
    }
    set{
        _selYear = value;
        UpdateCalendar();
    }
}

If you want to use the 0 value, then you can make it nullable, but never return null, only allow setting null:

如果你想使用 0 值,那么你可以让它为空,但永远不要返回空值,只允许设置空值:

int _selYear = null;

[AmbientValue(null)]
[DefaultValue(null)]
public int? SelectedYear{
    get{
        return _selYear ?? DateTime.Today.Year;
    }
    set{
        _selYear = value;
        UpdateCalendar();
    }
}

Now, it is the null value that represents the current year.

现在,它是代表当前年份的空值。

The AmbientValueAttribute means:if you set this property to that value, when you get the property, another value will be returned from the environment, and that the return value in that case may vary, just like the current date.

AmbientValueAttribute 的意思是:如果你把这个属性设置为那个值,当你得到这个属性时,会从环境中返回另一个值,这种情况下的返回值可能会有所不同,就像当前日期一样。