Beispiel #1
0
        public IEnumerable <IAnalogyExtension> GetExtensions()
        {
            if (LoadedExtensions.Any())
            {
                return(LoadedExtensions);
            }
            NameValueCollection appSettings = ConfigurationManager.AppSettings;
            List <string>       files       = appSettings["AssembliesToLoad"]
                                              .Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            string dir         = Environment.CurrentDirectory;
            Type   isExtension = typeof(IAnalogyExtension);

            List <Type> res = new List <Type>();

            files.AddRange(Directory.GetFiles(dir, "Analogy.*.Extension.dll"));
            foreach (var file in files)
            {
                var fileToload = Path.Combine(dir, file);
                if (!File.Exists(fileToload))
                {
                    Log.LogError("Analogy", $"{file} does not exist. Skipping");
                    continue;
                }
                try
                {
                    var assm = Assembly.LoadFrom(fileToload).GetTypes()
                               .Where(t => t.GetInterfaces().Any(i => i.Name.Equals(isExtension.Name))).ToList();
                    res.AddRange(assm);
                }
                catch (Exception ex)
                {
                    Log.LogError("Analogy", $"Error for:{file}: {ex.Message}");
                }
            }

            foreach (Type type in res)
            {
                try
                {
                    IAnalogyExtension control = (IAnalogyExtension)Activator.CreateInstance(type);
                    LoadedExtensions.Add(control);
                }
                catch (Exception exception)
                {
                    Log.LogError("Analogy", $"Error for:{type.Name}: {exception.Message}");
                }
            }

            return(LoadedExtensions);
        }