BioCore  5.0.0
A Windows .NET microscopy library and application for viewing, editing, and annotating images.
Loading...
Searching...
No Matches
Library.cs
Go to the documentation of this file.
1using System.Reflection;
2
3namespace BioCore
4{
5 public partial class Library : Form
6 {
7 public static Lib dll = null;
8 public Library()
9 {
10 InitializeComponent();
11 }
12
20 private void importToolStripMenuItem_Click(object sender, EventArgs e)
21 {
22 if (openFileDialog.ShowDialog() != DialogResult.OK)
23 return;
24 dll = new Lib(openFileDialog.FileName);
25 typeBox.Items.AddRange(dll.Types.Values.ToArray());
26 foreach (var item in dll.Interfaces)
27 {
28 interBox.Items.Add(item);
29 }
30
31 }
32 }
33 /* It loads a .NET assembly and provides a dictionary of all the types, modules, constructors,
34 interfaces, enums, properties, fields, methods, and members */
35 public class Lib
36 {
37 public Assembly dll;
38 public Dictionary<string, TypeInfo> Types = new Dictionary<string, TypeInfo>();
39 public Dictionary<string, TypeInfo> Objects = new Dictionary<string, TypeInfo>();
40 public Dictionary<string, Module> Modules = new Dictionary<string, Module>();
41 public Dictionary<string, ConstructorInfo> Constructors = new Dictionary<string, ConstructorInfo>();
42 public Dictionary<string, InterfaceMapping> Interfaces = new Dictionary<string, InterfaceMapping>();
43 /* It's a wrapper for a type that allows you to get information about the type and invoke
44 methods on it */
45 public class TypeInfo
46 {
47 /* It's a way to categorize the members of a type. */
48 public enum Kind
49 {
51 Member,
52 Field,
53 Enum,
56 }
57 public Kind kind;
58 public Dictionary<string, Enum> Enums = new Dictionary<string, Enum>();
59 public Dictionary<string, Type> Interfaces = new Dictionary<string, Type>();
60 public Dictionary<string, ConstructorInfo> Constructors = new Dictionary<string, ConstructorInfo>();
61 public Dictionary<string, List<MemberInfo>> Members = new Dictionary<string, List<MemberInfo>>();
62 public Dictionary<string, FieldInfo> Fields = new Dictionary<string, FieldInfo>();
63 public Dictionary<string, PropertyInfo> Properties = new Dictionary<string, PropertyInfo>();
64 public Dictionary<string, List<MethodInfo>> Methods = new Dictionary<string, List<MethodInfo>>();
65 public Type type;
66 public string GUID;
71 public void AddMember(MemberInfo inf)
72 {
73 if (Members.ContainsKey(inf.ToString()))
74 {
75 Members[inf.ToString()].Add(inf);
76 }
77 else
78 {
79 List<MemberInfo> l = new List<MemberInfo>();
80 l.Add(inf);
81 Members.Add(inf.ToString(), l);
82 }
83 }
88 public void AddMethod(MethodInfo inf)
89 {
90 if (Methods.ContainsKey(inf.ToString()))
91 {
92 Methods[inf.ToString()].Add(inf);
93 }
94 else
95 {
96 List<MethodInfo> l = new List<MethodInfo>();
97 l.Add(inf);
98 Methods.Add(inf.ToString(), l);
99 }
100 }
104 public void AddField(FieldInfo inf)
105 {
106 Fields.Add(inf.ToString(), inf);
107 }
111 public void AddProperty(PropertyInfo inf)
112 {
113 Properties.Add(inf.ToString(), inf);
114 }
124 public object Invoke(string name, object o, object[] args)
125 {
126 return type.InvokeMember(name, BindingFlags.InvokeMethod, null, o, args);
127 }
134 public object GetProperty(string name, object obj)
135 {
136 PropertyInfo p = type.GetProperty(name);
137 return p.GetValue(obj);
138 }
142 public object Instance()
143 {
144 return Activator.CreateInstance(type);
145 }
146 }
147 /* The ObjectInfo class contains a TypeInfo object */
148 public class ObjectInfo
149 {
151
152 }
153 /* It loads a .NET assembly and provides a dictionary of all the types, modules, constructors,
154 interfaces, enums, properties, fields, methods, and members. */
155 public Lib(string file)
156 {
157 dll = Assembly.LoadFile(file);
158 Type[] tps = dll.GetExportedTypes();
159 foreach (Type type in tps)
160 {
161 TypeInfo t = new TypeInfo();
162 t.type = type;
163 string s = type.ToString();
164 s = s.Remove(0, s.LastIndexOf('.') + 1);
165 if (type.IsEnum)
166 {
167 GetEnums(t);
168 }
169 GetInterfaces(t);
170 GetConstructors(t);
171 GetProperties(t);
172 GetFields(t);
173 GetMethods(t);
174 GetMembers(t);
175 Types.Add(s, t);
176 }
177 Module[] mds = dll.GetModules();
178 foreach (Module m in mds)
179 {
180 string s = m.ToString();
181 s = s.Remove(0, s.LastIndexOf('.') + 1);
182 Modules.Add(s, m);
183 }
184 }
195 public object Invoke(Type type, string name, object o, object[] args)
196 {
197 return type.InvokeMember(name, BindingFlags.InvokeMethod, null, o, args);
198 }
209 public object Invoke(string st, string name, object o, object[] args)
210 {
211 Type t = Types[st].type;
212 return t.InvokeMember(name, BindingFlags.InvokeMethod, null, o, args);
213 }
221 public object GetProperty(string type, string name, object obj)
222 {
223 Type myType = Types[type].type;
224 PropertyInfo p = myType.GetProperty(name);
225 return p.GetValue(obj);
226 }
231 private void GetEnums(TypeInfo type)
232 {
233 Array ar = Enum.GetValues(type.type);
234 foreach (var item in ar)
235 {
236 if (!type.Enums.ContainsKey(item.ToString()))
237 type.Enums.Add(item.ToString(), (Enum)item);
238 }
239 }
245 private void GetInterfaces(TypeInfo type)
246 {
247 Type[] ts = type.type.GetInterfaces();
248 foreach (Type item in ts)
249 {
250 type.Interfaces.Add(item.ToString(), item);
251 }
252 }
256 private void GetConstructors(TypeInfo type)
257 {
258 ConstructorInfo[] ts = type.type.GetConstructors();
259 foreach (ConstructorInfo item in ts)
260 {
261 type.Constructors.Add(item.ToString(), item);
262 }
263 }
267 private void GetMembers(TypeInfo type)
268 {
269 MemberInfo[] ts = type.type.GetMembers();
270 foreach (MemberInfo item in ts)
271 {
272 type.AddMember(item);
273 }
274 }
278 private void GetProperties(TypeInfo type)
279 {
280 PropertyInfo[] ts = type.type.GetProperties();
281 foreach (PropertyInfo item in ts)
282 {
283 type.AddProperty(item);
284 }
285 }
289 private void GetFields(TypeInfo type)
290 {
291 FieldInfo[] ts = type.type.GetFields();
292 foreach (FieldInfo item in ts)
293 {
294 type.AddField(item);
295 }
296 }
300 private void GetMethods(TypeInfo type)
301 {
302 MethodInfo[] ts = type.type.GetMethods();
303 foreach (MethodInfo item in ts)
304 {
305 type.AddMethod(item);
306 }
307 }
308 }
309}
Dictionary< string, PropertyInfo > Properties
Definition Library.cs:63
Dictionary< string, Type > Interfaces
Definition Library.cs:59
void AddProperty(PropertyInfo inf)
Definition Library.cs:111
Dictionary< string, List< MethodInfo > > Methods
Definition Library.cs:64
Dictionary< string, FieldInfo > Fields
Definition Library.cs:62
Dictionary< string, ConstructorInfo > Constructors
Definition Library.cs:60
object Invoke(string name, object o, object[] args)
Definition Library.cs:124
void AddMember(MemberInfo inf)
Definition Library.cs:71
object GetProperty(string name, object obj)
Definition Library.cs:134
Dictionary< string, List< MemberInfo > > Members
Definition Library.cs:61
void AddMethod(MethodInfo inf)
Definition Library.cs:88
Dictionary< string, Enum > Enums
Definition Library.cs:58
void AddField(FieldInfo inf)
Definition Library.cs:104
Dictionary< string, Module > Modules
Definition Library.cs:40
Dictionary< string, TypeInfo > Objects
Definition Library.cs:39
Dictionary< string, ConstructorInfo > Constructors
Definition Library.cs:41
Dictionary< string, InterfaceMapping > Interfaces
Definition Library.cs:42
object Invoke(string st, string name, object o, object[] args)
Definition Library.cs:209
Assembly dll
Definition Library.cs:37
Dictionary< string, TypeInfo > Types
Definition Library.cs:38
object GetProperty(string type, string name, object obj)
Definition Library.cs:221
Lib(string file)
Definition Library.cs:155
object Invoke(Type type, string name, object o, object[] args)
Definition Library.cs:195
static Lib dll
Definition Library.cs:7