-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrmILInstructions.cs
192 lines (180 loc) · 7.61 KB
/
frmILInstructions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using SDILReader;
namespace MSIL
{
public partial class frmILInstructions : Form
{
private Assembly assembly;
private List<Type> types = new List<Type>();
private Dictionary<Type, IEnumerable<MethodInfo>> methods = new Dictionary<Type, IEnumerable<MethodInfo>>();
public frmILInstructions()
{
InitializeComponent();
gvILInstructions.AutoGenerateColumns = false;
tbAssemblyName.Focus();
//tbAssemblyName.Text = @"C:\temp\mscorlib.dll";
//ReadAndShowAssemblyInfo(tbAssemblyName.Text);
}
private void tbAssemblyName_Leave(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(tbAssemblyName.Text)) return;
ReadAndShowAssemblyInfo(tbAssemblyName.Text);
}
private void btnBrowse_Click(object sender, EventArgs e)
{
using (OpenFileDialog _openFileDialog = new OpenFileDialog() { DefaultExt = ".dll" })
{
if ((!string.IsNullOrWhiteSpace(tbAssemblyName.Text)) && Directory.Exists(Path.GetDirectoryName(tbAssemblyName.Text)))
{
_openFileDialog.InitialDirectory = tbAssemblyName.Text;
}
if (_openFileDialog.ShowDialog() != DialogResult.OK)
{
return;
}
if (string.IsNullOrWhiteSpace(_openFileDialog.FileName)) return;
tbAssemblyName.Text = _openFileDialog.FileName;
ReadAndShowAssemblyInfo(tbAssemblyName.Text);
}
}
private void ReadAndShowAssemblyInfo(string inAssemblyFileName)
{
Cursor _currentCursor = Cursor.Current;
try
{
Cursor.Current = Cursors.WaitCursor;
this.types.Clear();
this.methods.Clear();
this.assembly = Assembly.LoadFrom(inAssemblyFileName);
this.types = TypesHelper.GetTypes(this.assembly);
ShowTypes();
}
catch (Exception ex)
{
MessageBox.Show(string.Format("A problem occured while loading assembly {0}.{1}Exception data:{1}{2}", inAssemblyFileName, Environment.NewLine, ex.ToString()),
"Problem", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
Cursor.Current = _currentCursor;
}
}
private void ShowTypes()
{
cbTypes.DisplayMember = "mTypeName";
cbTypes.ValueMember = "mType";
cbTypes.DataSource = this.types.Select(t => new { mTypeName = t.Name, mType = t }).ToList();
cbTypes.AutoCompleteCustomSource.AddRange(this.types.Select(t => t.Name).ToArray());
}
public void ShowMethods(Type selectedType)
{
if (!this.methods.ContainsKey(selectedType))
{
this.methods.Add(selectedType, TypesHelper.GetMethodInfos(selectedType));
}
cbMethods.ValueMember = "mMethodInfo";
cbMethods.DisplayMember = "mMethodName";
var _ds = this.methods[selectedType].Select(m => new { mMethodInfo = m, mMethodName = m.ToString() }).ToList();
cbMethods.DataSource = _ds;
cbMethods.AutoCompleteCustomSource.AddRange(_ds.Select(s => s.mMethodName).ToArray());
}
private void Show_ILInstructions(MethodInfo inMethodInfo)
{
gvILInstructions.DataSource = TypesHelper.GetILInstructions(inMethodInfo);
}
private void cbTypes_SelectedIndexChanged(object sender, EventArgs e)
{
if (!(cbTypes.SelectedValue is Type)) return;
try
{
ShowMethods(cbTypes.SelectedValue as Type);
}
catch (Exception ex)
{
MessageBox.Show(string.Format("A problem occured while displaying IL instuctions.{0}Assembly: {1}{0}Type: {2}{0}Exception data: {3}",
Environment.NewLine, this.assembly.FullName, cbTypes.Text, ex.ToString()),
"Problem",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private void cbMethods_SelectedIndexChanged(object sender, EventArgs e)
{
if (!(cbMethods.SelectedValue is MethodInfo)) return;
try
{
MethodInfo _methodInfo = cbMethods.SelectedValue as MethodInfo;
if (_methodInfo.IsGenericMethod)
{
Type _type = _methodInfo.DeclaringType;
_methodInfo = _type.GetMethod(_methodInfo.Name, _methodInfo.GetGenericArguments());
}
Show_ILInstructions(_methodInfo);
}
catch (Exception ex)
{
MessageBox.Show(string.Format("A problem occured while displaying IL instuctions.{0}Assembly: {1}{0}Type: {2}{0}Method: {3}{0}Exception data:{4}",
Environment.NewLine, this.assembly.FullName, cbTypes.Text, cbMethods.Text, ex.ToString()),
"Problem",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private void btnSearchTypeAndMethod_Click(object sender, EventArgs e)
{
Cursor _currentCursor = Cursor.Current;
string _className = string.Empty;
string _methodName = string.Empty;
Type _type = null;
try
{
Cursor.Current = Cursors.WaitCursor;
var _words = tbManualEntry.Text.Split(new char[] { '.' });
if (_words.Length != 2)
{
throw new ArgumentException("The manual entry pattern should be [ClassName.Method]!");
}
_className = _words[0];
_methodName = _words[1];
_type = this.types.Single(t => t.Name == _className);
MethodInfo _methodInfo = _type.GetMethod(_methodName);
if (_methodInfo == null)
{
throw new NullReferenceException(string.Format("The type {0} doesn't have a method named {1}", _className, _methodName));
}
Show_ILInstructions(_methodInfo);
}
catch (InvalidOperationException iopex)
{
MessageBox.Show(string.Format("The assembly {0} doesn't have a type named {1}", this.assembly.FullName, _className),
"Problem", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
catch (AmbiguousMatchException ambex)
{
cbTypes.SelectedValue = _type;
MessageBox.Show(string.Format("The type {0} contains more than one method named {1}. Please, use the combobox to select the exact method.",
_className, _methodName), "Problem", MessageBoxButtons.OK, MessageBoxIcon.Error);
cbMethods.Focus();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Problem", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
Cursor.Current = _currentCursor;
}
}
}
}