简体   繁体   English

如何将 object 传递给它自己的函数之一?

[英]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.

相关问题 如何将数据从数据库从一个控制器传递到在ASP.NET Core中拥有自己的数据库的另一个控制器? - How do I pass data from a database from one controller to another controller that has its own database in ASP.NET Core? 如何将对象转换为自己的类型? - How do I cast an object to its own type? 如何在函数之间传递对象 - How can I pass an object between functions 如何传递物体? - How do I pass an object? 如何使用自己的DataTemplate DependencyProperty实现WPF控件? - How do I implement a WPF control with its own DataTemplate DependencyProperty? 如何防止班级引用自己的班级? - How do i prevent a class referencing its own class? 如何让用户控件更新自己的属性? - How do I make a usercontrol update its own property? 如何为一个类创建对象并在一行中使用其功能 - How to create an object for a class and use its functions in one line 如何将 XAML 文本框绑定到另一个文本框的输入或它自己的输入,然后将输入保存到 MVVM 中的数据对象? - How do I bind a XAML TextBox to either another TextBox's input or its own input, and then save the input to the data object in MVVM? 如何将C#接口传递给外部COM对象以供使用? - How do I pass a C# interface to an external COM object for its use?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM