错误消失了.
public struct DealImportRequest { public DealRequestBase DealReq { get; set; } public int ImportRetryCounter { get; set; } public DealImportRequest(DealRequestBase drb) { DealReq = drb; ImportRetryCounter = 0; } }
>错误CS0188:在这个对象的所有字段都被赋值之前,不能使用’this’对象
>错误CS0843:自动实现属性的备份字段
在将控制权返回给调用者之前,必须完全指定“DealImportRequest.DealReq”.考虑从构造函数初始化器调用默认构造函数.
解决方法
public DealImportRequest(DealRequestBase drb) : this() { DealReq = drb; ImportRetryCounter = 0; }
从语言规范:
10.7.3 Automatically implemented properties
When a property is
specified as an automatically
implemented property,a hidden backing
field is automatically available for
the property,and the accessors are
implemented to read from and write to
that backing field. […] Because the
backing field is inaccessible,it can
be read and written only through the
property accessors,even within the
containing type. […] This
restriction also means that definite
assignment of struct types with
auto-implemented properties can only
be achieved using the standard
constructor of the struct,since
assigning to the property itself
requires the struct to be definitely
assigned. This means that user-defined
constructors must call the default
constructor.
另一个(更冗长)的替代方法当然是手动实现属性并在构造函数中自行设置后备字段.
请注意你拥有的结构是可变的. This is not recommended.我建议你把类型做成一个类(你的编译问题应该马上消失),或使类型不可变.实现这一点的最简单的方法是假设你所呈现的代码是整个结构体,将使setter为private(get; private set;).当然,您还应该确保不要在结构之后添加任何依赖于私有访问来修改字段的突变方法.或者,您可以使用只读备份字段来返回属性,并完全删除setter.