Finding whether a class has an indexer

  • Thread starter Thread starter PseudoBill
  • Start date Start date
P

PseudoBill

Is there a way to find out if a class has an indexer via reflection?

Assuming I have a System.Type object "asmType", how do I find out if
the class has an indexer? I'd prefer to do this without iterating
through all the class members, but even in that case I can't find out
how to determine the indexer.
 
Thanks! That helped a lot. In case anyone else is interested here is
the code to find whether a type has indexers:

object[] oAttributes = asmType.GetCustomAttributes(true);
for(int j = 0; j < oAttributes.Length; j++)
{
if (oAttributes[j].ToString() ==
"System.Reflection.DefaultMemberAttribute")
MessageBox.Show(asmType.Name);
break;
}

It would be nice if you didn't have to iterate through the attributes,
but this works fine.
 
Wouldn't comparing the attribute for "is
System.Reflection.DefaultMemberAttribute" be a little better (faster and
less error prone) than comparing the string interpretation of the object,
which may change?

Jerry

PseudoBill said:
Thanks! That helped a lot. In case anyone else is interested here is
the code to find whether a type has indexers:

object[] oAttributes = asmType.GetCustomAttributes(true);
for(int j = 0; j < oAttributes.Length; j++)
{
if (oAttributes[j].ToString() ==
"System.Reflection.DefaultMemberAttribute")
MessageBox.Show(asmType.Name);
break;
}

It would be nice if you didn't have to iterate through the attributes,
but this works fine.
 
Jerry Piskwrote:
Wouldn't comparing the attribute for "is
System.Reflection.DefaultMemberAttribute" be a little better (faster and
less error prone) than comparing the string interpretation of the object,
which may change?

Jerry

Thanks - I didn't know that!
 
Back
Top