2 // TypeLoader.cs: Loads types from a list of Assemblies
4 // Author: Jonathan Pryor (jonpryor@vt.edu)
6 // (C) 2002 Jonathan Pryor
10 using System.Collections;
12 using System.Diagnostics;
13 using System.Reflection;
15 using System.Text.RegularExpressions;
17 namespace Mono.TypeReflector
19 public class TypeLoader {
21 private static TraceSwitch info =
22 new TraceSwitch ("type-loader", "TypeLoader messages");
25 private ICollection assemblies = null;
27 private bool matchFullName = true;
28 private bool matchName = false;
29 private bool matchBase = false;
30 private bool matchMethodReturnType = false;
31 private bool matchNamespace = false;
33 public bool MatchFullName {
34 get {return matchFullName;}
35 set {matchFullName = value;}
38 public bool MatchClassName {
39 get {return matchName;}
40 set {matchName = value;}
43 public bool MatchBase {
44 get {return matchBase;}
45 set {matchBase = value;}
48 public bool MatchMethodReturnType {
49 get {return matchMethodReturnType;}
50 set {matchMethodReturnType = value;}
53 public bool MatchNamespace {
54 get {return matchNamespace;}
55 set {matchNamespace = value;}
62 public TypeLoader (ICollection assemblies)
64 this.assemblies = assemblies;
67 public ICollection Assemblies {
68 get {return assemblies;}
69 set {assemblies = value;}
72 public ICollection LoadTypes (IList match)
74 if (assemblies == null)
75 throw new ArgumentNullException ("Assemblies");
76 if (match == null || match.Count == 0)
77 throw new ArgumentNullException ("match");
79 StringBuilder regex = new StringBuilder ();
80 regex.Append (match[0]);
81 for (int i = 1; i < match.Count; ++i)
82 regex.AppendFormat ("|{0}", match[i]);
84 Regex re = new Regex (regex.ToString());
86 Trace.WriteLineIf (info.TraceInfo,
87 string.Format ("using regex: '{0}'", regex.ToString()));
89 IList found = new ArrayList ();
91 foreach (string a in assemblies) {
92 LoadMatchingTypesFrom (a, regex.ToString(), re, found);
98 private void LoadMatchingTypesFrom (string where, string regex, Regex re, IList types)
101 Assembly a = Assembly.LoadFrom (where);
102 Type[] _types = a.GetTypes();
103 foreach (Type t in _types) {
107 } catch (Exception e) {
108 Trace.WriteLineIf (info.TraceError, String.Format (
109 "Unable to load type regex `{0}' from `{1}'.",
111 Trace.WriteLineIf (info.TraceError, e.ToString());
115 private bool Matches (Regex r, Type t)
118 f = c = b = rt = n = false;
120 f = r.Match(t.FullName).Success;
121 else if (MatchClassName)
122 c = r.Match(t.Name).Success;
123 else if (MatchNamespace)
124 n = r.Match(t.Namespace).Success;
126 b = (!MatchFullName ? false : r.Match (t.BaseType.FullName).Success) ||
127 (!MatchClassName ? false : r.Match (t.BaseType.Name).Success) ||
128 (!MatchNamespace ? false : r.Match (t.BaseType.Namespace).Success);
130 // TODO: MatchMethodReturnType
131 Trace.WriteLineIf (info.TraceInfo, String.Format("TypeLoader.Matches: c={0}, b={1}, rt={2}, n={3}", c, b, rt, n));
132 return f || c || b || rt || n;