[EDIT]
I organize my question again,
Models for parameter
public class PaymentModel
{
...
}
public class CCPaymentModel : PaymentModel
{
...
}
public class PaypalPaymentModel : PaymentModel
{
...
}
public class GooglePaymentModel : PaymentModel
{
...
}
Interface class
public interface IPayment<T> where T : PaymentModel
{
...
}
Models (get inheritance from 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>
{...}
Controller (Create instance)
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);
}

[EDIT #2]
With this,
public interface IPayment {
}
public interface IPayment<T> : IPayment where T : PaymentModel
{
void MakePayment(string pickno);
void makeRefund(T refundInfo);
}
I got an error, Error 1 ‘com.WebUI.Models.IPayment’ does not contain a definition for ‘MakePayment’ and no extension method ‘MakePayment’ accepting a first argument of type ‘Ecom.WebUI.Models.IPayment’ could be found (are you missing a using directive or an assembly reference?)
So, to avoid that error, I move MakePayment method to upper interface class,
public interface IPayment {
void MakePayment(string pickno);
}
public interface IPayment<T> : IPayment where T : PaymentModel
{
void makeRefund(T refundInfo);
}
Now, the error is gone, BUT how should I do in makeRefund case?
I can not move to upper interface class because I need generic type parameter.
Could you help me a little more please?
You would want to have another IPayment interface without the generics from which IPayment inherits from. That is:
EDIT: If you really don’t want to have an
IPaymentbase interface, then you’d have to treat them as typeobject:But that might cost you later; regardless you’re going to have to cast to work with the specific implementation types. You’re really best using a base interface. You may even use it in a nice way:
So you can reference and use the
PaymentModelwithout knowing that it’s actually aGooglePaymentModelspecifically.EDIT: Based on your comment, you might have something like:
You could even have a non-generic
MakeRefundversion typed againstPaymentModelso your calling code might not care if it’s aGooglePaymentor not. (but that could cause other issues if they pass aPayPalPayment, so that’s up to you)EDIT: Based on your latest code, you’ll want something like this:
Your controller/factory would look like:
Your usage code would somewhere have to cast it to the known payment type to use:
EDIT: Based on your latest edits, this is what you want. Drive your base IPayment calls against a PaymentModel. Then in the specific implementations you can cast or type-check at runtime:
Then your
Controller: