c# - 有没有更自动的方法来包装这些接口(interface)

标签 c# .net oop interface

我正在做一个多平台的事情,但我不太擅长 OOP。目前我的代码是:

    public interface IMessageBox {
        void Show(string Text);
        void Show(string Text, string Description, MessageBoxType Type);
        MessageBoxResult ShowYesNo(string Text, string Description, MessageBoxType Type);
        MessageBoxResult ShowYesNoCancel(string Text, string Description, MessageBoxType Type);
    }

    public class MessageBox : InstanceGenerator {
        public static void Show(string Text) {
            MessageBoxImpl.Show(Text);
        }

        public static void Show(string Text, string Description, MessageBoxType Type) {
            MessageBoxImpl.Show(Text, Description, Type);
        }
        public static MessageBoxResult ShowYesNo(string Text, string Description, MessageBoxType Type) {
            return MessageBoxImpl.ShowYesNo(Text, Description, Type);
        }
        public static MessageBoxResult ShowYesNoCancel(string Text, string Description, MessageBoxType Type) {
            return MessageBoxImpl.ShowYesNoCancel(Text, Description, Type);
        }
    }

protected class InstanceGenerator {
        public static IMessageBox MessageBoxImpl = null;
        public static IWindow WindowImpl = null;

        private static Assembly Instance = null;
        private static string InstanceName = null;

        private static Assembly LoadAssembly(string lib) {
            string AppPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            Assembly assembly = Assembly.LoadFile(Path.Combine(AppPath, lib + ".dll"));
            return assembly;
        }

        private static object CreateInstance(string @class) {
            Type type = Instance.GetType(InstanceName + "." + @class);
            return Activator.CreateInstance(type);
        }

        private static object CreateInstanceFromPath(string lib, string @class) {
            string AppPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            Assembly assembly = Assembly.LoadFile(Path.Combine(AppPath, lib + ".dll"));
            Type type = assembly.GetType(lib + "." + @class);
            return Activator.CreateInstance(type);
        }


        /// <summary>
        /// Inits the whole thing
        /// </summary>
        public static void Init() {
            if (CurrentOS.IsWindows)
                InstanceName = "Lib.Windows";
            else if (CurrentOS.IsMac)
                InstanceName = "Lib.MacOS";
            else if (CurrentOS.IsLinux)
                InstanceName = "Lib.Linux";
            else // no implementation for other OSes
                throw new Exception("No implementation of Lib for this OS");

            Instance = LoadAssembly(InstanceName);

            // initialize the classes
            MessageBoxImpl = (IMessageBox) CreateInstance("MessageBox");
        }
    }

编辑:

其中 InstanceGenerator 返回从程序集中加载的 IMessageBox 实例。有没有更好的方法来创建/连接到实例?实现所有相同的静态方法看起来根本不是一个好的解决方案。是否有更自动的方法来用类包装这些接口(interface),或者我做错了什么?

最佳答案

您当前尚未实现该界面。您不能使用静态方法来实现接口(interface)。您必须使用实例方法。当然,您可以在实现中调用静态方法,但这是另一回事。

听起来 MessageBox 不应该派生 InstanceGenerator - 我建议它应该组合它:

public class MessageBox : IMessageBox {

    private readonly InstanceGenerator generator;

    public MessageBox(InstanceGenerator generator) {
        this.generator = generator;
    }

    public static void Show(string Text) {
        generator.MessageBoxImpl.Show(Text);
    }

    public static void Show(string text, string description, MessageBoxType type) {
        generator.MessageBoxImpl.Show(text, description, type);
    }

    public static MessageBoxResult ShowYesNo(string text, string description, 
                                             MessageBoxType type) {
        return generator.MessageBoxImpl.ShowYesNo(text, description, type);
    }

    public static MessageBoxResult ShowYesNoCancel(string text,
                                                   string description,
                                                   MessageBoxType type) {
        return generator.MessageBoxImpl.ShowYesNoCancel(text, description, type);
    }
}

假设 MessageBoxImpl 尚未实现 IMessageBox,否则所有这些都是毫无意义的......

关于c# - 有没有更自动的方法来包装这些接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10017666/

相关文章:

c# - 处理 veracode 引发的跨站点脚本漏洞

c# - 调试程序集加载问题

c# - 传递参数给线程问题

c# - yield 返回 IEnumerator 或 IEnumerable

c# - Windows 服务总线 - 多个接收器

c# - ViewBag 未从基本 Controller 传递

c# - 为 Visual Studio 创建代码生成器插件

c# - ZeroMQ/ØMQ/0MQ 如何入门?

javascript - setInterval 在类内部创建游戏内时钟

python - 为什么我们要为链表中的头节点创建不同的类?