繁体   English   中英

如何在不使用 new() 方法的情况下设置属性值使用另一个属性?

[英]How can I set property value use another property without use new() method?

我想不使用 NEW() 方法而是通过以下参数来初始化 object:

[HttpPost]
        public ActionResult UpdateXXX([FromBody] MyObj myob)
        {
            ...
        }

但是 object 的道具可以通过另一个道具获得设定值:

public class MyObj 
    {
        public long id { get; set; }
        public string month{ get; set; }
        decimal _price { get; set; }
        public decimal price
        {

            get
            {
               
                if (month< new DateTime(2019, 9, 1))
                {
                    _price = 50
                }
                return _price ;

            }
            set
            {
               
                if (month>= new DateTime(2019, 9, 1))
                    _price = value;


            }
        }
      

    }

我知道构造函数中的 init the month 选项,但是我可以在 HttpPost 请求中这样做吗?

尝试使用只读成员。

在你的情况下:

decimal readonly _priceReadOnly { get; set; }

decimal _price { get; set; }

并像_price一样初始化它

并从 raedonly 如果常规是 null

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM