PageRenderTime 83ms CodeModel.GetById 41ms app.highlight 4ms RepoModel.GetById 36ms app.codeStats 0ms

/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
26
27using System;
28using System.Linq;
29using System.Reflection;
30using System.Collections;
31using System.Collections.Generic;
32
33using Manos;
34
35namespace Manos.Tool
36{
37	public class Loader
38	{
39		public static T LoadLibrary<T> (string library, IList<string> arguments)
40		{
41			T app = default(T);
42			Assembly a = Assembly.LoadFrom (library);
43			
44			foreach (Type t in a.GetTypes ()) {
45				if (typeof(T).IsAssignableFrom(t)) {
46					app = CreateAppInstance<T> (t, arguments);
47				}
48			}
49
50			return app;
51		}
52		
53		public static T CreateAppInstance<T> (Type t, IList<string> arguments)
54		{
55			int arg_count = arguments.Count;
56			ConstructorInfo [] constructors = t.GetConstructors ();
57			
58			foreach (ConstructorInfo ci in constructors.Where (c => c.GetParameters ().Count () == arg_count)) {
59				object [] args = ArgsForParams (ci.GetParameters (), arguments);
60				if (args == null)
61					continue;
62				try {
63					return (T) Activator.CreateInstance (t, args);
64				} catch (Exception e) {
65					Console.Error.WriteLine ("Exception creating App Type: '{0}'.", t);
66					Console.Error.WriteLine (e);
67				}
68			}
69			
70			return default(T);
71		}
72		
73		public static object [] ArgsForParams (ParameterInfo [] prms, IList<string> arguments)
74		{
75			object [] res = new object [prms.Length];
76			
77			for (int i = 0; i < prms.Count (); i++) {
78				try {
79					res [i] = Convert.ChangeType (arguments [i], prms [i].ParameterType);
80				} catch (Exception e) {
81					Console.Error.WriteLine ("Exception converting type: '{0}'.", prms [i].ParameterType);
82					Console.Error.WriteLine (e);
83					
84					return null;
85				}
86			}
87			
88			return res;
89		}
90
91	}
92}
93