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 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
|
//
// outline -- support for rendering in monop
// Some code stolen from updater.cs in monodoc.
//
// Authors:
// Ben Maurer (bmaurer@users.sourceforge.net)
//
// (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.
//
#if !NET_2_1
using System;
using System.Reflection;
using System.Collections;
using System.CodeDom.Compiler;
using System.IO;
using System.Text;
namespace Mono.CSharp {
public class Outline {
bool declared_only;
bool show_private;
bool filter_obsolete;
IndentedTextWriter o;
Type t;
public Outline (Type t, TextWriter output, bool declared_only, bool show_private, bool filter_obsolete)
{
this.t = t;
this.o = new IndentedTextWriter (output, "\t");
this.declared_only = declared_only;
this.show_private = show_private;
this.filter_obsolete = filter_obsolete;
}
public void OutlineType ()
{
bool first;
OutlineAttributes ();
o.Write (GetTypeVisibility (t));
if (t.IsClass && !t.IsSubclassOf (typeof (System.MulticastDelegate))) {
if (t.IsSealed)
o.Write (t.IsAbstract ? " static" : " sealed");
else if (t.IsAbstract)
o.Write (" abstract");
}
o.Write (" ");
o.Write (GetTypeKind (t));
o.Write (" ");
Type [] interfaces = (Type []) Comparer.Sort (TypeGetInterfaces (t, declared_only));
Type parent = t.BaseType;
if (t.IsSubclassOf (typeof (System.MulticastDelegate))) {
MethodInfo method;
method = t.GetMethod ("Invoke");
o.Write (FormatType (method.ReturnType));
o.Write (" ");
o.Write (GetTypeName (t));
o.Write (" (");
OutlineParams (method.GetParameters ());
o.Write (")");
WriteGenericConstraints (t.GetGenericArguments ());
o.WriteLine (";");
return;
}
o.Write (GetTypeName (t));
if (((parent != null && parent != typeof (object) && parent != typeof (ValueType)) || interfaces.Length != 0) && ! t.IsEnum) {
first = true;
o.Write (" : ");
if (parent != null && parent != typeof (object) && parent != typeof (ValueType)) {
o.Write (FormatType (parent));
first = false;
}
foreach (Type intf in interfaces) {
if (!first) o.Write (", ");
first = false;
o.Write (FormatType (intf));
}
}
if (t.IsEnum) {
Type underlyingType = System.Enum.GetUnderlyingType (t);
if (underlyingType != typeof (int))
o.Write (" : {0}", FormatType (underlyingType));
}
WriteGenericConstraints (t.GetGenericArguments ());
o.WriteLine (" {");
o.Indent++;
if (t.IsEnum) {
bool is_first = true;
foreach (FieldInfo fi in t.GetFields (BindingFlags.Public | BindingFlags.Static)) {
if (! is_first)
o.WriteLine (",");
is_first = false;
o.Write (fi.Name);
}
o.WriteLine ();
o.Indent--; o.WriteLine ("}");
return;
}
first = true;
foreach (ConstructorInfo ci in t.GetConstructors (DefaultFlags)) {
if (! ShowMember (ci))
continue;
if (first)
o.WriteLine ();
first = false;
OutlineMemberAttribute (ci);
OutlineConstructor (ci);
o.WriteLine ();
}
first = true;
foreach (MethodInfo m in Comparer.Sort (t.GetMethods (DefaultFlags))) {
if (! ShowMember (m))
continue;
if ((m.Attributes & MethodAttributes.SpecialName) != 0)
continue;
if (first)
o.WriteLine ();
first = false;
OutlineMemberAttribute (m);
OutlineMethod (m);
o.WriteLine ();
}
first = true;
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;
OutlineMemberAttribute (m);
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 = false;
OutlineMemberAttribute (pi);
OutlineProperty (pi);
o.WriteLine ();
}
first = true;
foreach (FieldInfo fi in t.GetFields (DefaultFlags)) {
if (! ShowMember (fi))
continue;
if (first)
o.WriteLine ();
first = false;
OutlineMemberAttribute (fi);
OutlineField (fi);
o.WriteLine ();
}
first = true;
foreach (EventInfo ei in Comparer.Sort (t.GetEvents (DefaultFlags))) {
if (! ShowMember (ei.GetAddMethod (true)))
continue;
if (first)
o.WriteLine ();
first = false;
OutlineMemberAttribute (ei);
OutlineEvent (ei);
o.WriteLine ();
}
first = true;
foreach (Type ntype in Comparer.Sort (t.GetNestedTypes (DefaultFlags))) {
if (! ShowMember (ntype))
continue;
if (first)
o.WriteLine ();
first = false;
new Outline (ntype, o, declared_only, show_private, filter_obsolete).OutlineType ();
}
o.Indent--; o.WriteLine ("}");
}
BindingFlags DefaultFlags {
get {
BindingFlags f = BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic;
if (declared_only)
f |= BindingFlags.DeclaredOnly;
return f;
}
}
// FIXME: add other interesting attributes?
void OutlineAttributes ()
{
if (t.IsSerializable)
o.WriteLine ("[Serializable]");
if (t.IsDefined (typeof (System.FlagsAttribute), true))
o.WriteLine ("[Flags]");
if (t.IsDefined (typeof (System.ObsoleteAttribute), true))
o.WriteLine ("[Obsolete]");
}
void OutlineMemberAttribute (MemberInfo mi)
{
if (!mi.IsDefined (typeof (System.ObsoleteAttribute), false))
return;
var oa = mi.GetCustomAttributes (typeof (System.ObsoleteAttribute), false) [0] as ObsoleteAttribute;
var msg = oa.Message;
o.WriteLine ("[Obsolete{0}]", msg == null || msg == "" ? "" : string.Format ("(\"{0}\")", msg));
}
void OutlineEvent (EventInfo ei)
{
MethodBase accessor = ei.GetAddMethod (true);
o.Write (GetMethodVisibility (accessor));
o.Write ("event ");
o.Write (FormatType (ei.EventHandlerType));
o.Write (" ");
o.Write (ei.Name);
o.Write (";");
}
void OutlineConstructor (ConstructorInfo ci)
{
o.Write (GetMethodVisibility (ci));
o.Write (RemoveGenericArity (t.Name));
o.Write (" (");
OutlineParams (ci.GetParameters ());
o.Write (");");
}
void OutlineProperty (PropertyInfo pi)
{
ParameterInfo [] idxp = pi.GetIndexParameters ();
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.Write (FormatType (pi.PropertyType));
o.Write (" ");
if (idxp.Length == 0)
o.Write (pi.Name);
else {
o.Write ("this [");
OutlineParams (idxp);
o.Write ("]");
}
o.WriteLine (" {");
o.Indent ++;
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 ("}");
}
void OutlineMethod (MethodInfo mi)
{
if (MethodIsExplicitIfaceImpl (mi)) {
o.Write (FormatType (mi.ReturnType));
o.Write (" ");
// MSFT has no way to get the method that we are overriding
// from the interface. this would allow us to pretty print
// the type name (and be more correct if there compiler
// were to do some strange naming thing).
} else {
o.Write (GetMethodVisibility (mi));
o.Write (GetMethodModifiers (mi));
o.Write (FormatType (mi.ReturnType));
o.Write (" ");
}
o.Write (mi.Name);
o.Write (FormatGenericParams (mi.GetGenericArguments ()));
o.Write (" (");
OutlineParams (mi.GetParameters ());
o.Write (")");
WriteGenericConstraints (mi.GetGenericArguments ());
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 (");");
}
void OutlineParams (ParameterInfo [] pi)
{
int i = 0;
foreach (ParameterInfo p in pi) {
if (p.ParameterType.IsByRef) {
o.Write (p.IsOut ? "out " : "ref ");
o.Write (FormatType (p.ParameterType.GetElementType ()));
} else if (p.IsDefined (typeof (ParamArrayAttribute), false)) {
o.Write ("params ");
o.Write (FormatType (p.ParameterType));
} else {
o.Write (FormatType (p.ParameterType));
}
o.Write (" ");
o.Write (p.Name);
if (i + 1 < pi.Length)
o.Write (", ");
i++;
}
}
void OutlineField (FieldInfo fi)
{
if (fi.IsPublic) o.Write ("public ");
if (fi.IsFamily) o.Write ("protected ");
if (fi.IsPrivate) o.Write ("private ");
if (fi.IsAssembly) o.Write ("internal ");
if (fi.IsLiteral) o.Write ("const ");
else if (fi.IsStatic) o.Write ("static ");
if (fi.IsInitOnly) o.Write ("readonly ");
o.Write (FormatType (fi.FieldType));
o.Write (" ");
o.Write (fi.Name);
if (fi.IsLiteral) {
object v = fi.GetValue (this);
// TODO: Escape values here
o.Write (" = ");
if (v is char)
o.Write ("'{0}'", v);
else if (v is string)
o.Write ("\"{0}\"", v);
else
o.Write (fi.GetValue (this));
}
o.Write (";");
}
static string GetMethodVisibility (MethodBase m)
{
// itnerfaces have no modifiers here
if (m.DeclaringType.IsInterface)
return "";
if (m.IsPublic) return "public ";
if (m.IsFamily) return "protected ";
if (m.IsPrivate) return "private ";
if (m.IsAssembly) return "internal ";
return null;
}
static string GetMethodModifiers (MethodBase method)
{
if (method.IsStatic)
return "static ";
if (method.IsFinal) {
// This will happen if you have
// class X : IA {
// public void A () {}
// static void Main () {}
// }
// interface IA {
// void A ();
// }
//
// A needs to be virtual (the CLR requires
// methods implementing an iface be virtual),
// but can not be inherited. It also can not
// be inherited. In C# this is represented
// with no special modifiers
if (method.IsVirtual)
return null;
return "sealed ";
}
// all interface methods are "virtual" but we don't say that in c#
if (method.IsVirtual && !method.DeclaringType.IsInterface) {
if (method.IsAbstract)
return "abstract ";
return ((method.Attributes & MethodAttributes.NewSlot) != 0) ?
"virtual " :
"override ";
}
return null;
}
static string GetTypeKind (Type t)
{
if (t.IsEnum)
return "enum";
if (t.IsClass) {
if (t.IsSubclassOf (typeof (System.MulticastDelegate)))
return "delegate";
else
return "class";
}
if (t.IsInterface)
return "interface";
if (t.IsValueType)
return "struct";
return "class";
}
static string GetTypeVisibility (Type t)
{
switch (t.Attributes & TypeAttributes.VisibilityMask){
case TypeAttributes.Public:
case TypeAttributes.NestedPublic:
return "public";
case TypeAttributes.NestedFamily:
case TypeAttributes.NestedFamANDAssem:
case TypeAttributes.NestedFamORAssem:
return "protected";
default:
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 ();
}
// TODO: fine tune this so that our output is less verbose. We need to figure
// out a way to do this while not making things confusing.
string FormatType (Type t)
{
if (t == null)
return "";
string type = GetFullName (t);
if (type == null)
return t.ToString ();
if (!type.StartsWith ("System.")) {
if (t.Namespace == this.t.Namespace)
return t.Name;
return type;
}
if (t.HasElementType) {
Type et = t.GetElementType ();
if (t.IsArray)
return FormatType (et) + " []";
if (t.IsPointer)
return FormatType (et) + " *";
if (t.IsByRef)
return "ref " + FormatType (et);
}
switch (type) {
case "System.Byte": return "byte";
case "System.SByte": return "sbyte";
case "System.Int16": return "short";
case "System.Int32": return "int";
case "System.Int64": return "long";
case "System.UInt16": return "ushort";
case "System.UInt32": return "uint";
case "System.UInt64": return "ulong";
case "System.Single": return "float";
case "System.Double": return "double";
case "System.Decimal": return "decimal";
case "System.Boolean": return "bool";
case "System.Char": return "char";
case "System.String": return "string";
case "System.Object": return "object";
case "System.Void": return "void";
}
if (type.LastIndexOf(".") == 6)
return type.Substring(7);
//
// If the namespace of the type is the namespace of what
// we are printing (or is a member of one if its children
// don't print it. This basically means that in C# we would
// automatically get the namespace imported by virtue of the
// namespace {} block.
//
if (this.t.Namespace.StartsWith (t.Namespace + ".") || t.Namespace == this.t.Namespace)
return type.Substring (t.Namespace.Length + 1);
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));
sb.Append (FormatGenericParams (t.GetGenericArguments ()));
}
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 (t.IsGenericParameter) {
sb.Append (t.Name);
return;
}
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);
}
void WriteGenericConstraints (Type [] args)
{
foreach (Type t in args) {
bool first = true;
Type[] ifaces = TypeGetInterfaces (t, true);
GenericParameterAttributes attrs = t.GenericParameterAttributes & GenericParameterAttributes.SpecialConstraintMask;
GenericParameterAttributes [] interesting = {
GenericParameterAttributes.ReferenceTypeConstraint,
GenericParameterAttributes.NotNullableValueTypeConstraint,
GenericParameterAttributes.DefaultConstructorConstraint
};
if (t.BaseType != typeof (object) || ifaces.Length != 0 || attrs != 0) {
o.Write (" where ");
o.Write (FormatType (t));
o.Write (" : ");
}
if (t.BaseType != typeof (object)) {
o.Write (FormatType (t.BaseType));
first = false;
}
foreach (Type iface in ifaces) {
if (!first)
o.Write (", ");
first = false;
o.Write (FormatType (iface));
}
foreach (GenericParameterAttributes a in interesting) {
if ((attrs & a) == 0)
continue;
if (!first)
o.Write (", ");
first = false;
switch (a) {
case GenericParameterAttributes.ReferenceTypeConstraint:
o.Write ("class");
break;
case GenericParameterAttributes.NotNullableValueTypeConstraint:
o.Write ("struct");
break;
case GenericParameterAttributes.DefaultConstructorConstraint:
o.Write ("new ()");
break;
}
}
}
}
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 MethodIsExplicitIfaceImpl (MethodBase mb)
{
if (!(mb.IsFinal && mb.IsVirtual && mb.IsPrivate))
return false;
// UGH msft has no way to get the info about what method is
// getting overriden. Another reason to use cecil :-)
//
//MethodInfo mi = mb as MethodInfo;
//if (mi == null)
// return false;
//
//Console.WriteLine (mi.GetBaseDefinition ().DeclaringType);
//return mi.GetBaseDefinition ().DeclaringType.IsInterface;
// So, we guess that virtual final private methods only come
// from ifaces :-)
return true;
}
bool ShowMember (MemberInfo mi)
{
if (mi.MemberType == MemberTypes.Constructor && ((MethodBase) mi).IsStatic)
return false;
if (show_private)
return true;
if (filter_obsolete && mi.IsDefined (typeof (ObsoleteAttribute), false))
return false;
switch (mi.MemberType) {
case MemberTypes.Constructor:
case MemberTypes.Method:
MethodBase mb = mi as MethodBase;
if (mb.IsFamily || mb.IsPublic || mb.IsFamilyOrAssembly)
return true;
if (MethodIsExplicitIfaceImpl (mb))
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;
}
static Type [] TypeGetInterfaces (Type t, bool declonly)
{
if (t.IsGenericParameter)
return new Type [0];
Type [] ifaces = t.GetInterfaces ();
if (! declonly)
return ifaces;
// Handle Object. Also, optimize for no interfaces
if (t.BaseType == null || ifaces.Length == 0)
return ifaces;
ArrayList ar = new ArrayList ();
foreach (Type i in ifaces)
if (! i.IsAssignableFrom (t.BaseType))
ar.Add (i);
return (Type []) ar.ToArray (typeof (Type));
}
}
public class Comparer : IComparer {
delegate int ComparerFunc (object a, object b);
ComparerFunc cmp;
Comparer (ComparerFunc f)
{
this.cmp = f;
}
public int Compare (object a, object b)
{
return cmp (a, b);
}
static int CompareType (object a, object b)
{
Type type1 = (Type) a;
Type type2 = (Type) b;
if (type1.IsSubclassOf (typeof (System.MulticastDelegate)) != type2.IsSubclassOf (typeof (System.MulticastDelegate)))
return (type1.IsSubclassOf (typeof (System.MulticastDelegate)))? -1:1;
return string.Compare (type1.Name, type2.Name);
}
// static Comparer TypeComparer = new Comparer (new ComparerFunc (CompareType));
// static Type [] Sort (Type [] types)
// {
// Array.Sort (types, TypeComparer);
// return types;
// }
static int CompareMemberInfo (object a, object b)
{
return string.Compare (((MemberInfo) a).Name, ((MemberInfo) b).Name);
}
static Comparer MemberInfoComparer = new Comparer (new ComparerFunc (CompareMemberInfo));
public static MemberInfo [] Sort (MemberInfo [] inf)
{
Array.Sort (inf, MemberInfoComparer);
return inf;
}
static int CompareMethodBase (object a, object b)
{
MethodBase aa = (MethodBase) a, bb = (MethodBase) b;
if (aa.IsStatic == bb.IsStatic) {
int c = CompareMemberInfo (a, b);
if (c != 0)
return c;
ParameterInfo [] ap, bp;
//
// Sort overloads by the names of their types
// put methods with fewer params first.
//
ap = aa.GetParameters ();
bp = bb.GetParameters ();
int n = System.Math.Min (ap.Length, bp.Length);
for (int i = 0; i < n; i ++)
if ((c = CompareType (ap [i].ParameterType, bp [i].ParameterType)) != 0)
return c;
return ap.Length.CompareTo (bp.Length);
}
if (aa.IsStatic)
return -1;
return 1;
}
static Comparer MethodBaseComparer = new Comparer (new ComparerFunc (CompareMethodBase));
public static MethodBase [] Sort (MethodBase [] inf)
{
Array.Sort (inf, MethodBaseComparer);
return inf;
}
static int ComparePropertyInfo (object a, object b)
{
PropertyInfo aa = (PropertyInfo) a, bb = (PropertyInfo) b;
bool astatic = (aa.CanRead ? aa.GetGetMethod (true) : aa.GetSetMethod (true)).IsStatic;
bool bstatic = (bb.CanRead ? bb.GetGetMethod (true) : bb.GetSetMethod (true)).IsStatic;
if (astatic == bstatic)
return CompareMemberInfo (a, b);
if (astatic)
return -1;
return 1;
}
static Comparer PropertyInfoComparer = new Comparer (new ComparerFunc (ComparePropertyInfo));
public static PropertyInfo [] Sort (PropertyInfo [] inf)
{
Array.Sort (inf, PropertyInfoComparer);
return inf;
}
static int CompareEventInfo (object a, object b)
{
EventInfo aa = (EventInfo) a, bb = (EventInfo) b;
bool astatic = aa.GetAddMethod (true).IsStatic;
bool bstatic = bb.GetAddMethod (true).IsStatic;
if (astatic == bstatic)
return CompareMemberInfo (a, b);
if (astatic)
return -1;
return 1;
}
static Comparer EventInfoComparer = new Comparer (new ComparerFunc (CompareEventInfo));
public static EventInfo [] Sort (EventInfo [] inf)
{
Array.Sort (inf, EventInfoComparer);
return inf;
}
}
}
#endif
|