1
// DefaultAssemblyReflector.cs
4
// Lluis Sanchez Gual <lluis@novell.com>
6
// Copyright (c) 2007 Novell, Inc (http://www.novell.com)
8
// Permission is hereby granted, free of charge, to any person obtaining a copy
9
// of this software and associated documentation files (the "Software"), to deal
10
// in the Software without restriction, including without limitation the rights
11
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
// copies of the Software, and to permit persons to whom the Software is
13
// 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 FROM,
23
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
29
using System.Reflection;
30
using System.Collections;
31
using System.Collections.Generic;
33
namespace Mono.Addins.Database
35
class DefaultAssemblyReflector: IAssemblyReflector
37
public void Initialize (IAssemblyLocator locator)
41
public object LoadAssembly (string file)
43
return Util.LoadAssemblyForReflection (file);
46
public string[] GetResourceNames (object asm)
48
return ((Assembly)asm).GetManifestResourceNames ();
51
public System.IO.Stream GetResourceStream (object asm, string resourceName)
53
return ((Assembly)asm).GetManifestResourceStream (resourceName);
56
public object[] GetCustomAttributes (object obj, Type type, bool inherit)
58
ICustomAttributeProvider aprov = obj as ICustomAttributeProvider;
60
return aprov.GetCustomAttributes (type, inherit);
62
return new object [0];
65
public object GetCustomAttribute (object obj, Type type, bool inherit)
67
foreach (object att in GetCustomAttributes (obj, type, inherit))
68
if (type.IsInstanceOfType (att))
73
public List<CustomAttribute> GetRawCustomAttributes (object obj, Type type, bool inherit)
75
ICustomAttributeProvider aprov = obj as ICustomAttributeProvider;
76
List<CustomAttribute> atts = new List<CustomAttribute> ();
80
foreach (object at in aprov.GetCustomAttributes (type, inherit))
81
atts.Add (ConvertAttribute (at));
86
CustomAttribute ConvertAttribute (object ob)
88
CustomAttribute at = new CustomAttribute ();
89
Type type = ob.GetType ();
90
at.TypeName = type.FullName;
92
foreach (PropertyInfo prop in type.GetProperties (BindingFlags.Public | BindingFlags.Instance)) {
93
object val = prop.GetValue (ob, null);
95
NodeAttributeAttribute bt = (NodeAttributeAttribute) Attribute.GetCustomAttribute (prop, typeof(NodeAttributeAttribute), true);
97
string name = string.IsNullOrEmpty (bt.Name) ? prop.Name : bt.Name;
98
at [name] = Convert.ToString (val, System.Globalization.CultureInfo.InvariantCulture);
102
foreach (FieldInfo field in type.GetFields (BindingFlags.Public | BindingFlags.Instance)) {
103
object val = field.GetValue (ob);
105
NodeAttributeAttribute bt = (NodeAttributeAttribute) Attribute.GetCustomAttribute (field, typeof(NodeAttributeAttribute), true);
107
string name = string.IsNullOrEmpty (bt.Name) ? field.Name : bt.Name;
108
at [name] = Convert.ToString (val, System.Globalization.CultureInfo.InvariantCulture);
115
public string GetTypeName (object type)
117
return ((Type)type).Name;
120
public IEnumerable GetFields (object type)
122
return ((Type)type).GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
125
public string GetFieldName (object field)
127
return ((FieldInfo)field).Name;
130
public string GetFieldTypeFullName (object field)
132
return ((FieldInfo)field).FieldType.FullName;
135
public IEnumerable GetAssemblyTypes (object asm)
137
return ((Assembly)asm).GetTypes ();
140
public IEnumerable GetBaseTypeFullNameList (object type)
142
ArrayList list = new ArrayList ();
143
Type btype = ((Type)type).BaseType;
144
while (btype != typeof(object)) {
145
list.Add (btype.FullName);
146
btype = btype.BaseType;
148
foreach (Type iterf in ((Type)type).GetInterfaces ()) {
149
list.Add (iterf.FullName);
154
public object LoadAssemblyFromReference (object asmReference)
156
return Assembly.Load ((AssemblyName)asmReference);
159
public IEnumerable GetAssemblyReferences (object asm)
161
return ((Assembly)asm).GetReferencedAssemblies ();
164
public object GetType (object asm, string typeName)
166
return ((Assembly)asm).GetType (typeName);
169
public string GetTypeFullName (object type)
171
return ((Type)type).FullName;
174
public bool TypeIsAssignableFrom (object baseType, object type)
176
return ((Type)baseType).IsAssignableFrom ((Type)type);
179
public string GetTypeAssemblyQualifiedName (object type)
181
return ((Type)type).AssemblyQualifiedName;