简体   繁体   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#

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() . 我过去做过一些项目,其中我做了ISomeInterface proxy = ChannelFactory<SomeImplementation>().CreateChannel() ,然后使用了诸如proxy.Method()类的接口方法。 I want to do something like that, probably without ChannleFactory if possible, and I'm wondering if it is possible. 我想做这样的事情,如果可能的话可能没有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();
        }

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(); 在您的示例中: proxy.PrintMenu(); who has implemented the PrintMenu() ? 谁实现了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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#如何在不使方法静态的情况下从界面内的另一个类调用方法? - C# How do I call a method from another class inside of my interface without making the method static? C#:将子类实例分配给抽象类实现的接口 - C#: Assign child class instance to interface implemented by abstract class 无法在C#中将实现接口的类的实例添加到所述接口的List中 - Can't add instance of class that implements an interface to a List of said interface in C# 如何调用在接口中定义并在C#中的类中实现的方法? - How to call a method defined in interface and implemented in class in C#? C# 接口 function 返回 class 取决于实现的位置 - C# interface function returning class depending on where it gets implemented 如何在另一个 class 中使用在两个不同类中声明的列表,而不使用 C# 中的接口? - How can I use a list declared in two different classes in another class, without using interface in C#? C#接口由空抽象类实现 - C# Interface implemented by empty abstract class 在 C# 中创建类的实例 - making instance of class in c# 确定IDisposable应该扩展接口还是在实现该接口的类上实现 - Determining if IDisposable should extend an interface or be implemented on a class implementing said interface C# 调用在许多类中实现的接口方法而没有这些类的实例 - C# call interface method implemented in many classes without having instance to those classes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM