[英]How do I pass an object into one of its own functions?
Let's say I have the following:假设我有以下内容:
public class Order
{
public string orderNumber { get; set; }
public string customerName { get; set; }
public bool isRewardsMember { get; set; }
public string transactionNumber { get; set; }
public List<Guid> cart { get; set; }
public CouponResponse GeneratedCoupons = GenerateCoupons(this); <--- How could I do this?
public CouponResponse GenerateCoupons(Order order)
{
string congrats = "Based on this transaction we believe you would like these coupons.";
CouponResponse.orderGuid = order.orderGuid;
CouponResponse.orderNumber = order.orderNumber;
// blah blah blah
}
}
And I wanted to automatically create some kind of string response based on the object on generation.我想自动创建某种基于 object 生成的字符串响应。 Could I do something like the above and just do something like this.order, or should I just use a constructor and bypass this?我可以做类似上面的事情并只做类似this.order的事情,还是应该只使用构造函数并绕过它? I want to try to have this function to be callable in case couponresponse is for whatever reason not created, or simply use this for further data collection later.我想尝试让这个 function 可以调用,以防由于任何原因未创建优惠券响应,或者只是稍后将其用于进一步的数据收集。
Passing an object for 2 properties seems like unnecessary coupling, but you could use the following syntax to make GeneratedCoupons do what you want:为 2 个属性传递 object 似乎是不必要的耦合,但您可以使用以下语法使 GeneratedCoupons 做您想做的事情:
public CouponResponse GeneratedCoupons => GenerateCoupons(this);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.