From bc98ed09b74fd68311fce40a20c185d345922d36 Mon Sep 17 00:00:00 2001 From: Christian Hornung Date: Sun, 21 Sep 2008 10:23:54 +0000 Subject: [PATCH] Reflector AddIn: Support indexers with non-default names. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@3549 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../Src/TextEditorContextMenuCommand.cs | 52 ++++++++++++++++++- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/src/AddIns/Misc/ReflectorAddIn/ReflectorAddIn/Project/Src/TextEditorContextMenuCommand.cs b/src/AddIns/Misc/ReflectorAddIn/ReflectorAddIn/Project/Src/TextEditorContextMenuCommand.cs index 6f17d11383..a961c1e5d6 100644 --- a/src/AddIns/Misc/ReflectorAddIn/ReflectorAddIn/Project/Src/TextEditorContextMenuCommand.cs +++ b/src/AddIns/Misc/ReflectorAddIn/ReflectorAddIn/Project/Src/TextEditorContextMenuCommand.cs @@ -7,6 +7,7 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Windows.Forms; using ICSharpCode.Core; @@ -111,8 +112,7 @@ namespace ReflectorAddIn element.MemberType = MemberType.Property; element.MemberParameters = ConvertParameters(property.Parameters); if (property.IsIndexer) { - // TODO: Support indexers with different names using DefaultMemberAttribute - element.MemberName = "Item"; + element.MemberName = GetRealIndexerPropertyName(c, property); } } else { @@ -144,6 +144,54 @@ namespace ReflectorAddIn } } + static string GetRealIndexerPropertyName(IClass c, IProperty property) + { + // Support indexers with different names using IndexerNameAttribute or DefaultMemberAttribute + + string name = GetSingleAttributeWithStringParameter(c, property.Attributes, "System.Runtime.CompilerServices.IndexerNameAttribute"); + if (name != null) { + LoggingService.Debug("Reflector AddIn: IndexerNameAttribute on property found: '" + name + "'"); + return name; + } + + name = GetSingleAttributeWithStringParameter(c, c.Attributes, "System.Reflection.DefaultMemberAttribute"); + if (name != null) { + LoggingService.Debug("Reflector AddIn: DefaultMemberAttribute on class with indexer found: '" + name + "'"); + return name; + } else { + if (property.Name == "Indexer") { + LoggingService.Debug("Reflector AddIn: Neither DefaultMemberAttribute nor IndexerNameAttribute found, assuming default name"); + return "Item"; + } else { + LoggingService.Debug("Reflector AddIn: Neither DefaultMemberAttribute nor IndexerNameAttribute found, but the property has name '" + property.Name + "', using that"); + return property.Name; + } + } + } + + static string GetSingleAttributeWithStringParameter(IClass c, IEnumerable attributeList, string attributeTypeName) + { + IReturnType attributeType = GetDomType(attributeTypeName, c.ProjectContent); + if (attributeType == null) { + return null; + } + var attributes = attributeList.Where(att => attributeType.Equals(att.AttributeType) && att.PositionalArguments.Count == 1 && att.PositionalArguments[0] is string); + if (attributes.Count() == 1) { + return (string)attributes.Single().PositionalArguments[0]; + } + return null; + } + + static IReturnType GetDomType(string className, IProjectContent pc) + { + IClass c = pc.GetClass(className, 0); + if (c == null) { + LoggingService.Warn("Reflector AddIn: Could not find the class for '" + className + "'"); + return null; + } + return c.DefaultReturnType; + } + static void SetNamespaceAndTypeNames(CodeTypeInfo t, IClass c) { Stack outerClasses = new Stack();