如何声明用于泛型实例的变量?
在控制器中,我需要创建依赖于支付类型的实例,并且每个类都有不同类型的参数。这就是我使用泛型类型的原因。但我不知道我需要设置什么类型来为每个支付类定义变量。
参数模型
public class PaymentModel
{
public string orderNo { get; set;}
}
public class CCPaymentModel : PaymentModel
{
public string CCNo {get; set;}
public string expDate {get; set;}
}
public class PaypalPaymentModel : PaymentModel
{
public string paypalID {get; set;}
}
public class GooglePaymentModel : PaymentModel
{
public string googleID {get; set;}
}
接口类,我使用通用类型参数,因为每种支付类型需要不同类型的参数。
public interface IPayment<T> where T : PaymentModel
{
void makePayment(string orderNo);
void makeRefund(T refundInfo);
}
楷模,
public class SagePayment
: IPayment<CreditCardPaymentInfo>
{
public void MakePayment( CreditCardPaymentInfo creditCardPaymentInfo ) {
// make payment
}
public void MakeRefund( CreditCardPaymentInfo creditCardPaymentInfo ) {
// make refund
}
}
public class GooglePayment
: IPayment<GooglePaymentModel>
{
public void MakePayment( GooglePaymentModel paymentInfo ) {
// make payment
}
public void MakeRefund( GooglePaymentModel paymentInfo ) {
// make refund
}
}
public class PaypalPayment
: IPayment<PayPalPaymentModel>
{
public void MakePayment( PayPalPaymentModel paymentInfo ) {
// make payment
}
public void MakeRefund( PayPalPaymentModel paymentInfo ) {
// make refund
}
}
控制器(创建实例)
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();
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();
payinfo = getPaymentInfo(paytype, orderNo); // it return GooglePaymentModel type object
}
paymentProcess.MakePayment(payinfo);
}
为了避免错误,我可以这样做,
public void Charge(string paytype,orderNo){
if (Regex.IsMatch(paytype, "^Credit Card"))
{
IPayment<CCPaymentModel> paymentProcess = new SagePayment();
payinfo = getPaymentInfo(paytype, orderNo);
paymentProcess.MakePayment(payinfo);
}
else if (Regex.IsMatch(paytype, "^PayPal"))
{
IPayment<PaypalPaymentModel> paymentProcess = new PayPalPayment();
payinfo = getPaymentInfo(paytype, orderNo);
paymentProcess.MakePayment(payinfo);
}
else if (Regex.IsMatch(paytype, "^Google"))
{
IPayment<GooglePaymentModel> paymentProcess = new GooglePayment();
payinfo = getPaymentInfo(paytype, orderNo);
paymentProcess.MakePayment(payinfo);
}
}
public void Refund(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();
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();
payinfo = getPaymentInfo(paytype, orderNo); // it return GooglePaymentModel type object
}
paymentProcess.MakeRefund(payinfo);
}
但我知道这不是正确的方法。
有谁知道,请指教。