C# 在基接口中定义的调用方法

C# 在基接口中定义的调用方法,c#,interface,C#,Interface,如果我有以下接口结构 public interface IPaymentTypeBase { void PayNow(); double Amount { get; set; } } public interface IPaymentTypePayPal : IPaymentTypeBase { string UserName { get; set; } string Password { get; set; } } public interface IPay

如果我有以下接口结构

public interface IPaymentTypeBase
{
    void PayNow();
    double Amount { get; set; }
}

public interface IPaymentTypePayPal : IPaymentTypeBase
{
    string UserName { get; set; }
    string Password { get; set; }
}

public interface IPaymentMethod<T>
{
}
公共接口IPaymentTypeBase
{
立即付款()无效;
双倍金额{get;set;}
}
公共接口IPaymentTypePayPal:IPaymentTypeBase
{
字符串用户名{get;set;}
字符串密码{get;set;}
}
公共接口IPaymentMethod
{
}
然后我有以下几节课

public class PaymentTypePayPal : IPaymentTypePayPal
{
    public string UserName { get; set; }
    public string Password { get; set; }

    public void PayNow()
    {
        throw new NotImplementedException();
    }
}

public class PaymentMethod<T> : IPaymentMethod<T> where T : IPaymentTypeBase
{
}
公共类PaymentTypePayPal:IPaymentTypePayPal
{
公共字符串用户名{get;set;}
公共字符串密码{get;set;}
公众免费现收现付()
{
抛出新的NotImplementedException();
}
}
公共类PaymentMethod:IPaymentMethod,其中T:IPaymentTypeBase
{
}
然后在我的web应用程序中,我有这个

IPaymentMethod<IPaymentTypePayPal> payer = (IPaymentMethod<IPaymentTypePayPal>) new PaymentMethod<PaymentTypePayPal>();
IPaymentMethod付款人=(IPaymentMethod)新的付款方法();
我现在想调用
payer.PayNow()但我只是在界面等方面迷失了方向,似乎无法实现这一点

我相信这是一件简单的事情,但我完全没有抓住要点

有人能帮忙吗

编辑

这里的目的是要有一套界面,如贝宝,凭证,信用卡,所有这些都做自己的支付网关类型的东西


因此,我想实例化一个类,该类将支付类型作为接口,并调用该接口PayNow方法。

付款人的类型为
IPaymentMethod

但是
IPaymentMethod
被定义为

public interface IPaymentMethod<T>
{
}
公共接口IPaymentMethod
{
}
因此,它没有方法,您无法对其调用
PayNow()

PaymentMethod
也是如此,因此您也不能对
PaymentMethod
的实例调用任何方法


也许你可以再解释一下你的意图。

我不太清楚你为什么要使用PaymentMethod类。。。但是,为什么不能将界面IPaymentMethod修改为以下内容:

public interface IPaymentMethod<T> : IPaymentTypeBase
公共接口IPaymentMethod:IPaymentTypeBase

为了让你能亲自调用patNow方法,每当涉及到继承时,我会使用类而不是接口。此外,在这种情况下,您可能需要更多的间接性。如果您将Payment建模为一个抽象基类(使用PayNow()和Amount),并将PayPalPayment建模为子类,那么只需从工厂实例化适当的子类,您就可以进行设置了,具有您的方法所具有的所有灵活性,但我认为更易于理解和维护。(不知道应用程序的所有其他要求)

是的,我知道,但如果我现在有IPaymentTypeCreditCard等,我如何在paypal或信用卡中调用PayNow方法?我的界面结构需要修改成什么?IPaymentTypeBase payer=new PaymentTypePayPal()怎么样