cvsimport
[beagle.git] / Util / ReflectionFu.cs
blob9d83483848d37be05ad3a9623d97de8b93cd7237
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 GetTypesFromAssemblyAttribute (Assembly assembly, Type attr_type)
97 ArrayList types = new ArrayList ();
99 TypeCacheAttribute attr = (TypeCacheAttribute) Attribute.GetCustomAttribute (assembly, attr_type);
101 if (attr != null)
102 types.AddRange (attr.Types);
104 return types;
107 static public ArrayList ScanTypeForAttribute (Type type, Type attribute_type)
109 ArrayList attrs = new ArrayList ();
111 object[] attributes = Attribute.GetCustomAttributes (type);
112 foreach (object obj in attributes) {
113 Type obj_type = obj.GetType ();
115 if (obj_type == attribute_type || obj_type.IsSubclassOf (attribute_type))
116 attrs.Add (obj);
119 return attrs;