/src/manostool/Loader.cs

http://github.com/jacksonh/manos · C# · 93 lines · 54 code · 15 blank · 24 comment · 5 complexity · dcdd9b289c83fcb8b6f2a8804d2221a5 MD5 · raw file

  1. //
  2. // Copyright (C) 2010 Jackson Harper (jackson@manosdemono.com)
  3. // Copyright (C) 2011 Andrius Bentkus (andrius.bentkus@gmail.com)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining
  6. // a copy of this software and associated documentation files (the
  7. // "Software"), to deal in the Software without restriction, including
  8. // without limitation the rights to use, copy, modify, merge, publish,
  9. // distribute, sublicense, and/or sell copies of the Software, and to
  10. // permit persons to whom the Software is furnished to do so, subject to
  11. // the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be
  14. // included in all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  20. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  21. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  22. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. //
  24. //
  25. using System;
  26. using System.Linq;
  27. using System.Reflection;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using Manos;
  31. namespace Manos.Tool
  32. {
  33. public class Loader
  34. {
  35. public static T LoadLibrary<T> (string library, IList<string> arguments)
  36. {
  37. T app = default(T);
  38. Assembly a = Assembly.LoadFrom (library);
  39. foreach (Type t in a.GetTypes ()) {
  40. if (typeof(T).IsAssignableFrom(t)) {
  41. app = CreateAppInstance<T> (t, arguments);
  42. }
  43. }
  44. return app;
  45. }
  46. public static T CreateAppInstance<T> (Type t, IList<string> arguments)
  47. {
  48. int arg_count = arguments.Count;
  49. ConstructorInfo [] constructors = t.GetConstructors ();
  50. foreach (ConstructorInfo ci in constructors.Where (c => c.GetParameters ().Count () == arg_count)) {
  51. object [] args = ArgsForParams (ci.GetParameters (), arguments);
  52. if (args == null)
  53. continue;
  54. try {
  55. return (T) Activator.CreateInstance (t, args);
  56. } catch (Exception e) {
  57. Console.Error.WriteLine ("Exception creating App Type: '{0}'.", t);
  58. Console.Error.WriteLine (e);
  59. }
  60. }
  61. return default(T);
  62. }
  63. public static object [] ArgsForParams (ParameterInfo [] prms, IList<string> arguments)
  64. {
  65. object [] res = new object [prms.Length];
  66. for (int i = 0; i < prms.Count (); i++) {
  67. try {
  68. res [i] = Convert.ChangeType (arguments [i], prms [i].ParameterType);
  69. } catch (Exception e) {
  70. Console.Error.WriteLine ("Exception converting type: '{0}'.", prms [i].ParameterType);
  71. Console.Error.WriteLine (e);
  72. return null;
  73. }
  74. }
  75. return res;
  76. }
  77. }
  78. }