开发者

plugins in winform application

I have a function for loading all the dll in the repository

namespace MFDBAnalyser

{

    public class PluginManager
    {

        /// <summary>
        /// This function gets the name of the plugins and return that in a datatable dt.
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        static void Main(string[] args)

        {

            List<string> assemblyNames = new List<string>();
            Assembly[] oAssemblies = new Assembly[args.Length];

            for(int assemblyCount = 0;assemblyCount < args.Length;assemblyCount++)
            {
                oAssemblies[assemblyCount] = Assembly.LoadFile(args[assemblyCount]);

                try
                {
                    foreach(Type oType in oAssemblies[assemblyCount].GetTypes())
                    {
                        // Check whether class is inheriting from IMFServicePlugin.
                        if(oType.GetInterface("IMFDBAnalyserPlugin") == typeof(IMFDBAnalyserPlugin开发者_运维百科))
                        {
                            assemblyNames.Add(args[assemblyCount].Substring(args[assemblyCount].LastIndexOf("\\") + 1));
                        }
                    }
                }
                catch(Exception ex)
                {
                    EventLog log = new EventLog("Application");
                    log.Source = "MFPluggerService";
                    log.WriteEntry(ex.Message);
                }
            }

            // Passing data one application domain to another.
            AppDomain.CurrentDomain.SetData("AssemblyNames", assemblyNames.ToArray());
        }

    }
}

And an another interface class like this

public interface IMFDBAnalyserPlugin
{

    string RunAnalysis(string ConnectionString);

}

What I need is to return the string that I wrote in the RunAnalysis method

public class PrimaryKeyChecker : IMFDBAnalyserPlugin
{

    public string RunAnalysis(string ConnectionString)

    {
        return "Hello Srivastav!";
    }
}

But It has a Program.cs class as the main entry point for the application.

    static class Program
    {

    /// <summary>
    /// The main entry point for the application.
    /// </summary>

    [STAThread]

    static void Main()

        {

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MFDBAnalyser());

        }
    }
}

So it is showing the error that no two main can be there in one application. I need to get the methods fire from the pliginmanager.cs

I am very confused...Can u guys please have a look at my problem


You should not let your PluginManager class have a Main Method. That is what confuses the compiler.

Try renaming your PluginManager.Main method.

Furthermore, you cannot instantiate a namespace like when you do new MFDBAnalyser, try instantiating your class instead.

Hope this helps!


In short, delete Main() method that you don't use, and if you do use it, call it from the main Application Main() method.


You have mixed up class library with application code. I don't think you intended PluginManager to be the main running application.

Secondly, try IsAssignableFrom instead of GetInterface as GetInterface will return null when it cannot find the interface in the given type

foreach(Type oType in oAssemblies[assemblyCount].GetTypes())
{
    if(typeof(IMFDBAnalyserPlugin).IsAssignableFrom(oType)) {
        assemblyNames.Add(args[assemblyCount].Substring(args[assemblyCount].LastIndexOf("\\") + 1));
    }
}

And most probably as your xml comment says, if your intention is to return the list of plugins you would need to return the assemblyNames list at the end of the function.

Alternatively, you would be better of using the Managed Extensibility Framework instead of building your own plugin framework.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜