BioImager  5.0.0
A .NET microscopy imaging application based on Bio library. Supports various microscopes by using imported libraries & GUI automation. Supports XInput game controllers to move stage, take images, run ImageJ macros on images or Bio C# scripts.
Loading...
Searching...
No Matches
Library.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Drawing;
6using System.Linq;
7using System.Text;
8using System.Threading.Tasks;
9using System.Windows.Forms;
10using System.Reflection;
11
12namespace BioImager
13{
14 public partial class Library : Form
15 {
16 public static Lib dll = null;
17 public Library()
18 {
19 InitializeComponent();
20 }
21
29 private void importToolStripMenuItem_Click(object sender, EventArgs e)
30 {
31 if (openFileDialog.ShowDialog() != DialogResult.OK)
32 return;
33 dll = new Lib(openFileDialog.FileName);
34 typeBox.Items.AddRange(dll.Types.Values.ToArray());
35
36 }
37
43 private void typeBox_SelectedIndexChanged(object sender, EventArgs e)
44 {
45 Lib.TypeInfo item = (Lib.TypeInfo)typeBox.SelectedItem;
46 interBox.Items.Clear();
47 interBox.Items.AddRange(item.Interfaces.Values.ToArray());
48 methodsBox.Items.Clear();
49 foreach (List<MethodInfo> me in item.Methods.Values)
50 {
51 foreach (MethodInfo meitem in me)
52 {
53 methodsBox.Items.Add(meitem);
54 }
55
56 }
57 enumsBox.Items.Clear();
58 enumsBox.Items.AddRange(item.Enums.Values.ToArray());
59 }
60 }
61 public class Lib
62 {
63 public Assembly dll;
64 public Dictionary<string, TypeInfo> Types = new Dictionary<string, TypeInfo>();
65 public Dictionary<string, TypeInfo> Objects = new Dictionary<string, TypeInfo>();
66 public Dictionary<string, Module> Modules = new Dictionary<string, Module>();
67 public Dictionary<string, ConstructorInfo> Constructors = new Dictionary<string, ConstructorInfo>();
68 public Dictionary<string, InterfaceMapping> Interfaces = new Dictionary<string, InterfaceMapping>();
69 /* It's a wrapper for a type that contains all the information about the type */
70 public class TypeInfo
71 {
72 public enum Kind
73 {
75 Member,
76 Field,
77 Enum,
80 }
81 public Kind kind;
82 public Dictionary<string, Enum> Enums = new Dictionary<string, Enum>();
83 public Dictionary<string, Type> Interfaces = new Dictionary<string, Type>();
84 public Dictionary<string, ConstructorInfo> Constructors = new Dictionary<string, ConstructorInfo>();
85 public Dictionary<string, List<MemberInfo>> Members = new Dictionary<string, List<MemberInfo>>();
86 public Dictionary<string, FieldInfo> Fields = new Dictionary<string, FieldInfo>();
87 public Dictionary<string, PropertyInfo> Properties = new Dictionary<string, PropertyInfo>();
88 public Dictionary<string, List<MethodInfo>> Methods = new Dictionary<string, List<MethodInfo>>();
89 public Type type;
90 public string GUID;
91 public void AddMember(MemberInfo inf)
92 {
93 if(Members.ContainsKey(inf.ToString()))
94 {
95 Members[inf.ToString()].Add(inf);
96 }
97 else
98 {
99 List<MemberInfo> l = new List<MemberInfo>();
100 l.Add(inf);
101 Members.Add(inf.ToString(), l);
102 }
103 }
104 public void AddMethod(MethodInfo inf)
105 {
106 if (Methods.ContainsKey(inf.ToString()))
107 {
108 Methods[inf.ToString()].Add(inf);
109 }
110 else
111 {
112 List<MethodInfo> l = new List<MethodInfo>();
113 l.Add(inf);
114 Methods.Add(inf.ToString(), l);
115 }
116 }
117 public void AddField(FieldInfo inf)
118 {
119 Fields.Add(inf.ToString(),inf);
120 }
121 public void AddProperty(PropertyInfo inf)
122 {
123 Properties.Add(inf.ToString(),inf);
124 }
125 public object Invoke(string name, object o, object[] args)
126 {
127 return type.InvokeMember(name, BindingFlags.InvokeMethod, null, o, args);
128 }
129 public object GetProperty(string name, object obj)
130 {
131 PropertyInfo p = type.GetProperty(name);
132 return p.GetValue(obj);
133 }
134 public object Instance()
135 {
136 return Activator.CreateInstance(type);
137 }
138 public override string ToString()
139 {
140 return type.Name;
141 }
142 }
143 /* The ObjectInfo class contains a TypeInfo object */
144 public class ObjectInfo
145 {
147
148 }
149 /* It's loading the dll, and then it's getting all the types, modules, interfaces,
150 constructors, properties, fields, methods, and members. */
151 public Lib(string file)
152 {
153 dll = Assembly.LoadFile(file);
154 //Assembly.ReflectionOnlyLoad
155 Type[] tps = dll.GetExportedTypes();
156 foreach (Type type in tps)
157 {
158 TypeInfo t = new TypeInfo();
159 t.type = type;
160 string s = type.ToString();
161 s = s.Remove(0, s.LastIndexOf('.') + 1);
162 if (type.IsEnum)
163 {
164 GetEnums(t);
165 }
166 GetInterfaces(t);
167 GetConstructors(t);
168 GetProperties(t);
169 GetFields(t);
170 GetMethods(t);
171 GetMembers(t);
172 Types.Add(s,t);
173 }
174 Module[] mds = dll.GetModules();
175 foreach (Module m in mds)
176 {
177 string s = m.ToString();
178 s = s.Remove(0, s.LastIndexOf('.') + 1);
179 Modules.Add(s, m);
180 }
181 }
192 public object Invoke(Type type, string name, object o, object[] args)
193 {
194 return type.InvokeMember(name, BindingFlags.InvokeMethod, null, o, args);
195 }
206 public object Invoke(string st, string name, object o, object[] args)
207 {
208 Type t = Types[st].type;
209 return t.InvokeMember(name, BindingFlags.InvokeMethod, null, o, args);
210 }
218 public object GetProperty(string type, string name, object obj)
219 {
220 Type myType = Types[type].type;
221 PropertyInfo p = myType.GetProperty(name);
222 return p.GetValue(obj);
223 }
228 private void GetEnums(TypeInfo type)
229 {
230 Array ar = Enum.GetValues(type.type);
231 foreach (var item in ar)
232 {
233 if(!type.Enums.ContainsKey(item.ToString()))
234 type.Enums.Add(item.ToString(), (Enum)item);
235 }
236 }
243 private void GetInterfaces(TypeInfo type)
244 {
245 Type[] ts = type.type.GetInterfaces();
246 foreach (Type item in ts)
247 {
248 type.Interfaces.Add(item.ToString(), item);
249 }
250 }
254 private void GetConstructors(TypeInfo type)
255 {
256 ConstructorInfo[] ts = type.type.GetConstructors();
257 foreach (ConstructorInfo item in ts)
258 {
259 type.Constructors.Add(item.ToString(), item);
260 }
261 }
265 private void GetMembers(TypeInfo type)
266 {
267 MemberInfo[] ts = type.type.GetMembers();
268 foreach (MemberInfo item in ts)
269 {
270 type.AddMember(item);
271 }
272 }
276 private void GetProperties(TypeInfo type)
277 {
278 PropertyInfo[] ts = type.type.GetProperties();
279 foreach (PropertyInfo item in ts)
280 {
281 type.AddProperty(item);
282 }
283 }
287 private void GetFields(TypeInfo type)
288 {
289 FieldInfo[] ts = type.type.GetFields();
290 foreach (FieldInfo item in ts)
291 {
292 type.AddField(item);
293 }
294 }
298 private void GetMethods(TypeInfo type)
299 {
300 MethodInfo[] ts = type.type.GetMethods();
301 foreach (MethodInfo item in ts)
302 {
303 type.AddMethod(item);
304 }
305 }
306 }
307}
Dictionary< string, Enum > Enums
Definition Library.cs:82
Dictionary< string, Type > Interfaces
Definition Library.cs:83
object Invoke(string name, object o, object[] args)
Definition Library.cs:125
object GetProperty(string name, object obj)
Definition Library.cs:129
void AddProperty(PropertyInfo inf)
Definition Library.cs:121
Dictionary< string, List< MemberInfo > > Members
Definition Library.cs:85
void AddMethod(MethodInfo inf)
Definition Library.cs:104
Dictionary< string, List< MethodInfo > > Methods
Definition Library.cs:88
override string ToString()
Definition Library.cs:138
void AddField(FieldInfo inf)
Definition Library.cs:117
void AddMember(MemberInfo inf)
Definition Library.cs:91
Dictionary< string, FieldInfo > Fields
Definition Library.cs:86
Dictionary< string, ConstructorInfo > Constructors
Definition Library.cs:84
Dictionary< string, TypeInfo > Objects
Definition Library.cs:65
Assembly dll
Definition Library.cs:63
Lib(string file)
Definition Library.cs:151
Dictionary< string, Module > Modules
Definition Library.cs:66
Dictionary< string, TypeInfo > Types
Definition Library.cs:64
Dictionary< string, InterfaceMapping > Interfaces
Definition Library.cs:68
Dictionary< string, ConstructorInfo > Constructors
Definition Library.cs:67
object Invoke(Type type, string name, object o, object[] args)
Definition Library.cs:192
object GetProperty(string type, string name, object obj)
Definition Library.cs:218
object Invoke(string st, string name, object o, object[] args)
Definition Library.cs:206
static Lib dll
Definition Library.cs:16