4 // Copyright (C) 2004-2006 Novell, Inc.
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.
28 using System
.Collections
;
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
== "")
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
)) {
72 static public ArrayList
ScanDirectoryForAssemblies (string dir
)
74 ArrayList assemblies
= new ArrayList ();
76 if (dir
== null || dir
== "")
79 if (! Directory
.Exists (dir
)) {
80 Logger
.Log
.Debug ("'{0}' is not a directory: Nothing loaded from here", dir
);
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
);
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
)
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
);
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
))