MinValue & MaxValue attribute for properties

前端 未结 5 2192
花落未央
花落未央 2020-12-17 15:27

Is it possible to make attribute which can limit minimum or maximum value of numbers.

Example:

[MinValue(1), MaxValue(50)]
public int Size { get; set         


        
5条回答
  •  天命终不由人
    2020-12-17 16:23

    Although it is possible to create a custom attribute, attributes are just metadata for the member they annotate, and cannot change its behavior.

    So, you won't get the behavior you want with a plain attribute. You need something to process the attributes in order to enact the desired behavior.

    Take a look at TypeConverters for a possibility.

提交回复
热议问题