/ILSpy/TreeNodes/Analyzer/AnalyzedInterfacePropertyImplementedByTreeNode.cs

http://github.com/icsharpcode/ILSpy · C# · 86 lines · 59 code · 10 blank · 17 comment · 11 complexity · 01e4c523329d3ce8a1946ea52dba7830 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 ICSharpCode.Decompiler.Ast;
  23. using Mono.Cecil;
  24. namespace ICSharpCode.ILSpy.TreeNodes.Analyzer
  25. {
  26. internal sealed class AnalyzedInterfacePropertyImplementedByTreeNode : AnalyzerSearchTreeNode
  27. {
  28. private readonly PropertyDefinition analyzedProperty;
  29. private readonly MethodDefinition analyzedMethod;
  30. public AnalyzedInterfacePropertyImplementedByTreeNode(PropertyDefinition analyzedProperty)
  31. {
  32. if (analyzedProperty == null)
  33. throw new ArgumentNullException("analyzedProperty");
  34. this.analyzedProperty = analyzedProperty;
  35. this.analyzedMethod = this.analyzedProperty.GetMethod ?? this.analyzedProperty.SetMethod;
  36. }
  37. public override object Text
  38. {
  39. get { return "Implemented By"; }
  40. }
  41. protected override IEnumerable<AnalyzerTreeNode> FetchChildren(CancellationToken ct)
  42. {
  43. var analyzer = new ScopedWhereUsedAnalyzer<AnalyzerTreeNode>(analyzedMethod, FindReferencesInType);
  44. return analyzer.PerformAnalysis(ct).OrderBy(n => n.Text);
  45. }
  46. private IEnumerable<AnalyzerTreeNode> FindReferencesInType(TypeDefinition type)
  47. {
  48. if (!type.HasInterfaces)
  49. yield break;
  50. TypeReference implementedInterfaceRef = type.Interfaces.FirstOrDefault(i => i.Resolve() == analyzedMethod.DeclaringType);
  51. if (implementedInterfaceRef == null)
  52. yield break;
  53. foreach (PropertyDefinition property in type.Properties.Where(e => e.Name == analyzedProperty.Name)) {
  54. MethodDefinition accessor = property.GetMethod ?? property.SetMethod;
  55. if (TypesHierarchyHelpers.MatchInterfaceMethod(accessor, analyzedMethod, implementedInterfaceRef)) {
  56. var node = new AnalyzedPropertyTreeNode(property);
  57. node.Language = this.Language;
  58. yield return node;
  59. }
  60. yield break;
  61. }
  62. foreach (PropertyDefinition property in type.Properties.Where(e => e.Name.EndsWith(analyzedProperty.Name))) {
  63. MethodDefinition accessor = property.GetMethod ?? property.SetMethod;
  64. if (accessor.HasOverrides && accessor.Overrides.Any(m => m.Resolve() == analyzedMethod)) {
  65. var node = new AnalyzedPropertyTreeNode(property);
  66. node.Language = this.Language;
  67. yield return node;
  68. }
  69. }
  70. }
  71. public static bool CanShow(PropertyDefinition property)
  72. {
  73. return property.DeclaringType.IsInterface;
  74. }
  75. }
  76. }