Browse Source

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
shortcuts
Christian Hornung 18 years ago
parent
commit
bc98ed09b7
  1. 52
      src/AddIns/Misc/ReflectorAddIn/ReflectorAddIn/Project/Src/TextEditorContextMenuCommand.cs

52
src/AddIns/Misc/ReflectorAddIn/ReflectorAddIn/Project/Src/TextEditorContextMenuCommand.cs

@ -7,6 +7,7 @@ @@ -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 @@ -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 @@ -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<IAttribute> 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<IClass> outerClasses = new Stack<IClass>();

Loading…
Cancel
Save