我的类将自身传递给另一个类中的方法。这个外部方法(下面代码的最后一行)相应地改变了我传递的对象,返回控制权,然后继续它的快乐方式。但是最后一行 ThisPaymentGateway.Process(ref this);
在设计时这样说:
Cannot pass 'this' as a ref or out argument because it is read-only***
那里没有只读属性。我该如何解决这个问题?
using System;
namespace Something
{
namespace Finance
{
namespace Donations
{
public class Electronic1 : Donation
{
private PaymentGateway ThisPaymentGateway { get; set; } = new PaymentGateway();
public void RunController()
{
if (DonorType.ToUpper() == "IND" && (PaymentMethod.ToUpper() == "CREDIT CARD" || PaymentMethod.ToUpper() == "ECHECK"))
{
ThisPaymentGateway.Process(ref this);
}
}
}
}
}
}
最佳答案
从调用和 Process 方法签名中删除“ref”
如果你只是想“改变对象”然后返回,你不需要通过 ref 传递它。类已经是引用类型,因此如果您在 Process 中更改对象属性,它会在源对象中更改它们。
关于c# - 无法将 'this' 作为 ref 或 out 参数传递,因为它是只读的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39400755/