今天我在使用结构时遇到了一个让我措手不及的问题,我希望有人可以为我解释一下。
我有一个这样定义的结构:
public struct PaymentDetail
{
public Decimal Amount{get;set;}
public string CheckNumber{get;set;}
public PaymentType PaymentType{get;set;}
}
我有一个包含此信息的课程
public class Transaction
{
public PaymentDetail Payment{get;}
}
我有一个演示模型,我想在其中设置这样的基础属性
public class ViewModel
{
public Decimal Amount
{
get{return _Transaction.PaymentDetail.Amount;}
set
{
//This is the offending line of code
_Transaction.PaymentDetail.Amount = value;
RaisePropertyChanged("Amount");
}
}
}
奇怪的是,如果我将 Payment 属性更改为这样的公共字段,我可以完成这项工作:
public class Transaction
{
public PaymentDetail Payment;
}
显然有一些我不了解导致这种情况的结构。这是一个坏主意吗?有没有更好的办法?我究竟做错了什么?