C# 如果不使用new()方法,如何使用另一个属性设置属性值?
我不想使用NEW()方法初始化对象,而是想使用以下参数初始化对象:C# 如果不使用new()方法,如何使用另一个属性设置属性值?,c#,http-post,C#,Http Post,我不想使用NEW()方法初始化对象,而是想使用以下参数初始化对象: [HttpPost] public ActionResult UpdateXXX([FromBody] MyObj myob) { ... } 但该对象具有通过其他道具获取设置值的道具: public class MyObj { public long id { get; set; } public string
[HttpPost]
public ActionResult UpdateXXX([FromBody] MyObj myob)
{
...
}
但该对象具有通过其他道具获取设置值的道具:
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;
}
}
}
公共类MyObj
{
公共长id{get;set;}
公共字符串月份{get;set;}
十进制价格{get;set;}
公共十进制价格
{
得到
{
如果(月份<新日期时间(2019年9月1日))
{
_价格=50
}
退货价格;
}
设置
{
如果(月>=新日期时间(2019年9月1日))
_价格=价值;
}
}
}
我知道构造函数中的init the month选项,但是我可以在HttpPost请求中执行吗?尝试使用只读成员 就你而言:
decimal readonly _priceReadOnly { get; set; }
decimal _price { get; set; }
而且像价格一样
并从raedonly中获取值(仅当正则表达式为null时)您希望避免哪一个新值?约会时间?res变量是什么?From:public long id{get;set;}To:public long id=123;我在类名上有一个错误,对不起,我现在已经修复了。我想避免使用MyObj类的新()