2 // TypeLoader.cs: Loads types from a list of Assemblies
4 // Author: Jonathan Pryor (jonpryor@vt.edu)
6 // (C) 2002 Jonathan Pryor
8 // Permission is hereby granted, free of charge, to any
9 // person obtaining a copy of this software and associated
10 // documentation files (the "Software"), to deal in the
11 // Software without restriction, including without limitation
12 // the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software,
14 // and to permit persons to whom the Software is furnished to
15 // do so, subject to the following conditions:
17 // The above copyright notice and this permission notice
18 // shall be included in all copies or substantial portions
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
22 // KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
23 // THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
24 // PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
26 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
27 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections;
34 using System.Diagnostics;
35 using System.Reflection;
37 using System.Text.RegularExpressions;
39 namespace Mono.TypeReflector
41 public class TypeLoader {
44 private ICollection assemblies = null;
46 private bool matchFullName = true;
47 private bool matchName = false;
48 private bool matchBase = false;
49 private bool matchMethodReturnType = false;
50 private bool matchNamespace = false;
52 public bool MatchFullName {
53 get {return matchFullName;}
54 set {matchFullName = value;}
57 public bool MatchClassName {
58 get {return matchName;}
59 set {matchName = value;}
62 public bool MatchBase {
63 get {return matchBase;}
64 set {matchBase = value;}
67 public bool MatchMethodReturnType {
68 get {return matchMethodReturnType;}
69 set {matchMethodReturnType = value;}
72 public bool MatchNamespace {
73 get {return matchNamespace;}
74 set {matchNamespace = value;}
81 public TypeLoader (ICollection assemblies)
83 this.assemblies = assemblies;
86 public ICollection Assemblies {
87 get {return assemblies;}
88 set {assemblies = value;}
91 public ICollection LoadTypes (string match)
93 if (assemblies == null)
94 throw new ArgumentNullException ("Assemblies");
96 IList found = new ArrayList ();
98 foreach (string a in assemblies) {
99 LoadMatchingTypesFrom (a, match, found);
105 private void LoadMatchingTypesFrom (string where, string match, IList types)
107 Regex re = new Regex (match);
109 Assembly a = Assembly.LoadFrom (where);
110 Type[] _types = a.GetTypes();
111 foreach (Type t in _types) {
115 } catch (Exception e) {
116 Trace.WriteLine (String.Format (
117 "Unable to load type regex `{0}' from `{1}'.",
119 Trace.WriteLine (e.ToString());
123 private bool Matches (Regex r, Type t)
126 f = c = b = rt = n = false;
128 f = r.Match(t.FullName).Success;
130 c = r.Match(t.Name).Success;
132 n = r.Match(t.Namespace).Success;
134 b = (!MatchFullName ? false : r.Match (t.BaseType.FullName).Success) ||
135 (!MatchClassName ? false : r.Match (t.BaseType.Name).Success) ||
136 (!MatchNamespace ? false : r.Match (t.BaseType.Namespace).Success);
138 // TODO: MatchMethodReturnType
139 Trace.WriteLine (String.Format("TypeLoader.Matches: c={0}, b={1}, rt={2}, n={3}", c, b, rt, n));
140 return f || c || b || rt || n;