Example #1
0
        /// <summary>
        /// Lädt und registriert das Plugin aus einem Assembly
        /// </summary>
        /// <param name="assembly">Das Assembly, indem sich das Plugins befindet</param>
        public static void Register(Assembly assembly)
        {
            try
            {
                foreach (var type in assembly.GetExportedTypes())
                {
                    if (type.IsClass && type.IsSealed && type.GetInterface(typeof(IPlugin).Name) != null && type.Name.Equals("Plugin"))
                    {
                        var id          = type.Name?.ToLower();
                        var name        = type.Assembly.GetCustomAttribute <AssemblyTitleAttribute>()?.Title;
                        var icon        = string.Empty;
                        var description = type.Assembly.GetCustomAttribute <AssemblyDescriptionAttribute>()?.Description;

                        foreach (var customAttribute in type.CustomAttributes.Where(x => x.AttributeType.GetInterfaces().Contains(typeof(IPluginAttribute))))
                        {
                            if (customAttribute.AttributeType == typeof(IDAttribute))
                            {
                                id = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString().ToLower();
                            }

                            if (customAttribute.AttributeType == typeof(NameAttribute))
                            {
                                name = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString();
                            }

                            if (customAttribute.AttributeType == typeof(IconAttribute))
                            {
                                icon = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString();
                            }

                            if (customAttribute.AttributeType == typeof(DescriptionAttribute))
                            {
                                description = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString();
                            }
                        }

                        var context = new PluginContext()
                        {
                            Assembly     = type.Assembly,
                            PluginID     = id,
                            PluginName   = name,
                            Manufacturer = type.Assembly.GetCustomAttribute <AssemblyCompanyAttribute>()?.Company,
                            Copyright    = type.Assembly.GetCustomAttribute <AssemblyCopyrightAttribute>()?.Copyright,
                            Icon         = UriRelative.Combine(Context.ContextPath, icon),
                            Description  = description,
                            Version      = type.Assembly.GetCustomAttribute <AssemblyInformationalVersionAttribute>()?.InformationalVersion,
                            Host         = Context,
                            Log          = Context.Log
                        };

                        var plugin = (IPlugin)type.Assembly.CreateInstance(type.FullName);

                        if (!Dictionary.ContainsKey(id))
                        {
                            Dictionary.Add(id, new PluginItem()
                            {
                                Context = context,
                                Plugin  = plugin
                            });

                            Context.Log.Info(message: I18N("webexpress:pluginmanager.created"), args: id);
                        }
                        else
                        {
                            Context.Log.Warning(message: I18N("webexpress:pluginmanager.duplicate"), args: id);
                        }
                    }
                    //else
                    //{
                    //    Context.Log.Warning(message: I18N("webexpress:pluginmanager.notfound"), args: assembly.FullName);
                    //}
                }
            }
            catch
            {
            }
        }
        /// <summary>
        /// Fügt Modul-Einträge aus dem angegebenen Plugin hinzu
        /// </summary>
        /// <param name="pluginContext">Der Kontext des zugehörigen Plugins</param>
        public static void Register(IPluginContext pluginContext)
        {
            var assembly = pluginContext.Assembly;

            foreach (var type in assembly.GetExportedTypes().Where(x => x.IsClass && x.IsSealed && x.GetInterface(typeof(IModule).Name) != null))
            {
                var id            = type.Name?.ToLower();
                var name          = type.Name;
                var icon          = string.Empty;
                var description   = string.Empty;
                var contextPath   = string.Empty;
                var assetPath     = string.Empty;
                var applicationID = string.Empty;

                foreach (var customAttribute in type.CustomAttributes.Where(x => x.AttributeType.GetInterfaces().Contains(typeof(IModuleAttribute))))
                {
                    if (customAttribute.AttributeType == typeof(IDAttribute))
                    {
                        id = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString().ToLower();
                    }

                    if (customAttribute.AttributeType == typeof(NameAttribute))
                    {
                        name = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString();
                    }

                    if (customAttribute.AttributeType == typeof(IconAttribute))
                    {
                        icon = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString();
                    }

                    if (customAttribute.AttributeType == typeof(DescriptionAttribute))
                    {
                        description = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString();
                    }

                    if (customAttribute.AttributeType == typeof(ContextPathAttribute))
                    {
                        contextPath = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString();
                    }

                    if (customAttribute.AttributeType == typeof(AssetPathAttribute))
                    {
                        assetPath = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString();
                    }

                    if (customAttribute.AttributeType == typeof(ApplicationAttribute))
                    {
                        applicationID = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString().ToLower();
                    }
                }

                // Zugehörige Anwendung ermitteln.
                var application = ApplicationManager.GetApplcation(applicationID);
                if (string.IsNullOrWhiteSpace(applicationID))
                {
                    // Es wurde keine Anwendung angebgeben
                    Context.Log.Warning(message: I18N("webexpress:modulemanager.applicationless"), args: id);
                }
                else if (application == null)
                {
                    // Anwendung wurde nicht gefunden
                    Context.Log.Warning(message: I18N("webexpress:modulemanager.applicationnotfound"), args: new object[] { id, applicationID });
                }
                else if (application != null && application.ApplicationID.Equals(applicationID, StringComparison.OrdinalIgnoreCase))
                {
                    var cp = UriRelative.Combine(Context.ContextPath, application.ContextPath);

                    // Kontext erstellen
                    var context = new ModuleContext()
                    {
                        Assembly      = assembly,
                        PluginID      = pluginContext.PluginID,
                        ApplicationID = application.ApplicationID,
                        ModuleID      = id,
                        ModuleName    = name,
                        Description   = description,
                        Icon          = UriRelative.Combine(cp, contextPath, icon),
                        AssetPath     = Path.Combine(Context.AssetPath, assetPath),
                        ContextPath   = UriRelative.Combine(cp, contextPath),
                        Log           = Context.Log
                    };

                    // Modul erstellen
                    var module = (IModule)type.Assembly.CreateInstance(type.FullName);

                    if (!Dictionary.ContainsKey(id))
                    {
                        Dictionary.Add(id, new ModuleItem()
                        {
                            Context = context,
                            Module  = module
                        });

                        Context.Log.Info(message: I18N("webexpress:modulemanager.register"), args: new object[] { id, application.ApplicationID });
                    }
                    else
                    {
                        Context.Log.Warning(message: I18N("webexpress:modulemanager.duplicate"), args: new object[] { id, application.ApplicationID });
                    }
                }
            }
        }
        /// <summary>
        /// Fügt Anwendungs-Einträge aus dem angegebenen Plugin hinzu
        /// </summary>
        /// <param name="pluginContext">Der Kontext des zugehörigen Plugins</param>
        public static void Register(IPluginContext pluginContext)
        {
            var assembly = pluginContext.Assembly;

            foreach (var type in assembly.GetExportedTypes().Where(x => x.IsClass && x.IsSealed && x.GetInterface(typeof(IApplication).Name) != null))
            {
                var id          = type.Name?.ToLower();
                var name        = type.Name;
                var icon        = string.Empty;
                var description = string.Empty;
                var contextPath = string.Empty;
                var assetPath   = string.Empty;

                // Attribute ermitteln
                foreach (var customAttribute in type.CustomAttributes.Where(x => x.AttributeType.GetInterfaces().Contains(typeof(IApplicationAttribute))))
                {
                    if (customAttribute.AttributeType == typeof(IDAttribute))
                    {
                        id = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString().ToLower();
                    }

                    if (customAttribute.AttributeType == typeof(NameAttribute))
                    {
                        name = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString();
                    }

                    if (customAttribute.AttributeType == typeof(IconAttribute))
                    {
                        icon = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString();
                    }

                    if (customAttribute.AttributeType == typeof(DescriptionAttribute))
                    {
                        description = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString();
                    }

                    if (customAttribute.AttributeType == typeof(ContextPathAttribute))
                    {
                        contextPath = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString();
                    }

                    if (customAttribute.AttributeType == typeof(AssetPathAttribute))
                    {
                        assetPath = customAttribute.ConstructorArguments.FirstOrDefault().Value?.ToString();
                    }
                }

                // Anwendungskontext erstellen
                var context = new ApplicationContext()
                {
                    Assembly        = assembly,
                    PluginID        = pluginContext.PluginID,
                    ApplicationID   = id,
                    ApplicationName = name,
                    Description     = description,
                    Icon            = UriRelative.Combine(Context.ContextPath, contextPath, icon),
                    AssetPath       = Path.Combine(Context.AssetPath, assetPath),
                    ContextPath     = UriRelative.Combine(Context.ContextPath, contextPath),
                    Log             = Context.Log
                };

                // Anwendung erstellen
                var application = (IApplication)type.Assembly.CreateInstance(type.FullName);

                if (!Dictionary.ContainsKey(id))
                {
                    Dictionary.Add(id, new ApplicationItem()
                    {
                        Context     = context,
                        Application = application
                    });

                    Context.Log.Info(message: I18N("webexpress:applicationmanager.register"), args: id);
                }
                else
                {
                    Context.Log.Warning(message: I18N("webexpress:applicationmanager.duplicate"), args: id);
                }
            }
        }