/// <summary>
        /// Searches for extensions in a particular file and tries to load them.
        /// </summary>
        /// <param name="file">The file to load.</param>
        /// <param name="extension">The extension that the file contains</param>
        /// <returns>Returns an array of loading results, indicating which extension loaded succesfully and which didn't.</returns>
        public ExtensionLoadResult[] LoadExtensions(ExtensionLibraryData file)
        {
            try
            {
                if (LoadedExtensions.FirstOrDefault(x => x.GetType().Assembly.Location.Equals(file.GetAbsolutePath(), StringComparison.OrdinalIgnoreCase)) != null)
                {
                    throw new InvalidOperationException("The extension library is already loaded.");
                }

                var extensionTypes = SearchExtensionTypes(file);

                if (extensionTypes.Length == 0)
                {
                    throw new BadImageFormatException(string.Format("No extension classes found in file {0}", file));
                }

                var results = new ExtensionLoadResult[extensionTypes.Length];
                for (int i = 0; i < extensionTypes.Length; i++)
                {
                    results[i] = LoadExtension(extensionTypes[i]);
                }
                return(results);
            }
            catch (Exception ex)
            {
                return(new ExtensionLoadResult[] { new ExtensionLoadResult(file.GetAbsolutePath(), ex) });
            }
        }
        /// <summary>
        /// Adds all assemblies in a folder to LoadedExtensions.
        /// </summary>
        /// <param name="folderPath">Full path of folder to load from</param>
        public static void AddAllAssembliesInPath(string folderPath)
        {
            DirectoryInfo di = new DirectoryInfo(folderPath);

            FileInfo[] files = di.GetFiles("*", SearchOption.TopDirectoryOnly);

            foreach (FileInfo file in files)
            {
                try
                {
                    Assembly assem = Assembly.LoadFile(file.FullName);
                    LoadedExtensions.Add(assem);
                }
                catch (FileLoadException)
                {
                    continue;
                }
                catch (FileNotFoundException)
                {
                    continue;
                }
                catch (BadImageFormatException)
                {
                    continue;
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Load all Plugins
        /// </summary>
        public void LoadExtensions()
        {
            List <Assembly> assemblies   = new List <Assembly>();
            var             tempAssembly = Assembly.GetEntryAssembly();

            if (tempAssembly == null)
            {
                tempAssembly = Assembly.GetAssembly(GetType());
            }

            DirectoryInfo dir = new DirectoryInfo(Path.GetDirectoryName(tempAssembly.Location));

            assemblies.AddRange(LoadAssemblies(dir));

            DirectoryInfo plugins = new DirectoryInfo(Path.Combine(dir.FullName, "plugins"));

            if (plugins.Exists)
            {
                assemblies.AddRange(LoadAssemblies(plugins));
            }

            var disabledExtensions = settings.KeyExists(SETTINGSKEY) ? settings.GetArray <string>(SETTINGSKEY) : new string[0];

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

            foreach (var assembly in assemblies)
            {
                foreach (var type in assembly.GetTypes())
                {
                    if (!typeof(IExtension).IsAssignableFrom(type))
                    {
                        continue;
                    }

                    try
                    {
                        IExtension extension = (IExtension)Activator.CreateInstance(type);
                        extension.Register(this);

                        if (disabledExtensions.Contains(type.FullName))
                        {
                            LoadedExtensions.Add(extension);
                        }
                        else
                        {
                            ActiveExtensions.Add(extension);
                        }
                    }
                    catch (Exception)
                    {
                        // TODO: Logging
                    }
                }
            }
        }
 /// <summary>
 /// Tries to create a LiteExtension instance of the specified type.
 /// </summary>
 /// <param name="extensionType">The type to create the instance from.</param>
 /// <returns>Returns an instance of an ExtensionLoadResult indicating whenever the extension is loaded or not.</returns>
 public ExtensionLoadResult LoadExtension(Type extensionType)
 {
     try
     {
         var extension = Activator.CreateInstance(extensionType) as LiteExtension;
         LoadedExtensions.Add(extension);
         extension.Initialize(_extensionHost);
         return(new ExtensionLoadResult(extensionType, extension));
     }
     catch (Exception ex)
     {
         return(new ExtensionLoadResult(extensionType, ex));
     }
 }
Beispiel #5
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);
        }
 public ExtensionLoadResult LoadExtension(Type extensionType)
 {
     try
     {
         var extension = Activator.CreateInstance(extensionType) as LiteExtension;
         LoadedExtensions.Add(extension);
         extension.Initialize(new ExtensionInitializationContext(LiteDevelopApplication.Current.IsInitialized ? InitializationTime.UserLoad : InitializationTime.Startup));
         return(new ExtensionLoadResult(extensionType, extension));
     }
     catch (Exception ex)
     {
         return(new ExtensionLoadResult(extensionType, ex));
     }
 }
 public T GetLoadedExtension <T>() where T : LiteExtension
 {
     return(LoadedExtensions.FirstOrDefault(x => x is T) as T);
 }
 public LiteExtension GetLoadedExtension(Type extensionType)
 {
     return(LoadedExtensions.FirstOrDefault(x => x.GetType() == extensionType));
 }
Beispiel #9
0
 private void LoadDefaultExtensions()
 {
     // Load the default extension
     LoadedExtensions.Add(new DefaultExtension());
 }