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.

103 lines
3.2 KiB

  1. // Copyright (c) 2015 Siegfried Pammer
  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.Collections.Generic;
  20. using ICSharpCode.Decompiler.Documentation;
  21. using ICSharpCode.Decompiler.TypeSystem;
  22. namespace ICSharpCode.Decompiler
  23. {
  24. public static class NRExtensions
  25. {
  26. public static bool IsCompilerGenerated(this IEntity entity)
  27. {
  28. if (entity != null)
  29. {
  30. return entity.HasAttribute(KnownAttribute.CompilerGenerated);
  31. }
  32. return false;
  33. }
  34. public static bool IsCompilerGeneratedOrIsInCompilerGeneratedClass(this IEntity entity)
  35. {
  36. if (entity == null)
  37. return false;
  38. if (entity.IsCompilerGenerated())
  39. return true;
  40. return IsCompilerGeneratedOrIsInCompilerGeneratedClass(entity.DeclaringTypeDefinition);
  41. }
  42. public static bool HasGeneratedName(this IMember member)
  43. {
  44. return member.Name.StartsWith("<", StringComparison.Ordinal);
  45. }
  46. public static bool HasGeneratedName(this IType type)
  47. {
  48. return type.Name.StartsWith("<", StringComparison.Ordinal) || type.Name.Contains("<");
  49. }
  50. public static bool IsAnonymousType(this IType type)
  51. {
  52. if (type == null)
  53. return false;
  54. if (string.IsNullOrEmpty(type.Namespace) && type.HasGeneratedName()
  55. && (type.Name.Contains("AnonType") || type.Name.Contains("AnonymousType")))
  56. {
  57. ITypeDefinition td = type.GetDefinition();
  58. return td != null && td.IsCompilerGenerated();
  59. }
  60. return false;
  61. }
  62. public static bool ContainsAnonymousType(this IType type)
  63. {
  64. var visitor = new ContainsAnonTypeVisitor();
  65. type.AcceptVisitor(visitor);
  66. return visitor.ContainsAnonType;
  67. }
  68. class ContainsAnonTypeVisitor : TypeVisitor
  69. {
  70. public bool ContainsAnonType;
  71. public override IType VisitOtherType(IType type)
  72. {
  73. if (IsAnonymousType(type))
  74. ContainsAnonType = true;
  75. return base.VisitOtherType(type);
  76. }
  77. public override IType VisitTypeDefinition(ITypeDefinition type)
  78. {
  79. if (IsAnonymousType(type))
  80. ContainsAnonType = true;
  81. return base.VisitTypeDefinition(type);
  82. }
  83. }
  84. internal static string GetDocumentation(this IEntity entity)
  85. {
  86. var docProvider = XmlDocLoader.LoadDocumentation(entity.ParentModule.PEFile);
  87. if (docProvider == null)
  88. return null;
  89. return docProvider.GetDocumentation(entity);
  90. }
  91. }
  92. }