[编辑]
我再次整理我的问题,
参数模型
public class PaymentModel
{
...
}
public class CCPaymentModel : PaymentModel
{
...
}
public class PaypalPaymentModel : PaymentModel
{
...
}
public class GooglePaymentModel : PaymentModel
{
...
}
接口类
public interface IPayment<T> where T : PaymentModel
{
...
}
模型(从 IPayment 继承),
public class SagePayment
: IPayment<CreditCardPaymentInfo>
{
public void MakePayment( CreditCardPaymentInfo creditCardPaymentInfo ) {
// ...
}
public void MakeRefund( CreditCardPaymentInfo creditCardPaymentInfo ) {
// ...
}
}
public class GooglePayment
: IPayment<GooglePaymentModel>
{
public void MakePayment( GooglePaymentModel paymentInfo ) {
// ...
}
public void MakeRefund( GooglePaymentModel paymentInfo ) {
// ...
}
}
public class PaypalPayment
: IPayment<PayPalPaymentModel>
{...}
控制器(创建实例)
IPayment<???> paymentProcess; // //Error 1 Using the generic type 'com.WebUI.Models.IPayment<T>' requires 1 type arguments
if (Regex.IsMatch(paytype, "^Credit Card"))
{
paymentProcess = new SagePayment(); // it need CCPaymentModel type parameter
}
else if (Regex.IsMatch(paytype, "^PayPal"))
{
paymentProcess = new PayPalPayment(); // it need PaypalPaymentModel type parameter
}
else if (Regex.IsMatch(paytype, "^Google"))
{
paymentProcess = new GooglePayment(); // it need GooglePaymentModel type parameter
}
[EDIT]
public void Charge(string paytype,orderNo){
IPayment<???> paymentProcess; // //Error 1 Using the generic type 'com.WebUI.Models.IPayment<T>' requires 1 type arguments
Object payinfo;
if (Regex.IsMatch(paytype, "^Credit Card"))
{
paymentProcess = new SagePayment(); // <== Error, Can not casting
payinfo = getPaymentInfo(paytype, orderNo); // it return CCPaymentModel type object
}
else if (Regex.IsMatch(paytype, "^PayPal"))
{
paymentProcess = new PayPalPayment();
payinfo = getPaymentInfo(paytype, orderNo); // it return PaypalPaymentModel type object
}
else if (Regex.IsMatch(paytype, "^Google"))
{
paymentProcess = new GooglePayment(); // it return GooglePaymentModel type object
payinfo = getPaymentInfo(paytype, orderNo);
}
paymentProcess.MakePayment(payinfo);
}
[编辑#2]
有了这个,
public interface IPayment {
}
public interface IPayment<T> : IPayment where T : PaymentModel
{
void MakePayment(string pickno);
void makeRefund(T refundInfo);
}
我收到一个错误,错误 1 'com.WebUI.Models.IPayment' 不包含 'MakePayment' 的定义,并且没有扩展方法 'MakePayment' 接受类型为 'Ecom.WebUI.Models.IPayment' 的第一个参数(您是否缺少 using 指令或程序集引用?)
所以,为了避免这个错误,我将 MakePayment 方法移动到上层接口类,
public interface IPayment {
void MakePayment(string pickno);
}
public interface IPayment<T> : IPayment where T : PaymentModel
{
void makeRefund(T refundInfo);
}
现在,错误消失了,但是在 makeRefund 情况下我应该怎么做?我不能移动到上层接口类,因为我需要泛型类型参数。
你能帮我多一点吗?