// (C) 2004 Ben Maurer
//
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+//
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
using System;
using System.Reflection;
using System.Collections;
using System.CodeDom.Compiler;
using System.IO;
+using System.Text;
public class Outline {
+ Options options;
IndentedTextWriter o;
Type t;
- public Outline (Type t, TextWriter output)
+ public Outline (Type t, TextWriter output, Options options)
{
this.t = t;
this.o = new IndentedTextWriter (output, " ");
+ this.options = options;
}
- public void OutlineType (BindingFlags flags)
+ public void OutlineType ()
{
bool first;
o.Write (FormatType (method.ReturnType));
o.Write (" ");
- o.Write (t.Name);
+ o.Write (GetTypeName (t));
o.Write (" (");
OutlineParams (method.GetParameters ());
o.WriteLine (");");
return;
}
- o.Write (t.Name);
+ o.Write (GetTypeName (t));
if (((parent != null && parent != typeof (object) && parent != typeof (ValueType)) || interfaces.Length != 0) && ! t.IsEnum) {
first = true;
o.Write (" : ");
o.Write (FormatType (intf));
}
}
+
+ if (t.IsEnum) {
+ Type underlyingType = Enum.GetUnderlyingType (t);
+ if (underlyingType != typeof (int))
+ o.Write (" : {0}", FormatType (underlyingType));
+ }
o.WriteLine (" {");
o.Indent++;
first = true;
- foreach (ConstructorInfo ci in t.GetConstructors (flags)) {
+ foreach (ConstructorInfo ci in t.GetConstructors (DefaultFlags)) {
+
+ if (! ShowMember (ci))
+ continue;
+
if (first)
o.WriteLine ();
first = false;
first = true;
- foreach (MethodInfo m in Comparer.Sort (t.GetMethods (flags))) {
+ foreach (MethodInfo m in Comparer.Sort (t.GetMethods (DefaultFlags))) {
+
+ if (! ShowMember (m))
+ continue;
+
if ((m.Attributes & MethodAttributes.SpecialName) != 0)
continue;
first = true;
- foreach (PropertyInfo pi in Comparer.Sort (t.GetProperties (flags))) {
+ foreach (MethodInfo m in t.GetMethods (DefaultFlags)) {
+
+ if (! ShowMember (m))
+ continue;
+
+ if ((m.Attributes & MethodAttributes.SpecialName) == 0)
+ continue;
+ if (!(m.Name.StartsWith ("op_")))
+ continue;
+
+ if (first)
+ o.WriteLine ();
+ first = false;
+
+ OutlineOperator (m);
+
+ o.WriteLine ();
+ }
+
+ first = true;
+
+ foreach (PropertyInfo pi in Comparer.Sort (t.GetProperties (DefaultFlags))) {
+
+ if (! ((pi.CanRead && ShowMember (pi.GetGetMethod (true))) ||
+ (pi.CanWrite && ShowMember (pi.GetSetMethod (true)))))
+ continue;
if (first)
o.WriteLine ();
first = true;
- foreach (FieldInfo fi in t.GetFields (flags)) {
+ foreach (FieldInfo fi in t.GetFields (DefaultFlags)) {
+
+ if (! ShowMember (fi))
+ continue;
if (first)
o.WriteLine ();
first = true;
- foreach (EventInfo ei in Comparer.Sort (t.GetEvents (flags))) {
+ foreach (EventInfo ei in Comparer.Sort (t.GetEvents (DefaultFlags))) {
+
+ if (! ShowMember (ei.GetAddMethod ()))
+ continue;
if (first)
o.WriteLine ();
first = true;
- foreach (Type ntype in Comparer.Sort (t.GetNestedTypes (flags))) {
+ foreach (Type ntype in Comparer.Sort (t.GetNestedTypes (DefaultFlags))) {
+
+ if (! ShowMember (ntype))
+ continue;
if (first)
o.WriteLine ();
first = false;
- new Outline (ntype, o).OutlineType (flags);
+ new Outline (ntype, o, options).OutlineType ();
}
o.Indent--; o.WriteLine ("}");
}
+
+ BindingFlags DefaultFlags {
+ get {
+ BindingFlags f = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
+
+ if (options.DeclaredOnly)
+ f |= BindingFlags.DeclaredOnly;
+
+ return f;
+ }
+ }
// FIXME: add other interesting attributes?
void OutlineAttributes ()
void OutlineConstructor (ConstructorInfo ci)
{
o.Write (GetMethodVisibility (ci));
- o.Write (t.Name);
+ o.Write (RemoveGenericArity (t.Name));
o.Write (" (");
OutlineParams (ci.GetParameters ());
o.Write (");");
void OutlineProperty (PropertyInfo pi)
{
ParameterInfo [] idxp = pi.GetIndexParameters ();
- MethodBase accessor = pi.CanRead ? pi.GetGetMethod (true) : pi.GetSetMethod (true);
+ MethodBase g = pi.GetGetMethod (true);
+ MethodBase s = pi.GetSetMethod (true);
+ MethodBase accessor = g != null ? g : s;
+
+ if (pi.CanRead && pi.CanWrite) {
+
+
+ // Get the more accessible accessor
+ if ((g.Attributes & MethodAttributes.MemberAccessMask) !=
+ (s.Attributes & MethodAttributes.MemberAccessMask)) {
+
+ if (g.IsPublic) accessor = g;
+ else if (s.IsPublic) accessor = s;
+ else if (g.IsFamilyOrAssembly) accessor = g;
+ else if (s.IsFamilyOrAssembly) accessor = s;
+ else if (g.IsAssembly || g.IsFamily) accessor = g;
+ else if (s.IsAssembly || s.IsFamily) accessor = s;
+ }
+ }
o.Write (GetMethodVisibility (accessor));
o.Write (GetMethodModifiers (accessor));
o.WriteLine (" {");
o.Indent ++;
- if (pi.CanRead) o.WriteLine ("get;");
- if (pi.CanWrite) o.WriteLine ("set;");
+ if (g != null && ShowMember (g)) {
+ if ((g.Attributes & MethodAttributes.MemberAccessMask) !=
+ (accessor.Attributes & MethodAttributes.MemberAccessMask))
+ o.Write (GetMethodVisibility (g));
+ o.WriteLine ("get;");
+ }
+
+ if (s != null && ShowMember (s)) {
+ if ((s.Attributes & MethodAttributes.MemberAccessMask) !=
+ (accessor.Attributes & MethodAttributes.MemberAccessMask))
+ o.Write (GetMethodVisibility (s));
+ o.WriteLine ("set;");
+ }
o.Indent --;
o.Write ("}");
o.Write (FormatType (mi.ReturnType));
o.Write (" ");
o.Write (mi.Name);
+#if NET_2_0
+ o.Write (FormatGenericParams (mi.GetGenericArguments ()));
+#endif
+ o.Write (" (");
+ OutlineParams (mi.GetParameters ());
+ o.Write (");");
+ }
+
+ void OutlineOperator (MethodInfo mi)
+ {
+ o.Write (GetMethodVisibility (mi));
+ o.Write (GetMethodModifiers (mi));
+ if (mi.Name == "op_Explicit" || mi.Name == "op_Implicit") {
+ o.Write (mi.Name.Substring (3).ToLower ());
+ o.Write (" operator ");
+ o.Write (FormatType (mi.ReturnType));
+ } else {
+ o.Write (FormatType (mi.ReturnType));
+ o.Write (" operator ");
+ o.Write (OperatorFromName (mi.Name));
+ }
o.Write (" (");
OutlineParams (mi.GetParameters ());
o.Write (");");
if (fi.IsFamily) o.Write ("protected ");
if (fi.IsPrivate) o.Write ("private ");
if (fi.IsAssembly) o.Write ("internal ");
- if (fi.IsLiteral) o.Write ("const ");
+ if (fi.IsLiteral) o.Write ("const ");
+ if (fi.IsInitOnly) o.Write ("readonly ");
+
o.Write (FormatType (fi.FieldType));
o.Write (" ");
o.Write (fi.Name);
return "internal";
}
}
+
+ string FormatGenericParams (Type [] args)
+ {
+ StringBuilder sb = new StringBuilder ();
+ if (args.Length == 0)
+ return "";
+
+ sb.Append ("<");
+ for (int i = 0; i < args.Length; i++) {
+ if (i > 0)
+ sb.Append (",");
+ sb.Append (FormatType (args [i]));
+ }
+ sb.Append (">");
+ return sb.ToString ();
+ }
string FormatType (Type t)
{
- string type = t.FullName;
+ string type = GetFullName (t);
- if (t.Namespace == this.t.Namespace)
- return t.Name;
-
- if (!type.StartsWith ("System."))
+ if (!type.StartsWith ("System.")) {
+ if (t.Namespace == this.t.Namespace)
+ return t.Name;
return type;
+ }
if (t.HasElementType) {
Type et = t.GetElementType ();
return type;
}
+
+ public static string RemoveGenericArity (string name)
+ {
+ int start = 0;
+ StringBuilder sb = new StringBuilder ();
+ while (start < name.Length) {
+ int pos = name.IndexOf ('`', start);
+ if (pos < 0) {
+ sb.Append (name.Substring (start));
+ break;
+ }
+ sb.Append (name.Substring (start, pos-start));
+
+ pos++;
+
+ while ((pos < name.Length) && Char.IsNumber (name [pos]))
+ pos++;
+
+ start = pos;
+ }
+
+ return sb.ToString ();
+ }
+
+ string GetTypeName (Type t)
+ {
+ StringBuilder sb = new StringBuilder ();
+ GetTypeName (sb, t);
+ return sb.ToString ();
+ }
+
+ void GetTypeName (StringBuilder sb, Type t)
+ {
+ sb.Append (RemoveGenericArity (t.Name));
+#if NET_2_0
+ sb.Append (FormatGenericParams (t.GetGenericArguments ()));
+#endif
+ }
+
+ string GetFullName (Type t)
+ {
+ StringBuilder sb = new StringBuilder ();
+ GetFullName_recursed (sb, t, false);
+ return sb.ToString ();
+ }
+
+ void GetFullName_recursed (StringBuilder sb, Type t, bool recursed)
+ {
+#if NET_2_0
+ if (t.IsGenericParameter) {
+ sb.Append (t.Name);
+ return;
+ }
+#endif
+
+ if (t.DeclaringType != null) {
+ GetFullName_recursed (sb, t.DeclaringType, true);
+ sb.Append (".");
+ }
+
+ if (!recursed) {
+ string ns = t.Namespace;
+ if ((ns != null) && (ns != "")) {
+ sb.Append (ns);
+ sb.Append (".");
+ }
+ }
+
+ GetTypeName (sb, t);
+ }
+
+ string OperatorFromName (string name)
+ {
+ switch (name) {
+ case "op_UnaryPlus": return "+";
+ case "op_UnaryNegation": return "-";
+ case "op_LogicalNot": return "!";
+ case "op_OnesComplement": return "~";
+ case "op_Increment": return "++";
+ case "op_Decrement": return "--";
+ case "op_True": return "true";
+ case "op_False": return "false";
+ case "op_Addition": return "+";
+ case "op_Subtraction": return "-";
+ case "op_Multiply": return "*";
+ case "op_Division": return "/";
+ case "op_Modulus": return "%";
+ case "op_BitwiseAnd": return "&";
+ case "op_BitwiseOr": return "|";
+ case "op_ExclusiveOr": return "^";
+ case "op_LeftShift": return "<<";
+ case "op_RightShift": return ">>";
+ case "op_Equality": return "==";
+ case "op_Inequality": return "!=";
+ case "op_GreaterThan": return ">";
+ case "op_LessThan": return "<";
+ case "op_GreaterThanOrEqual": return ">=";
+ case "op_LessThanOrEqual": return "<=";
+ default: return name;
+ }
+ }
+
+ bool ShowMember (MemberInfo mi)
+ {
+ if (mi.MemberType == MemberTypes.Constructor && ((MethodBase) mi).IsStatic)
+ return false;
+
+ if (options.ShowPrivate)
+ return true;
+
+ switch (mi.MemberType) {
+ case MemberTypes.Constructor:
+ case MemberTypes.Method:
+ MethodBase mb = mi as MethodBase;
+
+ if (mb.IsFamily || mb.IsPublic || mb.IsFamilyOrAssembly)
+ return true;
+
+ return false;
+
+
+ case MemberTypes.Field:
+ FieldInfo fi = mi as FieldInfo;
+
+ if (fi.IsFamily || fi.IsPublic || fi.IsFamilyOrAssembly)
+ return true;
+
+ return false;
+
+
+ case MemberTypes.NestedType:
+ case MemberTypes.TypeInfo:
+ Type t = mi as Type;
+
+ switch (t.Attributes & TypeAttributes.VisibilityMask){
+ case TypeAttributes.Public:
+ case TypeAttributes.NestedPublic:
+ case TypeAttributes.NestedFamily:
+ case TypeAttributes.NestedFamORAssem:
+ return true;
+ }
+
+ return false;
+ }
+
+ // What am I !!!
+ return true;
+ }
}
public class Comparer : IComparer {