CreateInstance() public method

public CreateInstance ( ) : T
return T
Beispiel #1
0
        /// <summary>
        ///		Loads a plugin of the given class name from the given assembly, and calls Initialize() on it.
        ///		This function does NOT add the plugin to the PluginManager's
        ///		list of plugins.
        /// </summary>
        /// <returns>The loaded plugin.</returns>
        private static IPlugin LoadPlugin(ObjectCreator creator)
        {
            try
            {
                // Avoid duplicates of plugins of the same type.
                if (_plugins.Count > 0)
                {
                    var byTypePlugins = from p in _plugins
                                        where p.GetType() == creator.CreatedType
                                        select p;

                    if (byTypePlugins.Count() > 0)
                    {
                        LogManager.Instance.Write("{0} already loaded.", creator.GetAssemblyTitle());
                        return(null);
                    }
                }

                // create and start the plugin
                var plugin = creator.CreateInstance <IPlugin>();

                if (plugin == null)
                {
                    LogManager.Instance.Write("Failed to load plugin: {0}", creator.GetAssemblyTitle());
                    return(null);
                }

                plugin.Initialize();

                LogManager.Instance.Write("Loaded plugin: {0}", creator.GetAssemblyTitle());

                return(plugin);
            }
            catch (Exception ex)
            {
                LogManager.Instance.Write(LogManager.BuildExceptionString(ex));
            }

            return(null);
        }