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
|
/**
* Copyright (c) Rich Hickey. All rights reserved.
* The use and distribution terms for this software are covered by the
* Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
* which can be found in the file CPL.TXT at the root of this distribution.
* By using this software in any fashion, you are agreeing to be bound by
* the terms of this license.
* You must not remove this notice, or any other, from this software.
**/
using System;
using System.IO;
using System.Reflection;
namespace TypeDump
{
class Program
{
static void Main(string[] args)
{
if (args.Length < 2)
{
Console.Error.WriteLine("usage: typedump assembly namespace [namespace ...]");
return;
}
Assembly a = Assembly.Load(new AssemblyName(args[0]));
Console.WriteLine('(');
foreach (Type t in a.GetExportedTypes())
{
if(Array.IndexOf(args,t.Namespace,1) >= 0
//we don't deal with generics
&& !(t.IsGenericTypeDefinition || t.IsGenericType))
dumpType(t, Console.Out);
}
Console.WriteLine(')');
}
static bool hasGenericOrRefParam(ParameterInfo[] args)
{
return Array.Find(args, delegate(ParameterInfo pi)
{
Type pit = pi.ParameterType;
return pit.IsGenericTypeDefinition || pit.IsGenericType ||pit.IsByRef;
})
!= null;
}
static void dumpType(Type t, TextWriter p)
{
p.Write('(');
p.Write("(:name \"" + t + "\")");
if(t.BaseType != null)
p.Write(" (:super \"" + t.BaseType + "\")");
Type[] interfaces = t.GetInterfaces();
if (interfaces.Length > 0)
{
p.WriteLine();
p.Write(" (:interfaces");
foreach (Type it in interfaces)
{
if (!(it.IsGenericTypeDefinition || it.IsGenericType))
p.Write(" \"" + it + "\"");
}
p.Write(')');
}
foreach (ConstructorInfo ci in t.GetConstructors(BindingFlags.Public|BindingFlags.Instance))
{
//this should filter for pointer args etc
Object[] clsattr = ci.GetCustomAttributes(typeof(CLSCompliantAttribute), false);
if (clsattr.Length > 0 && !((CLSCompliantAttribute)clsattr[0]).IsCompliant)
continue;
ParameterInfo[] args = ci.GetParameters();
//we don't deal with generics
if (hasGenericOrRefParam(args))
continue;
p.WriteLine();
p.Write(" (:ctor (:arity " + args.Length + ")");
if (args.Length > 0
&& args[args.Length - 1].GetCustomAttributes(typeof(ParamArrayAttribute), false).Length > 0)
{
p.Write("(:varargs t)");
}
if (args.Length > 0)
{
p.WriteLine();
p.Write(" (:args");
foreach (ParameterInfo pi in args)
{
p.Write(" \"" + pi.ParameterType + "\"");
}
p.Write(')');
}
p.Write(')');
}
foreach (MethodInfo mi in t.GetMethods(BindingFlags.Public | BindingFlags.Instance |BindingFlags.Static))
{
//this should filter for pointer args etc
Object[] clsattr = mi.GetCustomAttributes(typeof(CLSCompliantAttribute), false);
if (clsattr.Length > 0 && !((CLSCompliantAttribute)clsattr[0]).IsCompliant)
continue;
ParameterInfo[] args = mi.GetParameters();
//we don't deal with generics
if (hasGenericOrRefParam(args) ||mi.ReturnType.IsGenericType)
continue;
p.WriteLine();
p.Write(" (:method (:name \"" + mi.Name + "\") (:arity " + args.Length + ")(:ret \"" + mi.ReturnType + "\")");
if (mi.IsStatic)
{
p.Write(" (:static t)");
}
if (args.Length > 0
&& args[args.Length - 1].GetCustomAttributes(typeof(ParamArrayAttribute), false).Length > 0)
{
p.Write(" (:varargs t)");
}
if (mi.IsSpecialName)
{
p.Write(" (:special t)");
}
if (args.Length > 0)
{
p.WriteLine();
p.Write(" (:args");
foreach (ParameterInfo pi in args)
{
p.Write(" \"" + pi.ParameterType + "\"");
}
p.Write(')');
}
p.Write(')');
}
foreach (FieldInfo fi in t.GetFields(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static))
{
p.WriteLine();
p.Write(" (:field (:name \"");
p.Write(fi.Name);
p.Write("\") (:type \"");
p.Write(fi.FieldType);
p.Write("\")");
if (fi.IsStatic)
{
p.Write(" (:static t)");
if (fi.IsLiteral)
{
Object v = fi.GetRawConstantValue();
p.Write(" (:const-value ");
if (v is String)
p.Write('"');
p.Write(v);
if (v is String)
p.Write('"');
p.Write(")");
}
}
if (fi.IsSpecialName)
{
p.Write(" (:special t)");
}
p.Write(")");
}
p.WriteLine(')');
}
}
}
|