public virtual T ConstructObject <T>(string name, object[] args = null) where T : class
        {
            args ??= new object[0];
            if (!string.IsNullOrWhiteSpace(name))
            {
                try
                {
                    if (PluginTypes.FirstOrDefault(t => t.FullName == name) is TypeInfo type)
                    {
                        var matchingConstructors = from ctor in type.GetConstructors()
                                                   let parameters = ctor.GetParameters()
                                                                    where parameters.Length == args.Length
                                                                    where IsValidParameterFor(args, parameters)
                                                                    select ctor;

                        if (matchingConstructors.FirstOrDefault() is ConstructorInfo constructor)
                        {
                            return((T)constructor.Invoke(args) ?? null);
                        }
                    }
                }
                catch
                {
                    Log.Write("Plugin", $"Unable to construct object '{name}'", LogLevel.Error);
                }
            }
            Log.Write("Plugin", $"No constructor found for '{name}'", LogLevel.Debug);
            return(null);
        }
        public virtual T ConstructObject <T>(string name, object[] args = null) where T : class
        {
            args ??= new object[0];
            if (!string.IsNullOrWhiteSpace(name))
            {
                try
                {
                    if (PluginTypes.FirstOrDefault(t => t.FullName == name) is TypeInfo type)
                    {
                        var matchingConstructors = from ctor in type.GetConstructors()
                                                   let parameters = ctor.GetParameters()
                                                                    where parameters.Length == args.Length
                                                                    where IsValidParameterFor(args, parameters)
                                                                    select ctor;

                        if (matchingConstructors.FirstOrDefault() is ConstructorInfo constructor)
                        {
                            T obj = (T)constructor.Invoke(args) ?? null;

                            if (obj != null)
                            {
                                Inject(obj, type);
                            }
                            return(obj);
                        }
                        else
                        {
                            Log.Write("Plugin", $"No constructor found for '{name}'", LogLevel.Error);
                        }
                    }
                }
                catch (TargetInvocationException e) when(e.Message == "Exception has been thrown by the target of an invocation.")
                {
                    Log.Write("Plugin", "Object construction has thrown an error", LogLevel.Error);
                    Log.Exception(e.InnerException);
                }
                catch (Exception e)
                {
                    Log.Write("Plugin", $"Unable to construct object '{name}'", LogLevel.Error);
                    Log.Exception(e);
                }
            }
            return(null);
        }
Exemple #3
0
        public void Load(string jsonConfiguration)
        {
            JsonConfiguration = jsonConfiguration;

            var plugins = JsonConvert.DeserializeObject <Dictionary <string, JObject> >(JsonConfiguration);

            foreach (var plugin in plugins)
            {
                var type = PluginTypes.FirstOrDefault(each => each.Name == plugin.Key + "Plugin");

                if (type == null)
                {
                    throw new Exception("Invalid plugin: " + plugin.Key);
                }

                var instance = plugin.Value.ToObject(type) as Plugin;

                Plugins.Add(instance);
            }
        }
        public static T ConstructObject <T>(string name, object[] args = null) where T : class
        {
            args ??= new object[0];
            if (!string.IsNullOrWhiteSpace(name))
            {
                try
                {
                    var type = PluginTypes.FirstOrDefault(t => t.FullName == name);
                    var matchingConstructors = from ctor in type?.GetConstructors()
                                               let parameters = ctor.GetParameters()
                                                                where parameters.Length == args.Length
                                                                where args.IsValidParameterFor(parameters)
                                                                select ctor;

                    var constructor = matchingConstructors.FirstOrDefault();
                    return((T)constructor?.Invoke(args) ?? null);
                }
                catch
                {
                    Log.Write("Plugin", $"Unable to construct object '{name}'", LogLevel.Error);
                }
            }
            return(null);
        }
Exemple #5
0
        private T findForFamily <T>(Type pluginType, Func <IPluginTypeConfiguration, T> func, T defaultValue)
        {
            var family = PluginTypes.FirstOrDefault(x => x.PluginType == pluginType);

            return(family == null ? defaultValue : func(family));
        }
Exemple #6
0
        public virtual T ConstructObject <T>(string name, object[] args = null) where T : class
        {
            args ??= new object[0];
            if (!string.IsNullOrWhiteSpace(name))
            {
                try
                {
                    if (PluginTypes.FirstOrDefault(t => t.FullName == name) is TypeInfo type)
                    {
                        var matchingConstructors = from ctor in type.GetConstructors()
                                                   let parameters = ctor.GetParameters()
                                                                    where parameters.Length == args.Length
                                                                    where IsValidParameterFor(args, parameters)
                                                                    select ctor;

                        if (matchingConstructors.FirstOrDefault() is ConstructorInfo constructor)
                        {
                            T obj = (T)constructor.Invoke(args) ?? null;

                            if (obj != null)
                            {
                                var resolvedProperties = from property in type.GetProperties()
                                                         where property.GetCustomAttribute <ResolvedAttribute>() is ResolvedAttribute
                                                         select property;

                                foreach (var property in resolvedProperties)
                                {
                                    var service = GetService(property.PropertyType);
                                    if (service != null)
                                    {
                                        property.SetValue(obj, service);
                                    }
                                }

                                var resolvedFields = from field in type.GetFields()
                                                     where field.GetCustomAttribute <ResolvedAttribute>() is ResolvedAttribute
                                                     select field;

                                foreach (var field in resolvedFields)
                                {
                                    var service = GetService(field.FieldType);
                                    if (service != null)
                                    {
                                        field.SetValue(obj, service);
                                    }
                                }
                            }
                            return(obj);
                        }
                        else
                        {
                            Log.Write("Plugin", $"No constructor found for '{name}'", LogLevel.Error);
                        }
                    }
                }
                catch (TargetInvocationException e) when(e.Message == "Exception has been thrown by the target of an invocation.")
                {
                    Log.Write("Plugin", "Object construction has thrown an error", LogLevel.Error);
                    Log.Exception(e.InnerException);
                }
                catch (Exception e)
                {
                    Log.Write("Plugin", $"Unable to construct object '{name}'", LogLevel.Error);
                    Log.Exception(e);
                }
            }
            return(null);
        }