/ILSpy/TreeNodes/FieldTreeNode.cs

http://github.com/icsharpcode/ILSpy · C# · 96 lines · 61 code · 15 blank · 20 comment · 14 complexity · c6b323854c6b898108e2cb56ce1108d0 MD5 · raw file

  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.Windows.Media;
  20. using ICSharpCode.Decompiler;
  21. using ICSharpCode.Decompiler.TypeSystem;
  22. namespace ICSharpCode.ILSpy.TreeNodes
  23. {
  24. /// <summary>
  25. /// Represents a field in the TreeView.
  26. /// </summary>
  27. public sealed class FieldTreeNode : ILSpyTreeNode, IMemberTreeNode
  28. {
  29. public IField FieldDefinition { get; }
  30. public FieldTreeNode(IField field)
  31. {
  32. this.FieldDefinition = field ?? throw new ArgumentNullException(nameof(field));
  33. }
  34. public override object Text => GetText(FieldDefinition, Language) + FieldDefinition.MetadataToken.ToSuffixString();
  35. public static object GetText(IField field, Language language)
  36. {
  37. return language.FieldToString(field, includeDeclaringTypeName: false, includeNamespace: false, includeNamespaceOfDeclaringTypeName: false);
  38. }
  39. public override object Icon => GetIcon(FieldDefinition);
  40. public static ImageSource GetIcon(IField field)
  41. {
  42. if (field.DeclaringType.Kind == TypeKind.Enum && field.ReturnType.Kind == TypeKind.Enum)
  43. return Images.GetIcon(MemberIcon.EnumValue, MethodTreeNode.GetOverlayIcon(field.Accessibility), false);
  44. if (field.IsConst)
  45. return Images.GetIcon(MemberIcon.Literal, MethodTreeNode.GetOverlayIcon(field.Accessibility), false);
  46. if (field.IsReadOnly)
  47. return Images.GetIcon(MemberIcon.FieldReadOnly, MethodTreeNode.GetOverlayIcon(field.Accessibility), field.IsStatic);
  48. return Images.GetIcon(MemberIcon.Field, MethodTreeNode.GetOverlayIcon(field.Accessibility), field.IsStatic);
  49. }
  50. public override FilterResult Filter(FilterSettings settings)
  51. {
  52. if (settings.ShowApiLevel == ApiVisibility.PublicOnly && !IsPublicAPI)
  53. return FilterResult.Hidden;
  54. if (settings.SearchTermMatches(FieldDefinition.Name) && (settings.ShowApiLevel == ApiVisibility.All || settings.Language.ShowMember(FieldDefinition)))
  55. return FilterResult.Match;
  56. else
  57. return FilterResult.Hidden;
  58. }
  59. public override void Decompile(Language language, ITextOutput output, DecompilationOptions options)
  60. {
  61. language.DecompileField(FieldDefinition, output, options);
  62. }
  63. public override bool IsPublicAPI {
  64. get {
  65. switch (FieldDefinition.Accessibility) {
  66. case Accessibility.Public:
  67. case Accessibility.Protected:
  68. case Accessibility.ProtectedOrInternal:
  69. return true;
  70. default:
  71. return false;
  72. }
  73. }
  74. }
  75. IEntity IMemberTreeNode.Member => FieldDefinition;
  76. public override string ToString()
  77. {
  78. return FieldDefinition.Name;
  79. }
  80. }
  81. }