You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.9 KiB

15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
  1. // Copyright (c) 2011 AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.Reflection.Metadata;
  20. using System.Windows.Media;
  21. using ICSharpCode.Decompiler;
  22. namespace ICSharpCode.ILSpy.TreeNodes
  23. {
  24. using ICSharpCode.Decompiler.TypeSystem;
  25. /// <summary>
  26. /// Represents a field in the TreeView.
  27. /// </summary>
  28. public sealed class FieldTreeNode : ILSpyTreeNode, IMemberTreeNode
  29. {
  30. public IField FieldDefinition { get; }
  31. public FieldTreeNode(IField field)
  32. {
  33. this.FieldDefinition = field ?? throw new ArgumentNullException(nameof(field));
  34. }
  35. public override object Text => GetText(GetFieldDefinition(), Language) + FieldDefinition.MetadataToken.ToSuffixString();
  36. private IField GetFieldDefinition()
  37. {
  38. return ((MetadataModule)FieldDefinition.ParentModule.PEFile
  39. ?.GetTypeSystemWithCurrentOptionsOrNull()
  40. ?.MainModule)?.GetDefinition((FieldDefinitionHandle)FieldDefinition.MetadataToken) ?? FieldDefinition;
  41. }
  42. public static object GetText(IField field, Language language)
  43. {
  44. return language.FieldToString(field, includeDeclaringTypeName: false, includeNamespace: false, includeNamespaceOfDeclaringTypeName: false);
  45. }
  46. public override object Icon => GetIcon(GetFieldDefinition());
  47. public static ImageSource GetIcon(IField field)
  48. {
  49. if (field.DeclaringType.Kind == TypeKind.Enum && field.ReturnType.Kind == TypeKind.Enum)
  50. return Images.GetIcon(MemberIcon.EnumValue, MethodTreeNode.GetOverlayIcon(field.Accessibility), false);
  51. if (field.IsConst)
  52. return Images.GetIcon(MemberIcon.Literal, MethodTreeNode.GetOverlayIcon(field.Accessibility), false);
  53. if (field.IsReadOnly)
  54. return Images.GetIcon(MemberIcon.FieldReadOnly, MethodTreeNode.GetOverlayIcon(field.Accessibility), field.IsStatic);
  55. return Images.GetIcon(MemberIcon.Field, MethodTreeNode.GetOverlayIcon(field.Accessibility), field.IsStatic);
  56. }
  57. public override FilterResult Filter(FilterSettings settings)
  58. {
  59. if (settings.ShowApiLevel == ApiVisibility.PublicOnly && !IsPublicAPI)
  60. return FilterResult.Hidden;
  61. if (settings.SearchTermMatches(FieldDefinition.Name) && (settings.ShowApiLevel == ApiVisibility.All || settings.Language.ShowMember(FieldDefinition)))
  62. return FilterResult.Match;
  63. else
  64. return FilterResult.Hidden;
  65. }
  66. public override void Decompile(Language language, ITextOutput output, DecompilationOptions options)
  67. {
  68. language.DecompileField(FieldDefinition, output, options);
  69. }
  70. public override bool IsPublicAPI {
  71. get {
  72. switch (GetFieldDefinition().Accessibility)
  73. {
  74. case Accessibility.Public:
  75. case Accessibility.Protected:
  76. case Accessibility.ProtectedOrInternal:
  77. return true;
  78. default:
  79. return false;
  80. }
  81. }
  82. }
  83. IEntity IMemberTreeNode.Member => FieldDefinition;
  84. public override string ToString()
  85. {
  86. return FieldDefinition.Name;
  87. }
  88. }
  89. }