/// <summary> /// Init the factory by loading every dll and every instances found in the config file /// It fills the devicesDictionnary with an instance of each type. /// Must be called once at the start of the application /// </summary> /// <exception cref="System.ArgumentException"> Thrown when the package of instance is incorrect </exception> /// <exception cref="System.ArgumentNullException"> Thrown when the instance name or the package is null </exception> /// <exception cref="System.IO.FileNotFoundException"> Thrown when the library is not found </exception> /// <exception cref="System.IO.FileLoadException"> Thrown when the System.Reflection don't manage to load correctly the library </exception> /// <exception cref="System.BadImageFormatException"> Thrown when library doesn't match a understandable format </exception> /// <exception cref="System.NotSupportedException"> Thrown when the type of the creatd instance is not supported</exception> /// <exception cref="System.Reflection.TargetInvocationException"> Thrown when the constructor of the instance threw an exception </exception> /// <exception cref="System.MethodAccessException"> Thrown when the contructor of the instance is private </exception> /// <exception cref="System.MemberAccessException"> Thrown when a member of the instance is not accessible </exception> /// <exception cref="System.Runtime.InteropServices.InvalidComObjectException"> Thrown when an invalid COM object is used</exception> /// <exception cref="System.Runtime.InteropServices.COMException"> Thrown when an HRESULT is returned from a COM Method call </exception> /// <exception cref="System.MissingMethodException"> Thrown when a method is missing in the instance class </exception> /// <exception cref="System.TypeLoadException"> Thrown when the type cannot be load</exception> public static void Init() { devicesDictionnary = new Dictionary <string, IDevice>(); configReader = new XMLConfigReader(CONFIGURATION_FILE_PATH); ArrayList listOfEveryDllByName = configReader.GetAllDllName(); //Browsing through each library in configuration file foreach (string aLibraryName in listOfEveryDllByName) { Assembly assembly = null; string absolutePath = Regex.Replace(PROJECT_PATH, "\\\\", "/"); //Loading the current .dll assembly = Assembly.LoadFrom(absolutePath + aLibraryName + DLL_EXTENSION); //Load all the dependencies of the current assembly foreach (AssemblyName assemblyName in assembly.GetReferencedAssemblies()) { try { //effective loading Assembly.Load(assemblyName); } catch (Exception ex) { if (ex is FileNotFoundException || ex is ArgumentNullException) { Console.WriteLine("Missing .dll file : " + assemblyName); } if (ex is FileLoadException) { Console.WriteLine("Couldn't load the .dll file : " + assemblyName); } if (ex is BadImageFormatException) { Console.WriteLine("invalid .dll file : " + assemblyName); } Console.WriteLine(ex.Message); } } // Getting all the peripheral types from the current assembly ArrayList instances = configReader.GetAllInstancesFromOneDll(aLibraryName); // Browsing through each instance node foreach (string instanceName in instances) { string[] parsedPath = aLibraryName.Split("/"); string packageOfInstance = parsedPath[parsedPath.Length - 1]; // Getting the constructor parameters object[] objectParameters = configReader.GetParametersForOneInstance(aLibraryName, instanceName); Type typeOfInstance = null; try { // Getting the type of the peripheral to create typeOfInstance = assembly.GetType(packageOfInstance + "." + instanceName); // Creating the peripheral with right constructor parameters // Build so as to the instance is an IDevice var instance = Activator.CreateInstance(typeOfInstance, objectParameters) as IDevice; // Adding the event handler to the new instance instance.eventHandler = PeripheralEventHandlerProxy.GetInstance(); // Adding the instance to the dictionnary devicesDictionnary.Add(instanceName, instance); } catch (ArgumentNullException e) { Console.WriteLine("Null argument in " + instanceName + " handling : or " + packageOfInstance + " handling"); Console.WriteLine(e.Message); } catch (ArgumentException e) { Console.WriteLine("Incorrect argument : " + packageOfInstance + "." + instanceName); Console.WriteLine(e.Message); } catch (FileNotFoundException e) { Console.WriteLine("Couldn't find .dll file : " + assembly); Console.WriteLine(e.Message); } catch (FileLoadException e) { Console.WriteLine("Couldn't load .dll file : " + assembly); Console.WriteLine(e.Message); } catch (BadImageFormatException e) { Console.WriteLine("invalid .dll file : " + assembly); Console.WriteLine(e.Message); } catch (NotSupportedException e) { Console.WriteLine("Type " + typeOfInstance + " isn't handled correctly"); Console.WriteLine(e.Message); } catch (TargetInvocationException e) { Console.WriteLine("Invalid target on instance creation : " + typeOfInstance + ": the invoked constructor threw an exception."); Console.WriteLine(e.Message); } catch (MissingMethodException e) { Console.WriteLine("Constructor of : " + typeOfInstance + " isn't defined with those parameters"); Console.WriteLine(e.Message); } catch (MethodAccessException e) { Console.WriteLine("Constructor of +" + typeOfInstance + " is private"); Console.WriteLine(e.Message); } catch (MemberAccessException e) { Console.WriteLine("Couldn't access to member of class : " + typeOfInstance); Console.WriteLine(e.Message); } catch (InvalidComObjectException e) { Console.WriteLine("Object " + typeOfInstance + " isn't used properly"); Console.WriteLine(e.Message); } catch (COMException e) { Console.WriteLine("Object " + typeOfInstance + " isn't used properly"); Console.WriteLine(e.Message); } catch (TypeLoadException e) { Console.WriteLine("couldn't load type : " + typeOfInstance); Console.WriteLine(e.Message); } catch (Exception ex) { Console.WriteLine("Unhandled exception type" + ex.GetType()); Console.WriteLine(ex.Message); } } } }