为什么C#(或.NET)不允许我们在接口中放置静态/共享方法?

Hao*_*Hao 8 c# interface helper design-rationale

为什么C#(或.NET)不允许我们在接口中放置静态/共享方法?

看似从这里重复.但我的想法有点不同,我只是想为我的插件(接口)添加一个帮助器

不应该C#至少允许这个想法吗?

namespace MycComponent
{

    public interface ITaskPlugin : ITaskInfo
    {
        string Description { get; }
        string MenuTree { get; }
        string MenuCaption { get; }

        void ShowTask(Form parentForm);
        void ShowTask(Form parentForm, Dictionary<string, object> pkColumns);

        ShowTaskNewDelegate ShowTaskNew { set; get; }
        ShowTaskOpenDelegate ShowTaskOpen { set; get; }        

        // would not compile with this:
        public static Dictionary<string, ITaskPlugin> GetPlugins(string directory)
        {

            var l = new Dictionary<string, ITaskPlugin>();

            foreach (string file in Directory.GetFiles(directory))
            {
                var fileInfo = new FileInfo(file);   
                if (fileInfo.Extension.Equals(".dll"))
                {
                    Assembly asm = Assembly.LoadFile(file);       
                    foreach (Type asmType in asm.GetTypes())
                    {

                        if (asmType.GetInterface("MycComponent.ITaskPlugin") != null)
                        {
                            var plugIn = (ITaskPlugin)Activator.CreateInstance(asmType);
                            l.Add(plugIn.TaskName, plugIn);
                        }

                    }


                }
            }

            return l;
        } // GetPlugins.  would not compile inside an interface
    }



    /* because of the error above, I am compelled to 
       put the helper method in a new class. a bit overkill when the method should
       be closely coupled to what it is implementing */
    public static class ITaskPluginHelper
    {
        public static Dictionary<string, ITaskPlugin> GetPlugins(string directory)
        {

            var l = new Dictionary<string, ITaskPlugin>();

            foreach (string file in Directory.GetFiles(directory))
            {
                var fileInfo = new FileInfo(file);   
                if (fileInfo.Extension.Equals(".dll"))
                {
                    Assembly asm = Assembly.LoadFile(file);       
                    foreach (Type asmType in asm.GetTypes())
                    {

                        if (asmType.GetInterface("MycComponent.ITaskPlugin") != null)
                        {
                            var plugIn = (ITaskPlugin)Activator.CreateInstance(asmType);
                            l.Add(plugIn.TaskName, plugIn);
                        }

                    }


                }
            }

            return l;
        } // GetPlugins    
    } // ITaskPluginHelper
}
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 11

接口的概念是代表合同,而不是实现.

我不记得随便IL是否真的不会允许与接口的实现静态方法-我偷偷摸摸怀疑它-但有些muddies概念.

我可以看到你的观点 - 知道哪些辅助方法与接口连接有时很有用(扩展方法在那里特别相关)但我个人想要将它们放在一个单独的类中,只是为了保持精神状态.模特干净.


Jon*_*een 6

我已经多次遇到这种情况并做了一些研究.可悲的是,IL实际上支持这一点.我对此感到非常沮丧,我写了一篇关于它的博客文章.你可以在这里找到它.