singrdk/base/Windows/txref/TxRef.cs

916 lines
29 KiB
C#
Raw Permalink Normal View History

2008-11-17 18:29:00 -05:00
// ----------------------------------------------------------------------------
2008-03-05 09:52:00 -05:00
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
2008-11-17 18:29:00 -05:00
// ----------------------------------------------------------------------------
2008-03-05 09:52:00 -05:00
using System;
using System.IO;
using System.Reflection;
using System.Collections;
using System.Text;
public class TypeWriter
{
static Hashtable typeNames;
static Hashtable opNames;
static Hashtable opSortKeys;
public static int memberCt;
TextWriter writer;
int indent;
bool startOfLine;
bool includeMemberKeys = false;
bool fullTypeNames;
public TypeWriter(TextWriter writer)
{
this.writer = writer;
}
static TypeWriter()
{
typeNames = new Hashtable();
typeNames.Add(typeof(void), "void");
typeNames.Add(typeof(System.Object), "object");
typeNames.Add(typeof(System.String), "string");
typeNames.Add(typeof(System.SByte), "sbyte");
typeNames.Add(typeof(System.Byte), "byte");
typeNames.Add(typeof(System.Int16), "short");
typeNames.Add(typeof(System.UInt16), "ushort");
typeNames.Add(typeof(System.Int32), "int");
typeNames.Add(typeof(System.UInt32), "uint");
typeNames.Add(typeof(System.Int64), "long");
typeNames.Add(typeof(System.UInt64), "ulong");
typeNames.Add(typeof(System.Char), "char");
typeNames.Add(typeof(System.Boolean), "bool");
typeNames.Add(typeof(System.Single), "float");
typeNames.Add(typeof(System.Double), "double");
typeNames.Add(typeof(System.Decimal), "decimal");
opNames = new Hashtable();
opNames.Add("op_UnaryPlus", "+");
opNames.Add("op_Addition", "+");
opNames.Add("op_Increment", "++");
opNames.Add("op_UnaryNegation", "-");
opNames.Add("op_Subtraction", "-");
opNames.Add("op_Decrement", "--");
opNames.Add("op_Multiply", "*");
opNames.Add("op_Division", "/");
opNames.Add("op_Modulus", "%");
opNames.Add("op_BitwiseAnd", "&");
opNames.Add("op_BitwiseOr", "|");
opNames.Add("op_ExclusiveOr", "^");
opNames.Add("op_Negation", "!");
opNames.Add("op_OnesComplement", "~");
opNames.Add("op_LeftShift", "<<");
opNames.Add("op_RightShift", ">>");
opNames.Add("op_Equality", "==");
opNames.Add("op_Inequality", "!=");
opNames.Add("op_GreaterThanOrEqual", ">=");
opNames.Add("op_LessThanOrEqual", "<=");
opNames.Add("op_GreaterThan", ">");
opNames.Add("op_LessThan", "<");
opNames.Add("op_True", "true");
opNames.Add("op_False", "false");
opNames.Add("op_Implicit", "implicit");
opNames.Add("op_Explicit", "explicit");
opSortKeys = new Hashtable();
opSortKeys.Add("op_UnaryPlus", "A");
opSortKeys.Add("op_Addition", "B");
opSortKeys.Add("op_Increment", "C");
opSortKeys.Add("op_UnaryNegation", "D");
opSortKeys.Add("op_Subtraction", "E");
opSortKeys.Add("op_Decrement", "F");
opSortKeys.Add("op_Multiply", "G");
opSortKeys.Add("op_Division", "H");
opSortKeys.Add("op_Modulus", "I");
opSortKeys.Add("op_BitwiseAnd", "J");
opSortKeys.Add("op_BitwiseOr", "K");
opSortKeys.Add("op_ExclusiveOr", "L");
opSortKeys.Add("op_Negation", "M");
opSortKeys.Add("op_OnesComplement", "N");
opSortKeys.Add("op_LeftShift", "O");
opSortKeys.Add("op_RightShift", "P");
opSortKeys.Add("op_Equality", "Q");
opSortKeys.Add("op_Inequality", "R");
opSortKeys.Add("op_GreaterThanOrEqual", "S");
opSortKeys.Add("op_LessThanOrEqual", "T");
opSortKeys.Add("op_GreaterThan", "U");
opSortKeys.Add("op_LessThan", "V");
opSortKeys.Add("op_True", "W");
opSortKeys.Add("op_False", "X");
opSortKeys.Add("op_Implicit", "Y");
opSortKeys.Add("op_Explicit", "Z");
}
public bool FullTypeNames
{
get { return fullTypeNames; }
set { fullTypeNames = value; }
}
void AddMemberEntries(ArrayList list, Type type, bool inherited)
{
foreach (MemberInfo member in type.GetMembers(
//| BindingFlags.DeclaredOnly
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public))
{
2008-11-17 18:29:00 -05:00
if (member.DeclaringType != member.ReflectedType) {
2008-03-05 09:52:00 -05:00
continue;
}
// if (!inherited || member.MemberType != MemberTypes.Constructor) {
list.Add(new MemberEntry(member));
//}
}
}
static void AppendParams(StringBuilder sb, ParameterInfo[] parameters)
{
sb.Append('(');
2008-11-17 18:29:00 -05:00
for (int i = 0; i < parameters.Length; i++) {
2008-03-05 09:52:00 -05:00
if (i != 0) sb.Append(',');
AppendType(sb, parameters[i].ParameterType);
}
sb.Append(')');
}
static void AppendType(StringBuilder sb, Type type)
{
2008-11-17 18:29:00 -05:00
if (IsArray(type)) {
2008-03-05 09:52:00 -05:00
AppendType(sb, type.GetElementType());
sb.Append("[]");
}
2008-11-17 18:29:00 -05:00
else if (type.IsByRef) {
2008-03-05 09:52:00 -05:00
AppendType(sb, type.GetElementType());
sb.Append("@");
}
2008-11-17 18:29:00 -05:00
else if (type.IsPointer) {
2008-03-05 09:52:00 -05:00
AppendType(sb, type.GetElementType());
sb.Append("*");
}
2008-11-17 18:29:00 -05:00
else {
2008-03-05 09:52:00 -05:00
sb.Append(type.FullName);
}
}
static bool ContainsInterface(Type[] types, Type type)
{
2008-11-17 18:29:00 -05:00
foreach (Type t in types) {
2008-03-05 09:52:00 -05:00
if (ContainsType(t.GetInterfaces(), type)) return true;
}
return false;
}
static bool ContainsType(Type[] types, Type type)
{
2008-11-17 18:29:00 -05:00
if (types != null) {
foreach (Type t in types) {
2008-03-05 09:52:00 -05:00
if (t == type) return true;
}
}
return false;
}
static MethodInfo GetAccessor(EventInfo evnt)
{
return evnt.GetAddMethod();
}
static MethodInfo GetAccessor(PropertyInfo property)
{
MethodInfo result = property.GetGetMethod();
if (result == null) result = property.GetSetMethod();
return result;
}
static string GetMemberKey(MemberInfo member)
{
StringBuilder sb = new StringBuilder();
2008-11-17 18:29:00 -05:00
switch (member.MemberType) {
2008-03-05 09:52:00 -05:00
case MemberTypes.Constructor:
sb.Append("M:");
sb.Append(member.DeclaringType.FullName);
sb.Append(".#ctor");
AppendParams(sb, ((ConstructorInfo)member).GetParameters());
break;
case MemberTypes.Field:
sb.Append("F:");
sb.Append(member.DeclaringType.FullName);
sb.Append('.');
sb.Append(((FieldInfo)member).Name);
break;
case MemberTypes.Property:
sb.Append("P:");
sb.Append(member.DeclaringType.FullName);
sb.Append('.');
sb.Append(((PropertyInfo)member).Name);
ParameterInfo[] pi = ((PropertyInfo)member).GetIndexParameters();
if (pi.Length != 0) AppendParams(sb, pi);
break;
case MemberTypes.Method:
sb.Append("M:");
sb.Append(member.DeclaringType.FullName);
sb.Append('.');
sb.Append(((MethodInfo)member).Name);
AppendParams(sb, ((MethodInfo)member).GetParameters());
break;
case MemberTypes.Event:
sb.Append("E:");
sb.Append(member.DeclaringType.FullName);
sb.Append('.');
sb.Append(((EventInfo)member).Name);
break;
}
return sb.ToString().Trim();
}
string[] GetInterfaceNames(Type type)
{
Type[] typeIntfs = type.GetInterfaces();
if (typeIntfs.Length == 0) return new string[0];
Type[] baseIntfs = type.BaseType == null ? null : type.BaseType.GetInterfaces();
ArrayList list = new ArrayList();
2008-11-17 18:29:00 -05:00
foreach (Type intf in typeIntfs) {
if (!ContainsInterface(typeIntfs, intf)) {
2008-03-05 09:52:00 -05:00
string s = intf.Name;
#if GENERICS
2008-11-17 18:29:00 -05:00
if (intf.HasGenericArguments) {
2008-03-05 09:52:00 -05:00
s+="<";
Type[] types = intf.GetGenericArguments();
2008-11-17 18:29:00 -05:00
for (int i = 0; i < types.Length; i++) {
2008-03-05 09:52:00 -05:00
if (i != 0) s+=", ";
s+=types[i].Name;
}
s+=">";
}
#endif
list.Add(s);
s = "";
}
}
list.Sort();
return (string[])list.ToArray(typeof(string));
}
static bool IsVarArgs(MethodBase m)
{
return (m.CallingConvention == CallingConventions.VarArgs);
}
static bool IsArray(Type type)
{
return type.IsArray && type != typeof(Array);
}
static bool IsDelegate(Type type)
{
return type.IsSubclassOf(typeof(Delegate)) && type != typeof(MulticastDelegate);
}
static bool IsOverride(MethodInfo method)
{
return (method.Attributes & (MethodAttributes.Virtual |
MethodAttributes.NewSlot)) == MethodAttributes.Virtual;
}
void Write(string s)
{
WriteIndent();
writer.Write(s);
}
void Write(char ch)
{
WriteIndent();
writer.Write(ch);
}
public void WriteNamespace(string namespaceName, Type[] types)
{
Write("namespace ");
Write(namespaceName);
WriteLine();
Write("{");
WriteLine();
indent++;
2008-11-17 18:29:00 -05:00
foreach (Type t in types) {
if (t.IsPublic && t.Namespace == namespaceName) {
2008-03-05 09:52:00 -05:00
WriteTypeDecl(t);
}
}
indent--;
Write("} ");
WriteLine();
}
void WriteConstructor(ConstructorInfo ctor)
{
if (!(ctor.IsPublic || ctor.IsFamily || ctor.IsFamilyOrAssembly)) return;
if (ctor.IsStatic) return;
Write(ctor.IsPublic ? "public " : "protected ");
Write(ctor.DeclaringType.Name);
Write('(');
WriteParamList(ctor.GetParameters());
2008-11-17 18:29:00 -05:00
if (IsVarArgs(ctor)) {
2008-03-05 09:52:00 -05:00
Write(", __arglist");
}
Write(");");
WriteMemberEnd(ctor);
}
void WriteEvent(EventInfo evnt)
{
2008-11-17 18:29:00 -05:00
if (evnt.GetAddMethod() == null) {
2008-03-05 09:52:00 -05:00
return;
}
if (!(evnt.GetAddMethod().IsPublic || evnt.GetAddMethod().IsFamily || evnt.GetAddMethod().IsFamilyOrAssembly)) return;
memberCt++;
Write(evnt.GetAddMethod().IsPublic ? "public " : "protected ");
Write("event ");
Write(evnt.EventHandlerType.Name);
Write(" ");
Write(evnt.Name);
Write(";");
WriteMemberEnd(evnt);
}
void WriteField(FieldInfo field)
{
if (!(field.IsPublic || field.IsFamily || field.IsFamilyOrAssembly)) return;
2008-11-17 18:29:00 -05:00
if (field.DeclaringType.IsEnum) {
2008-03-05 09:52:00 -05:00
if (!field.IsLiteral) return;
Write(field.Name);
Write(',');
}
2008-11-17 18:29:00 -05:00
else {
2008-03-05 09:52:00 -05:00
Write(field.IsPublic ? "public " : "protected ");
2008-11-17 18:29:00 -05:00
if (field.IsStatic && field.IsLiteral) {
2008-03-05 09:52:00 -05:00
Write("const ");
}
2008-11-17 18:29:00 -05:00
else {
2008-03-05 09:52:00 -05:00
if (field.IsStatic) Write("static ");
if (field.IsInitOnly) Write("readonly ");
}
WriteType(field.FieldType);
Write(' ');
Write(field.Name);
Write(';');
}
WriteMemberEnd(field);
}
void WriteIndent()
{
2008-11-17 18:29:00 -05:00
if (startOfLine) {
2008-03-05 09:52:00 -05:00
for (int i = 0; i < indent; i++) writer.Write(" ");
startOfLine = false;
}
}
void WriteLine()
{
writer.WriteLine();
startOfLine = true;
}
public void WriteMember(MemberInfo member)
{
2008-11-17 18:29:00 -05:00
switch (member.MemberType) {
2008-03-05 09:52:00 -05:00
case MemberTypes.Constructor:
WriteConstructor((ConstructorInfo)member);
break;
case MemberTypes.Field:
WriteField((FieldInfo)member);
break;
case MemberTypes.Property:
WriteProperty((PropertyInfo)member);
break;
case MemberTypes.Method:
WriteMethod((MethodInfo)member);
break;
case MemberTypes.Event:
WriteEvent((EventInfo)member);
break;
}
}
void WriteMemberEnd(MemberInfo member)
{
2008-11-17 18:29:00 -05:00
if (includeMemberKeys) {
2008-03-05 09:52:00 -05:00
Write(" // ");
Write(GetMemberKey(member));
}
WriteLine();
}
void WriteMethod(MethodInfo method)
{
if (!(method.IsPublic || method.IsFamily || method.IsFamilyOrAssembly)) return;
if (method.IsSpecialName && !opNames.Contains(method.Name)) return;
if (IsOverride(method)) Write(""); //return;
memberCt++;
Write(method.IsPublic ? "public " : "protected ");
if (method.IsStatic) Write("static ");
WriteMethodModifiers(method);
2008-11-17 18:29:00 -05:00
if (method.IsSpecialName) {
2008-03-05 09:52:00 -05:00
string op = (string)opNames[method.Name];
2008-11-17 18:29:00 -05:00
if (op == "explicit" || op == "implicit") {
2008-03-05 09:52:00 -05:00
Write(op);
Write(" operator ");
WriteType(method.ReturnType);
}
2008-11-17 18:29:00 -05:00
else {
2008-03-05 09:52:00 -05:00
WriteType(method.ReturnType);
Write(" operator ");
Write(op);
}
}
2008-11-17 18:29:00 -05:00
else {
2008-03-05 09:52:00 -05:00
WriteType(method.ReturnType);
Write(' ');
Write(method.Name);
}
#if GENERICS
2008-11-17 18:29:00 -05:00
if (method.IsGenericMethodDefinition) {
2008-03-05 09:52:00 -05:00
Write("<");
WriteTypeList(method.GetGenericArguments());
Write(">");
}
#endif
Write('(');
WriteParamList(method.GetParameters());
2008-11-17 18:29:00 -05:00
if (IsVarArgs(method)) {
2008-03-05 09:52:00 -05:00
Write(", __arglist");
}
Write(");");
WriteMemberEnd(method);
}
void WriteMethodModifiers(MethodInfo method)
{
2008-11-17 18:29:00 -05:00
if (method.DeclaringType.IsInterface) {
2008-03-05 09:52:00 -05:00
return;
}
MethodAttributes attrs = method.Attributes;
if ((attrs & MethodAttributes.Final) != 0 &&
(attrs & MethodAttributes.Virtual) != 0 &&
(attrs & MethodAttributes.NewSlot) != 0)
{
attrs = 0;
}
if ((attrs & MethodAttributes.Final) != 0) Write("sealed ");
2008-11-17 18:29:00 -05:00
if ((attrs & MethodAttributes.Virtual) != 0) {
switch (attrs & (MethodAttributes.NewSlot | MethodAttributes.Abstract)) {
2008-03-05 09:52:00 -05:00
case 0:
Write("override ");
break;
case MethodAttributes.NewSlot:
Write("virtual ");
break;
case MethodAttributes.Abstract:
Write("abstract override ");
break;
case MethodAttributes.NewSlot | MethodAttributes.Abstract:
Write("abstract ");
break;
}
}
}
void WriteParamList(ParameterInfo[] parameters)
{
2008-11-17 18:29:00 -05:00
for (int i = 0; i < parameters.Length; i++) {
2008-03-05 09:52:00 -05:00
ParameterInfo param = parameters[i];
if (i != 0) Write(", ");
Type type = param.ParameterType;
2008-11-17 18:29:00 -05:00
if (IsArray(type) && Attribute.IsDefined(param, typeof(ParamArrayAttribute), true)) {
2008-03-05 09:52:00 -05:00
Write("params ");
}
WriteType(type);
Write(' ');
Write(param.Name);
}
}
void WriteProperty(PropertyInfo property)
{
MethodInfo accessor = GetAccessor(property);
if (accessor == null) return;
if (!(accessor.IsPublic || accessor.IsFamily || accessor.IsFamilyOrAssembly)) return;
if (IsOverride(accessor)) return;
ParameterInfo[] parameters = property.GetIndexParameters();
memberCt++;
Write(accessor.IsPublic ? "public " : "protected ");
if (accessor.IsStatic) Write("static ");
WriteMethodModifiers(accessor);
WriteType(property.PropertyType);
Write(' ');
2008-11-17 18:29:00 -05:00
if (parameters.Length == 0) {
2008-03-05 09:52:00 -05:00
Write(property.Name);
}
2008-11-17 18:29:00 -05:00
else {
2008-03-05 09:52:00 -05:00
Write("this[");
WriteParamList(accessor.GetParameters());
Write("]");
}
Write(" { ");
if (property.CanRead) Write("get; ");
if (property.CanWrite) Write("set; ");
Write("}");
WriteMemberEnd(property);
}
void WriteType(Type type)
{
2008-11-17 18:29:00 -05:00
if (IsArray(type)) {
2008-03-05 09:52:00 -05:00
WriteType(type.GetElementType());
Write('[');
for (int i = type.GetArrayRank(); i > 1; i--) Write(',');
Write(']');
}
2008-11-17 18:29:00 -05:00
else if (type.IsByRef) {
2008-03-05 09:52:00 -05:00
Write("ref ");
WriteType(type.GetElementType());
}
2008-11-17 18:29:00 -05:00
else if (type.IsPointer) {
2008-03-05 09:52:00 -05:00
WriteType(type.GetElementType());
Write('*');
}
#if GENERICS
2008-11-17 18:29:00 -05:00
else if (type.HasGenericArguments) {
2008-03-05 09:52:00 -05:00
WriteSimpleTypeName(type);
Write("<");
WriteTypeList(type.GetGenericArguments());
Write(">");
}
#endif
2008-11-17 18:29:00 -05:00
else {
2008-03-05 09:52:00 -05:00
WriteSimpleTypeName(type);
}
}
private void WriteTypeList(Type[] types)
{
2008-11-17 18:29:00 -05:00
for (int i = 0; i < types.Length; i++) {
2008-03-05 09:52:00 -05:00
if (i != 0) Write(",");
WriteType(types[i]);
}
}
private void WriteSimpleTypeName(Type type)
{
string name = (string)typeNames[type];
if (name == null) name = type.Name;
Write(name);
}
public void WriteCustomAttribute(Attribute att)
{
Write("[");
Write(att.GetType().Name);
Write("(..)");
Write("]");
WriteLine();
}
public void WriteTypeDecl(Type type)
{
Attribute[] attrs = Attribute.GetCustomAttributes(type);
2008-11-17 18:29:00 -05:00
foreach (Attribute a in attrs) {
2008-03-05 09:52:00 -05:00
WriteCustomAttribute(a);
}
2008-11-17 18:29:00 -05:00
if (type.IsPublic || type.IsNestedPublic) {
2008-03-05 09:52:00 -05:00
Write("public ");
}
2008-11-17 18:29:00 -05:00
else if (type.IsNestedPrivate) {
2008-03-05 09:52:00 -05:00
Write("private ");
}
2008-11-17 18:29:00 -05:00
else if (type.IsNestedAssembly) {
2008-03-05 09:52:00 -05:00
Write("internal ");
}
2008-11-17 18:29:00 -05:00
else if (type.IsNestedFamily) {
2008-03-05 09:52:00 -05:00
Write("protected ");
}
2008-11-17 18:29:00 -05:00
else if (type.IsNestedFamORAssem) {
2008-03-05 09:52:00 -05:00
Write("protected internal ");
}
2008-11-17 18:29:00 -05:00
if (IsDelegate(type)) {
2008-03-05 09:52:00 -05:00
Write("delegate ");
MethodInfo method = type.GetMethod("Invoke");
WriteType(method.ReturnType);
Write(' ');
WriteTypeName(type);
Write('(');
WriteParamList(method.GetParameters());
Write(")");
WriteTypeConstraints(type);
Write(";");
}
2008-11-17 18:29:00 -05:00
else {
2008-03-05 09:52:00 -05:00
ArrayList list = new ArrayList();
AddMemberEntries(list, type, true);
2008-11-17 18:29:00 -05:00
if (type.IsEnum) {
2008-03-05 09:52:00 -05:00
Write("enum ");
WriteTypeName(type);
}
2008-11-17 18:29:00 -05:00
else {
2008-03-05 09:52:00 -05:00
bool colonWritten = false;
2008-11-17 18:29:00 -05:00
if (type.IsValueType) {
2008-03-05 09:52:00 -05:00
Write("struct ");
WriteTypeName(type);
}
2008-11-17 18:29:00 -05:00
else if (type.IsInterface) {
2008-03-05 09:52:00 -05:00
Write("interface ");
WriteTypeName(type);
}
2008-11-17 18:29:00 -05:00
else {
2008-03-05 09:52:00 -05:00
if (type.IsAbstract) Write("abstract ");
if (type.IsSealed) Write("sealed ");
Write("class ");
WriteTypeName(type);
Type baseType = type.BaseType;
2008-11-17 18:29:00 -05:00
if (baseType != null && baseType != typeof(object)) {
2008-03-05 09:52:00 -05:00
Write(": ");
Write(baseType.Name);
colonWritten = true;
}
}
2008-11-17 18:29:00 -05:00
foreach (string s in GetInterfaceNames(type)) {
2008-03-05 09:52:00 -05:00
Write(colonWritten ? ", " : ": ");
Write(s);
colonWritten = true;
}
}
WriteTypeConstraints(type);
WriteLine();
Write('{');
WriteLine();
list.Sort();
indent++;
2008-11-17 18:29:00 -05:00
foreach (MemberEntry entry in list) {
2008-03-05 09:52:00 -05:00
if (!entry.IsSecured)
WriteMember(entry.Member);
}
indent--;
Write('}');
}
WriteLine();
WriteLine();
}
void WriteTypeName(Type type)
{
Write(fullTypeNames ? type.FullName : type.Name);
#if GENERICS
2008-11-17 18:29:00 -05:00
if (type.IsGenericTypeDefinition) {
2008-03-05 09:52:00 -05:00
Write("<");
WriteTypeList(type.GetGenericArguments());
Write(">");
}
#endif
}
void WriteTypeConstraints(Type type)
{
#if GENERICS
2008-11-17 18:29:00 -05:00
foreach (Type t in type.GetGenericArguments()) {
2008-03-05 09:52:00 -05:00
Type[] arr = t.GetInterfaces();
object[] arr2 = t.GetCustomAttributes(true);
2008-11-17 18:29:00 -05:00
if (t.BaseType != typeof(object) || arr.Length + arr2.Length > 0) {
2008-03-05 09:52:00 -05:00
Write(" where ");
WriteTypeName(t);
Write(":");
}
2008-11-17 18:29:00 -05:00
if (t.BaseType != typeof(Object)) {
2008-03-05 09:52:00 -05:00
WriteTypeName(t.BaseType);
if (arr.Length + arr2.Length > 0) Write(",");
}
2008-11-17 18:29:00 -05:00
for (int i = 0; i < arr.Length; i++) {
2008-03-05 09:52:00 -05:00
WriteTypeName(arr[i]);
if (i < arr.Length-1 || arr2.Length>0) Write(",");
}
2008-11-17 18:29:00 -05:00
for (int i = 0; i < arr2.Length; i++) {
if (arr2[i].GetType() == typeof(System.Runtime.CompilerServices.NewConstraintAttribute)) {
2008-03-05 09:52:00 -05:00
Write("new()");
}
2008-11-17 18:29:00 -05:00
else {
2008-03-05 09:52:00 -05:00
Write(arr2[i].ToString());
}
if (i < arr.Length - 1) Write(",");
}
}
#endif
}
class MemberEntry : IComparable
{
MemberInfo member;
string sortKey;
bool secured = false;
public MemberEntry(MemberInfo member)
{
this.member = member;
2008-11-17 18:29:00 -05:00
try {
2008-03-05 09:52:00 -05:00
StringBuilder sb = new StringBuilder();
2008-11-17 18:29:00 -05:00
switch (member.MemberType) {
2008-03-05 09:52:00 -05:00
case MemberTypes.Constructor:
sb.Append('A');
AppendParams(sb, ((ConstructorInfo)member).GetParameters());
break;
case MemberTypes.Field:
sb.Append('B');
sb.Append(member.Name);
break;
case MemberTypes.Property:
ParameterInfo[] parameters = ((PropertyInfo)member).GetIndexParameters();
2008-11-17 18:29:00 -05:00
if (parameters.Length == 0) {
2008-03-05 09:52:00 -05:00
sb.Append('C');
sb.Append(member.Name);
}
2008-11-17 18:29:00 -05:00
else {
2008-03-05 09:52:00 -05:00
sb.Append('D');
AppendParams(sb, parameters);
}
break;
case MemberTypes.Method:
2008-11-17 18:29:00 -05:00
if (((MethodInfo)member).IsSpecialName && opSortKeys.Contains(member.Name)) {
2008-03-05 09:52:00 -05:00
sb.Append('F');
sb.Append((string)opSortKeys[member.Name]);
2008-11-17 18:29:00 -05:00
if (member.Name == "op_Implicit" || member.Name == "op_Explicit") {
2008-03-05 09:52:00 -05:00
AppendType(sb, ((MethodInfo)member).ReturnType);
}
}
2008-11-17 18:29:00 -05:00
else {
2008-03-05 09:52:00 -05:00
sb.Append('E');
sb.Append(member.Name);
}
AppendParams(sb, ((MethodInfo)member).GetParameters());
break;
case MemberTypes.Event:
sb.Append('G');
sb.Append(member.Name);
((EventInfo)member).GetAddMethod(); // Force strong name security exception here
break;
}
sortKey = sb.ToString();
}
2008-11-17 18:29:00 -05:00
catch (System.Security.SecurityException) {
2008-03-05 09:52:00 -05:00
secured = true;
}
}
public MemberInfo Member
{
get { return member; }
}
public bool IsSecured
{
get { return secured; }
}
int IComparable.CompareTo(object other)
{
if (sortKey == null)
return -1;
return sortKey.CompareTo(((MemberEntry)other).sortKey);
}
static void AppendParams(StringBuilder sb, ParameterInfo[] parameters)
{
2008-11-17 18:29:00 -05:00
foreach (ParameterInfo param in parameters) {
2008-03-05 09:52:00 -05:00
AppendType(sb, param.ParameterType);
}
}
static void AppendType(StringBuilder sb, Type type)
{
2008-11-17 18:29:00 -05:00
if (IsArray(type)) {
2008-03-05 09:52:00 -05:00
AppendType(sb, type.GetElementType());
sb.Append("[]");
}
2008-11-17 18:29:00 -05:00
else if (type.IsByRef) {
2008-03-05 09:52:00 -05:00
AppendType(sb, type.GetElementType());
sb.Append("@");
}
2008-11-17 18:29:00 -05:00
else if (type.IsPointer) {
2008-03-05 09:52:00 -05:00
AppendType(sb, type.GetElementType());
sb.Append("*");
}
2008-11-17 18:29:00 -05:00
else {
2008-03-05 09:52:00 -05:00
sb.Append(',');
string name = (string)typeNames[type];
if (name == null) name = type.Name;
sb.Append(name);
}
}
}
}
class TXRef
{
static void Main(string[] args)
{
#if GENERICS
System.Diagnostics.Process.GetCurrentProcess().PriorityClass = System.Diagnostics.ProcessPriorityClass.Idle;
#endif
TypeWriter writer = new TypeWriter(Console.Out);
2008-11-17 18:29:00 -05:00
foreach (string s in args) {
2008-03-05 09:52:00 -05:00
Assembly a = null;
2008-11-17 18:29:00 -05:00
try {
2008-03-05 09:52:00 -05:00
a = Assembly.LoadFrom(s);
if (a == null)
a = Assembly.Load(s);
}
2008-11-17 18:29:00 -05:00
catch (Exception e) {
Console.WriteLine(e);
}
if (a == null) {
2008-03-05 09:52:00 -05:00
Console.WriteLine("TxRef: could not load assembly '{0}'.", s);
return;
}
ArrayList l = new ArrayList();
Type[] types;
2008-11-17 18:29:00 -05:00
try {
2008-03-05 09:52:00 -05:00
types = a.GetTypes();
}
2008-11-17 18:29:00 -05:00
catch (ReflectionTypeLoadException re) {
foreach (Exception e in re.LoaderExceptions) {
2008-03-05 09:52:00 -05:00
Console.WriteLine(e);
}
types = RemoveNulls(re.Types);
}
Array.Sort(types, new TypeComparer());
2008-11-17 18:29:00 -05:00
foreach (Type t in types) {
if (t.IsPublic) {
2008-03-05 09:52:00 -05:00
if (!l.Contains(t.Namespace))
l.Add(t.Namespace);
}
}
l.Sort(null);
2008-11-17 18:29:00 -05:00
foreach (string namespaceName in l) {
2008-03-05 09:52:00 -05:00
writer.WriteNamespace(namespaceName, types);
}
}
}
static Type[] RemoveNulls(Type[] types)
{
ArrayList l = new ArrayList();
2008-11-17 18:29:00 -05:00
foreach (Type t in types) {
2008-03-05 09:52:00 -05:00
if (t != null) l.Add(t);
}
return (Type[])l.ToArray(typeof(Type));
}
}
public class TypeComparer : IComparer
{
internal enum TypeGroup {
MainGroup = 0,
Enums = 1,
Collections = 2,
DelegatesAndEventArgs = 3,
Attributes = 4,
Exceptions = 5
}
internal static TypeGroup GetGroupForType(Type type) {
if (type.IsEnum) return TypeGroup.Enums;
if ((typeof(IEnumerable).IsAssignableFrom(type))) return TypeGroup.Collections;
if (type.IsSubclassOf(typeof(Delegate)) || type.IsSubclassOf(typeof(EventArgs))) return TypeGroup.DelegatesAndEventArgs;
if (type.IsSubclassOf(typeof(Attribute))) return TypeGroup.Attributes;
if (type.IsSubclassOf(typeof(Exception))) return TypeGroup.Exceptions;
return TypeGroup.MainGroup;
}
public int Compare(object obj1, object obj2)
{
Type t1 = obj1 as Type;
Type t2 = obj2 as Type;
// these are used to group by (for example all enums together)
TypeGroup t1Group = TypeComparer.GetGroupForType(t1);
TypeGroup t2Group = TypeComparer.GetGroupForType(t2);
if (t2Group != t1Group) return ((int)t1Group) < ((int)t2Group) ? -1 : 1;
return t1.Name.CompareTo(t2.Name);
}
}