不是C#(或.NET)为什么应该允许我们把一个静态/共享方法的接口里面呢? [英] Why shouldn't C#(or .NET) allow us to put a static/shared method inside an interface?

查看:219
本文介绍了不是C#(或.NET)为什么应该允许我们把一个静态/共享方法的接口里面呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么不C#(或.NET)允许我们把一个静态/共享方法的接口里面呢?



从貌似的此处。但我的想法是有点不同的,我只想把一个帮手我的插件(接口)



应该不是C#至少可以让这种想法?

 命名空间MycComponent 
{

公共接口ITaskPlugin:ITaskInfo
{
字符串描述{搞定; }
串MenuTree {搞定; }
串MenuCaption {搞定; }

无效ShowTask(表格parentForm);
无效ShowTask(表格parentForm,字典<字符串对象> pkColumns);

ShowTaskNewDelegate ShowTaskNew {集;得到; }
ShowTaskOpenDelegate ShowTaskOpen {集;得到; }

//将不能与这编译:
公共静态字典<字符串,ITaskPlugin> GetPlugins(字符串目录)
{

变种L =新词典与LT;字符串,ITaskPlugin>();

的foreach(在Directory.GetFiles字符串文件(目录))
{
变种的fileInfo =新的FileInfo(文件);
如果(fileInfo.Extension.Equals(DLL))
{
ASM大会= Assembly.LoadFile(文件);
的foreach(在asm.GetTypes类型asmType())
{

如果(asmType.GetInterface(MycComponent.ITaskPlugin)!= NULL)
$ { b $ b VAR插件=(ITaskPlugin)Activator.CreateInstance(asmType);
l.Add(plugIn.TaskName,插件);
}

}


}
}

返回升;
} // GetPlugins。内部接口
不会编译}



/ *由于上述错误的,我不得不
将辅助方法,在一个新的类。有点大材小用时,方法应该
紧密耦合到它正在实施* /
公共静态类ITaskPluginHelper
{
公共静态字典<字符串,ITaskPlugin> GetPlugins(字符串目录)
{

变种L =新词典与LT;字符串,ITaskPlugin>();

的foreach(在Directory.GetFiles字符串文件(目录))
{
变种的fileInfo =新的FileInfo(文件);
如果(fileInfo.Extension.Equals(DLL))
{
ASM大会= Assembly.LoadFile(文件);
的foreach(在asm.GetTypes类型asmType())
{

如果(asmType.GetInterface(MycComponent.ITaskPlugin)!= NULL)
$ { b $ b VAR插件=(ITaskPlugin)Activator.CreateInstance(asmType);
l.Add(plugIn.TaskName,插件);
}

}


}
}

返回升;
} // GetPlugins
} // ITaskPluginHelper
}


解决方案

接口的想法是代表一个合同,而不是实现。



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



我可以看到你点 - 它有时是有用知道什么辅助方法,它们有一个接​​口连接(和扩展方法尤其重要有),但我个人希望把那些在一个单独的类,无论如何,只是为了保持心智模式的清洁<。 / p>

Why shouldn't C#(or .NET) allow us to put a static/shared method inside an interface?

seemingly duplicate from here. but my idea is a bit different one, I just want to put a helper for my plugins(interface)

shouldn't C# at least allow this idea?

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
}

解决方案

The idea of an interface is to represent a contract, not implementation.

I can't remember offhand whether IL actually does allow static methods with implementations in interfaces - I've a sneaky suspicion that it does - but that muddies the concept somewhat.

I can see your point - it's sometimes useful to know what helper methods are available which are connected with an interface (and extension methods are particularly relevant there) but I would personally want to put those in a separate class anyway, just to keep the mental model clean.

这篇关于不是C#(或.NET)为什么应该允许我们把一个静态/共享方法的接口里面呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆