简体   繁体   中英

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#

I want to call all the functions using the interface method, but I don't want to make an instance of the class that implements the interface. I did some projects in the past where I did ISomeInterface proxy = ChannelFactory<SomeImplementation>().CreateChannel() and then used interface methods like proxy.Method() . I want to do something like that, probably without ChannleFactory if possible, and I'm wondering if it is possible.

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();
        }

The error that pops up is that proxy is not set to an instance of an object.

An interface is just a contract, it has nothing to do with an instance. If a class implements the interface, you can typecast an instance to that interface and call the methods/properties defined in the interface.. (It's NOT even a proxy)

The interface doesn't contain any implementation. It's just a set of agreements that a class must implement to stick to the contract.

So you must have an instance.

In your example: proxy.PrintMenu(); who has implemented the PrintMenu() ?

I could be something like:

The interface:

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

First implementation:

// 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;
    }
}

Other implementation:

// 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;
    }
}

Your main:

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM