-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
250 lines (234 loc) · 9.59 KB
/
Program.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/**
* Copyright 2012 Tim Rogers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Reflection;
namespace ListMembers
{
class Program
{
static void PrintHelp()
{
Console.Out.WriteLine(
"Usage: ListMembers [Public|Protected|Private] <Assembly1> [<Assembly2> ...]");
Console.Out.WriteLine("\tDefaults to \"Public\".\n");
}
static List<Type> GetSubTypes(Type T, BindingFlags flags, bool recurse, bool Protected = false, bool Private = false)
{
List<Type> retSet = new List<Type>();
if (Protected || Private) flags = flags | BindingFlags.NonPublic;
Type[] tmp = T.GetNestedTypes(flags);
foreach (Type t in tmp)
{
if (t.IsPublic || (Protected && t.IsNotPublic && t.IsNestedFamily) || (Private && t.IsNotPublic && !t.IsNestedAssembly))
retSet.Add(t);
}
if (recurse)
{
foreach (Type t in retSet)
{
retSet.AddRange(GetSubTypes(t, flags, recurse, Protected, Private));
}
}
return retSet;
}
static IEnumerable<string> ExpandPath(string path)
{
string dirPath = path.LastIndexOf('\\') >= 0 ? path.Substring(0, path.LastIndexOf('\\')) : String.Empty;
string prePath = path.Substring(0, path.IndexOf('*'));
string postPath = path.Substring(path.IndexOf('*') + 1);
if (dirPath.Equals(String.Empty))
{
dirPath = Directory.GetCurrentDirectory();
}
List<string> returning = new List<string>();
IEnumerable<string> files = Directory.GetFiles(dirPath);
foreach (string file in files)
{
if (file.StartsWith(prePath) && file.EndsWith(postPath))
returning.Add(Path.Combine(dirPath, file));
}
return returning;
}
static Assembly LoadAssembly(string path)
{
try
{
if (!File.Exists(path))
path = Path.Combine(Directory.GetCurrentDirectory(), path);
return Assembly.LoadFrom(path);
}
catch (Exception e)
{
ConsoleColor current = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.Out.WriteLine("Failed to load file: " + path);
Console.ForegroundColor = current;
throw e;
}
}
static void Main(string[] args)
{
BindingFlags BaseFlags = BindingFlags.Public;
BindingFlags MethodFlags = BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly;
bool Public = true, Protected = false, Private = false;
if (args.Length < 1)
{
PrintHelp();
return;
}
int pos = 0;
switch (args[0].ToLower(CultureInfo.CurrentCulture))
{
case @"public":
pos = 1;
break;
case @"protected":
Protected = true;
BaseFlags = BaseFlags | BindingFlags.NonPublic;
goto case @"public";
case @"private":
Private = true;
goto case @"protected";
default:
break;
}
if (args.Length <= pos)
{
PrintHelp();
return;
}
List<Assembly> Libs = new List<Assembly>();
for (int i = pos; i < args.Length; i++)
{
string path = args[i];
if (path.Contains("*"))
foreach (string s in ExpandPath(path))
{
try
{
Libs.Add(LoadAssembly(s));
}
catch (Exception E)
{
Console.Error.WriteLine(E.Message);
}
}
else
try
{
Libs.Add(LoadAssembly(path));
}
catch (Exception E)
{
try
{
string newPath = path;
// Since we're checking the GAC, drop the file extension (if any).
if (path[path.Length-4] == '.')
newPath = path.Substring(0, path.Length - 4);
newPath = AssemblyCache.QueryAssemblyInfo(newPath);
Libs.Add(LoadAssembly(newPath));
}
catch(Exception e)
{
Console.Error.WriteLine(E.Message);
Console.Error.WriteLine(e.Message);
}
}
}
foreach (Assembly A in Libs)
{
List<Type> tmpTypes;
try
{
tmpTypes = new List<Type>(A.GetExportedTypes());
}
catch (ReflectionTypeLoadException e)
{
Console.Error.WriteLine("Error loading types from assembly '" + A.GetName() + "':");
foreach (Exception E in e.LoaderExceptions)
{
Console.Error.WriteLine(E.Message);
}
Console.Error.WriteLine(e.StackTrace);
continue;
}
catch (FileNotFoundException e)
{
Console.Error.WriteLine(e.Message);
Console.Error.WriteLine(e.StackTrace);
continue;
}
bool AsmListed = false;
List<Type> types = new List<Type>(tmpTypes);
foreach (Type T in tmpTypes)
{
if ((!Protected && (T.IsNestedFamily || T.IsNotPublic)) || (!Private && T.IsNestedPrivate))
types.Remove(T);
}
List<Type> Subtypes = new List<Type>();
foreach (Type type in types)
{
Subtypes.AddRange(GetSubTypes(type, BaseFlags, true, Protected, Private));
}
types.AddRange(Subtypes);
foreach (Type T in types)
{
List<MethodInfo> methods = new List<MethodInfo>();
List<string> methodNames = new List<string>();
List<MethodInfo> tmp = new List<MethodInfo>(T.GetMethods(BaseFlags | MethodFlags));
foreach (MethodInfo M in tmp)
{
if ((M.IsPublic || (Protected && M.IsFamily) || (Private && M.IsPrivate))
&& !methodNames.Contains(String.Concat(M.Name,M.ReturnType.Name)))
{
methods.Add(M);
methodNames.Add(String.Concat(M.Name, M.ReturnType.Name));
}
}
if (methods.Count > 0)
{
if (!AsmListed)
{
Console.Out.WriteLine("Assembly: " + A.FullName);
AsmListed = true;
}
Console.Out.WriteLine("\tType: " + T.FullName + " (" + ( T.IsPublic ? "public" :
( T.IsNotPublic ? "non-public": "???"))
+") --");
foreach (MethodInfo I in methods)
{
string s = String.Empty;
if (I.IsStatic)
s += " S";
s += "\t\t";
if (I.IsPublic) s += "public ";
else if (I.IsFamily) s += "protected ";
else if (I.IsPrivate) s += "private ";
else s += "??? ";
s += I.Name;
s += " : " + I.ReturnType.Name;
Console.Out.WriteLine(s);
}
}
}
}
}
}
}