Пример #1
0
        public PluginContext LoadPlugin(string libPath)
        {
            if (!File.Exists(libPath))
            {
                throw new FileNotFoundException("File doesn't exist", libPath);
            }

            if (PluginContexts.Any(entry => Path.GetFullPath(entry.AssemblyPath) == Path.GetFullPath(libPath)))
            {
                throw new Exception("Plugin already loaded");
            }

            var asmData = File.ReadAllBytes(libPath);

            var pluginAssembly = Assembly.Load(asmData);
            var pluginContext  = new PluginContext(libPath, pluginAssembly);
            var initialized    = false;

            // search the entry point (the class that implements IPlugin)
            foreach (var pluginType in pluginAssembly.GetTypes().Where(pluginType => pluginType.IsPublic && !pluginType.IsAbstract).Where(pluginType => pluginType.HasInterface(typeof(IPlugin))))
            {
                if (initialized)
                {
                    throw new PluginLoadException("Found 2 classes that implements IPlugin. A plugin can contains only one");
                }

                pluginContext.Initialize(pluginType);
                initialized = true;

                RegisterPlugin(pluginContext);
            }

            return(pluginContext);
        }
        public PluginContext LoadPlugin(string libPath)
        {
            if (!File.Exists(libPath))
            {
                throw new FileNotFoundException("File doesn't exist", libPath);
            }
            if (this.PluginContexts.Any((PluginContext entry) => Path.GetFullPath(entry.AssemblyPath) == Path.GetFullPath(libPath)))
            {
                throw new Exception("Plugin already loaded");
            }
            byte[]        rawAssembly   = File.ReadAllBytes(libPath);
            Assembly      assembly      = Assembly.Load(rawAssembly);
            PluginContext pluginContext = new PluginContext(libPath, assembly);
            bool          flag          = false;

            foreach (Type current in
                     from pluginType in assembly.GetTypes()
                     where pluginType.IsPublic && !pluginType.IsAbstract
                     where pluginType.HasInterface(typeof(IPlugin))
                     select pluginType)
            {
                if (flag)
                {
                    throw new PluginLoadException("Found 2 classes that implements IPlugin. A plugin can contains only one");
                }
                pluginContext.Initialize(current);
                flag = true;
                this.RegisterPlugin(pluginContext);
            }
            return(pluginContext);
        }