簡體   English   中英

我想使用由另一個類實現的接口來調用函數,而無需在c#中創建該類的實例

[英]I want to use an interface that is being implemented by another class to call a function without making an instance of said class in c#

我想使用接口方法調用所有函數,但是我不想創建實現該接口的類的實例。 我過去做過一些項目,其中我做了ISomeInterface proxy = ChannelFactory<SomeImplementation>().CreateChannel() ,然后使用了諸如proxy.Method()類的接口方法。 我想做這樣的事情,如果可能的話可能沒有ChannleFactory ,我想知道是否有可能。

private static IUserInterface proxy;
        [STAThread]
        static void Main(string[] args)
        {
            bool closeApp = false;

            do
            {
                proxy.PrintMenu();

                int command;
                Int32.TryParse(Console.ReadKey().KeyChar.ToString(), out command);
                Console.WriteLine();
                closeApp = proxy.SendMenuCommand(command);
            } while (!closeApp);

            // Aplikacija ugasena
            Console.WriteLine("Application closed successfully. Press any key...");
            Console.ReadKey();
        }

彈出的錯誤是未將代理設置為對象的實例。

接口只是一個契約,它與實例無關。 如果一個類實現了該接口,則可以將實例轉換為該接口的類型,並調用該接口中定義的方法/屬性。 (甚至不是代理)

該接口不包含任何實現。 這是班級必須遵守的一組協議。

因此,您必須有一個實例。

在您的示例中: proxy.PrintMenu(); 誰實現了PrintMenu()

我可能是這樣的:

界面:

// This is the contract. (as you can see, no implementation)
public interface IUserInterface
{
    void PrintMenu();
    bool SendMenuCommand(int command);
}

第一次執行:

// The class implements that interface, which it MUST implements the methods.
// defined in the interface.  (except abstract classes)
public class MyUserInterface : IUserInterface
{
    public void PrintMenu()
    {
        Console.WriteLine("1 - Option one");
        Console.WriteLine("2 - Option two");
        Console.WriteLine("3 - Option three");
    }

    public bool SendMenuCommand(int command)
    {
        // do something.
        return false;
    }
}

其他實現:

// same for this class.
public class MyOtherUserInterface : IUserInterface
{
    public void PrintMenu()
    {
        Console.WriteLine("1) Submenu 1");
        Console.WriteLine("2) Submenu 2");
        Console.WriteLine("3) Submenu 3");
    }

    public bool SendMenuCommand(int command)
    {
        // do something.
        return true;
    }
}

您的主要:

private static IUserInterface menu;

[STAThread]
static void Main(string[] args)
{
    bool closeApp = false;

    // because the both classes implements the IUserInterface interface,
    // the both can be typecast to IUserInterface 
    IUserInterface menu = new MyUserInterface();

    // OR
    //IUserInterface menu = new MyOtherUserInterface();

    do
    {
        proxy.PrintMenu();

        int command;
        Int32.TryParse(Console.ReadKey().KeyChar.ToString(), out command);
        Console.WriteLine();
        closeApp = proxy.SendMenuCommand(command);
    } while (!closeApp);

    // Aplikacija ugasena
    Console.WriteLine("Application closed successfully. Press any key...");
    Console.ReadKey();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM