在C#中基于插件的应用 [英] Plugin based application in C#

查看:172
本文介绍了在C#中基于插件的应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须作出使用我所选择的语言的图形用户界面应用程序。该应用程序将在Windows XP上运行。这将是某种复杂的Windows窗体应用程序中。
我想,按照大多数建议,C#将是最好的使用。从这将是一个二进制文件的配置文件读取后
。关于GUI的左侧的树结构将填充。 (但最初我可以用一个简单的ASCII文件来测试我的代码工作)。该应用程序将通过该GUI接受来自用户的某些输入和将写回同一配置文件,并将反映在树结构或形式的变化的标签或任何其它有关领域

I have to make a graphical user interface application using the language of my choice. The application will run on Windows XP. It will be some sort of a complex windows form application. I think and as per most suggestions, C# will be the best to use. The tree structure on the left of the GUI will populate after reading from a configuration file which will be a binary file . (but initially I can work with a simple ASCII file to test my code.). The application will accept some inputs from the user through this GUI and will write the back to the same config file and will reflect the changes in the tree structure or the labels or any other pertaining field on the form.

将有3个选项卡,并为每个选项卡对应3配置文件。
我需要一些帮助,现在设计应用程序。我打算做一个主机应用程序(主要应用),并使用3选项卡控件插件。这是可行的?如果是的话可以请你指导我在此。我的意思是我怎么做3插件在C#中,我如何写的接口,以便在主应用程序知道哪些插件加载时加载它?会不会出现在我的项目文件夹中一个单独的插件文件夹?我希望你有我的观点,虽然这是太少的信息,为您开始。

There will be 3 tabs and 3 corresponding config files for each of the tabs. I need some help designing the application for now. I am planning to make a host application (main application) and use the 3 tab controls as plugins. Is this workable ? If so can you please guide me on this. I mean how do I make 3 plugins in C# and how do I write the interfaces so that the main application knows which plugin to load and when to load it ? Will there be a separate "Plugin" folder under my project folder ? I hope you got my point though this is too little of an information for you to begin with.

还有一些项目中已经存在的一些.cpp文件。一些.h文件以及这些文件包含了其中的一些重要的定义和常量。这些都需要与我的C#应用​​程序进行集成。我不知道该怎么做,但我相信,有可能通过编译在一个.dll在.cpp代码,然后编译的.dll暴露在我的C#应用​​程序。请让我知道如果你需要在顶层设计一些更多的信息。

Also there are some .cpp files already existing in the project. These files along with some .h files contain some important definitions and constants in them. These need to be integrated with my C# application. I have no clue how to do that but I am sure that it is possible by compiling the .cpp code in a .dll and then exposing the compiled .dll to my C# application. Please let me know if you need some more information for the top level design.

谢谢,
Viren

Thanks, Viren

推荐答案

要手动实现一个插件接口,你需要一个方法是这样的。我留下了一些待办事项中,在那里你会想增强错误处理和/或使具体执行多一点情况。

To implement a plugin interface manually, you will need a method something like this. I've left some TODOs in, where you would want to enhance the error handling and/or make the implementation a little more case specific.

    public List<T> LoadPlugin<T>(string directory)
    {
        Type interfaceType = typeof(T);
        List<T> implementations = new List<T>();

        //TODO: perform checks to ensure type is valid

        foreach (var file in System.IO.Directory.GetFiles(directory))
        {
            //TODO: add proper file handling here and limit files to check
            //try/catch added in place of ensure files are not .dll
            try
            {
                foreach (var type in System.Reflection.Assembly.LoadFile(file).GetTypes())
                {
                    if (interfaceType.IsAssignableFrom(type) && interfaceType != type)
                    { 
                        //found class that implements interface
                        //TODO: perform additional checks to ensure any
                        //requirements not specified in interface
                        //ex: ensure type is a class, check for default constructor, etc
                        T instance = (T)Activator.CreateInstance(type);
                        implementations.Add(instance);
                    }
                }
            }
            catch { }
        }

        return implementations;
    }



例如调用:

Example to call:

List<IPlugin> plugins = LoadPlugin<IPlugin>(path);



至于你的问题的C ++的一部分。还有,你可以处理这个几个不同的方法,但正确的选择取决于你的具体情况。你可以做一个符合CLR .dll文件在C ++中,你的C#项目可以引用,并呼吁像它引用任何其他.dll文件。此外,您可以使用的P / Invoke 调入本机.dll文件。

As for the c++ part of your question. There are few different ways you could approach this, though the correct choice depends on your specific situation. You can make a clr compliant .dll in c++, which your c# project could reference and call like any other .dll it references. Additionally, you could use P/Invoke to call into a native .dll.

这篇关于在C#中基于插件的应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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