Add --enable-deletion option to buildindex. If used, buildindex will remove deleted...
[beagle.git] / Util / ReflectionFu.cs
blob5d02354585c30f336b816ad39ec1a8174662517a
1 //
2 // ReflectionFu.cs
3 //
4 // Copyright (C) 2004-2006 Novell, Inc.
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a
9 // copy of this software and associated documentation files (the "Software"),
10 // to deal in the Software without restriction, including without limitation
11 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 // and/or sell copies of the Software, and to permit persons to whom the
13 // Software is furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 // DEALINGS IN THE SOFTWARE.
27 using System;
28 using System.Collections;
29 using System.IO;
30 using System.Reflection;
32 namespace Beagle.Util {
34 public class ReflectionFu {
36 public delegate void ForeachDirMethod (Assembly a);
38 static public ArrayList ScanEnvironmentForAssemblies (string env_var, string fallback_path)
40 return ScanEnvironmentForAssemblies (env_var, fallback_path, null);
43 static public ArrayList ScanEnvironmentForAssemblies (string env_var, string fallback_path, ForeachDirMethod method)
45 ArrayList assemblies = new ArrayList ();
47 string path = Environment.GetEnvironmentVariable (env_var);
49 if (path == null || path == "")
50 path = fallback_path;
51 else if (path [path.Length-1] == ':')
52 path += fallback_path;
54 Hashtable seen = new Hashtable ();
56 foreach (string dir in path.Split (':')) {
57 if (! seen.Contains (dir)) {
58 foreach (Assembly a in ScanDirectoryForAssemblies (dir)) {
59 assemblies.Add (a);
61 if (method != null)
62 method (a);
66 seen [dir] = true;
69 return assemblies;
72 static public ArrayList ScanDirectoryForAssemblies (string dir)
74 ArrayList assemblies = new ArrayList ();
76 if (dir == null || dir == "")
77 return assemblies;
79 if (! Directory.Exists (dir)) {
80 Logger.Log.Debug ("'{0}' is not a directory: Nothing loaded from here", dir);
81 return assemblies;
84 DirectoryInfo dir_info = new DirectoryInfo (dir);
85 foreach (FileInfo file_info in dir_info.GetFiles ()) {
86 if (file_info.Extension == ".dll") {
87 Assembly a = Assembly.LoadFrom (file_info.FullName);
88 assemblies.Add (a);
92 return assemblies;
95 static public ArrayList ScanAssemblyForClass (Assembly assembly, Type supertype)
97 ArrayList subclasses = new ArrayList ();
99 foreach (Type t in assembly.GetTypes ()) {
100 if (t.IsSubclassOf (supertype) && ! t.IsAbstract)
101 subclasses.Add (t);
104 return subclasses;
107 static public ArrayList ScanAssemblyForInterface (Assembly assembly, Type iface)
109 ArrayList implementors = new ArrayList ();
111 foreach (Type t in assembly.GetTypes ()) {
112 if (t.GetInterface (iface.ToString ()) != null)
113 implementors.Add (t);
116 return implementors;
119 static public ArrayList ScanTypeForAttribute (Type type, Type attribute_type)
121 ArrayList attrs = new ArrayList ();
123 object[] attributes = Attribute.GetCustomAttributes (type);
124 foreach (object obj in attributes) {
125 Type obj_type = obj.GetType ();
127 if (obj_type == attribute_type || obj_type.IsSubclassOf (attribute_type))
128 attrs.Add (obj);
131 return attrs;