~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/mono-addins/Mono.Addins/Mono.Addins.Database/DefaultAssemblyReflector.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// DefaultAssemblyReflector.cs
 
2
//
 
3
// Author:
 
4
//   Lluis Sanchez Gual <lluis@novell.com>
 
5
//
 
6
// Copyright (c) 2007 Novell, Inc (http://www.novell.com)
 
7
//
 
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:
 
14
//
 
15
// The above copyright notice and this permission notice shall be included in
 
16
// all copies or substantial portions of the Software.
 
17
//
 
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
 
24
// THE SOFTWARE.
 
25
//
 
26
//
 
27
 
 
28
using System;
 
29
using System.Reflection;
 
30
using System.Collections;
 
31
using System.Collections.Generic;
 
32
 
 
33
namespace Mono.Addins.Database
 
34
{
 
35
        class DefaultAssemblyReflector: IAssemblyReflector
 
36
        {
 
37
                public void Initialize (IAssemblyLocator locator)
 
38
                {
 
39
                }
 
40
 
 
41
                public object LoadAssembly (string file)
 
42
                {
 
43
                        return Util.LoadAssemblyForReflection (file);
 
44
                }
 
45
                
 
46
                public string[] GetResourceNames (object asm)
 
47
                {
 
48
                        return ((Assembly)asm).GetManifestResourceNames ();
 
49
                }
 
50
                
 
51
                public System.IO.Stream GetResourceStream (object asm, string resourceName)
 
52
                {
 
53
                        return ((Assembly)asm).GetManifestResourceStream (resourceName);
 
54
                }
 
55
 
 
56
                public object[] GetCustomAttributes (object obj, Type type, bool inherit)
 
57
                {
 
58
                        ICustomAttributeProvider aprov = obj as ICustomAttributeProvider;
 
59
                        if (aprov != null)
 
60
                                return aprov.GetCustomAttributes (type, inherit);
 
61
                        else
 
62
                                return new object [0];
 
63
                }
 
64
 
 
65
                public object GetCustomAttribute (object obj, Type type, bool inherit)
 
66
                {
 
67
                        foreach (object att in GetCustomAttributes (obj, type, inherit))
 
68
                                if (type.IsInstanceOfType (att))
 
69
                                        return att;
 
70
                        return null;
 
71
                }
 
72
                
 
73
                public List<CustomAttribute> GetRawCustomAttributes (object obj, Type type, bool inherit)
 
74
                {
 
75
                        ICustomAttributeProvider aprov = obj as ICustomAttributeProvider;
 
76
                        List<CustomAttribute> atts = new List<CustomAttribute> ();
 
77
                        if (aprov == null)
 
78
                                return atts;
 
79
                        
 
80
                        foreach (object at in aprov.GetCustomAttributes (type, inherit))
 
81
                                atts.Add (ConvertAttribute (at));
 
82
 
 
83
                        return atts;
 
84
                }
 
85
 
 
86
                CustomAttribute ConvertAttribute (object ob)
 
87
                {
 
88
                        CustomAttribute at = new CustomAttribute ();
 
89
                        Type type = ob.GetType ();
 
90
                        at.TypeName = type.FullName;
 
91
                        
 
92
                        foreach (PropertyInfo prop in type.GetProperties (BindingFlags.Public | BindingFlags.Instance)) {
 
93
                                object val = prop.GetValue (ob, null);
 
94
                                if (val != null) {
 
95
                                        NodeAttributeAttribute bt = (NodeAttributeAttribute) Attribute.GetCustomAttribute (prop, typeof(NodeAttributeAttribute), true);
 
96
                                        if (bt != null) {
 
97
                                                string name = string.IsNullOrEmpty (bt.Name) ? prop.Name : bt.Name;
 
98
                                                at [name] = Convert.ToString (val, System.Globalization.CultureInfo.InvariantCulture);
 
99
                                        }
 
100
                                }
 
101
                        }
 
102
                        foreach (FieldInfo field in type.GetFields (BindingFlags.Public | BindingFlags.Instance)) {
 
103
                                object val = field.GetValue (ob);
 
104
                                if (val != null) {
 
105
                                        NodeAttributeAttribute bt = (NodeAttributeAttribute) Attribute.GetCustomAttribute (field, typeof(NodeAttributeAttribute), true);
 
106
                                        if (bt != null) {
 
107
                                                string name = string.IsNullOrEmpty (bt.Name) ? field.Name : bt.Name;
 
108
                                                at [name] = Convert.ToString (val, System.Globalization.CultureInfo.InvariantCulture);
 
109
                                        }
 
110
                                }
 
111
                        }
 
112
                        return at;
 
113
                }
 
114
                
 
115
                public string GetTypeName (object type)
 
116
                {
 
117
                        return ((Type)type).Name;
 
118
                }
 
119
 
 
120
                public IEnumerable GetFields (object type)
 
121
                {
 
122
                        return ((Type)type).GetFields (BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
 
123
                }
 
124
 
 
125
                public string GetFieldName (object field)
 
126
                {
 
127
                        return ((FieldInfo)field).Name;
 
128
                }
 
129
 
 
130
                public string GetFieldTypeFullName (object field)
 
131
                {
 
132
                        return ((FieldInfo)field).FieldType.FullName;
 
133
                }
 
134
                
 
135
                public IEnumerable GetAssemblyTypes (object asm)
 
136
                {
 
137
                        return ((Assembly)asm).GetTypes ();
 
138
                }
 
139
 
 
140
                public IEnumerable GetBaseTypeFullNameList (object type)
 
141
                {
 
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;
 
147
                        }
 
148
                        foreach (Type iterf in ((Type)type).GetInterfaces ()) {
 
149
                                list.Add (iterf.FullName);
 
150
                        }
 
151
                        return list;
 
152
                }
 
153
 
 
154
                public object LoadAssemblyFromReference (object asmReference)
 
155
                {
 
156
                        return Assembly.Load ((AssemblyName)asmReference);
 
157
                }
 
158
 
 
159
                public IEnumerable GetAssemblyReferences (object asm)
 
160
                {
 
161
                        return ((Assembly)asm).GetReferencedAssemblies ();
 
162
                }
 
163
 
 
164
                public object GetType (object asm, string typeName)
 
165
                {
 
166
                        return ((Assembly)asm).GetType (typeName);
 
167
                }
 
168
 
 
169
                public string GetTypeFullName (object type)
 
170
                {
 
171
                        return ((Type)type).FullName;
 
172
                }
 
173
 
 
174
                public bool TypeIsAssignableFrom (object baseType, object type)
 
175
                {
 
176
                        return ((Type)baseType).IsAssignableFrom ((Type)type);
 
177
                }
 
178
 
 
179
                public string GetTypeAssemblyQualifiedName (object type)
 
180
                {
 
181
                        return ((Type)type).AssemblyQualifiedName;
 
182
                }
 
183
        }
 
184
}