/ILSpy/TreeNodes/Analyzer/AnalyzedTypeExtensionMethodsTreeNode.cs

http://github.com/icsharpcode/ILSpy · C# · 84 lines · 56 code · 10 blank · 18 comment · 14 complexity · a4a62b9be657a88f498c2ef045c90a8e 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.Collections.Generic;
  20. using System.Linq;
  21. using System.Threading;
  22. using Mono.Cecil;
  23. namespace ICSharpCode.ILSpy.TreeNodes.Analyzer
  24. {
  25. internal class AnalyzedTypeExtensionMethodsTreeNode : AnalyzerSearchTreeNode
  26. {
  27. private readonly TypeDefinition analyzedType;
  28. public AnalyzedTypeExtensionMethodsTreeNode(TypeDefinition analyzedType)
  29. {
  30. if (analyzedType == null)
  31. throw new ArgumentNullException("analyzedType");
  32. this.analyzedType = analyzedType;
  33. }
  34. public override object Text
  35. {
  36. get { return "Extension Methods"; }
  37. }
  38. protected override IEnumerable<AnalyzerTreeNode> FetchChildren(CancellationToken ct)
  39. {
  40. var analyzer = new ScopedWhereUsedAnalyzer<AnalyzerTreeNode>(analyzedType, FindReferencesInType);
  41. return analyzer.PerformAnalysis(ct).OrderBy(n => n.Text);
  42. }
  43. private IEnumerable<AnalyzerTreeNode> FindReferencesInType(TypeDefinition type)
  44. {
  45. if (!HasExtensionAttribute(type))
  46. yield break;
  47. foreach (MethodDefinition method in type.Methods) {
  48. if (method.IsStatic && HasExtensionAttribute(method)) {
  49. if (method.HasParameters && method.Parameters[0].ParameterType.Resolve() == analyzedType) {
  50. var node = new AnalyzedMethodTreeNode(method);
  51. node.Language = this.Language;
  52. yield return node;
  53. }
  54. }
  55. }
  56. }
  57. bool HasExtensionAttribute(ICustomAttributeProvider p)
  58. {
  59. if (p.HasCustomAttributes) {
  60. foreach (CustomAttribute ca in p.CustomAttributes) {
  61. TypeReference t = ca.AttributeType;
  62. if (t.Name == "ExtensionAttribute" && t.Namespace == "System.Runtime.CompilerServices")
  63. return true;
  64. }
  65. }
  66. return false;
  67. }
  68. public static bool CanShow(TypeDefinition type)
  69. {
  70. // show on all types except static classes
  71. return !(type.IsAbstract && type.IsSealed);
  72. }
  73. }
  74. }