Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identifying a custom indexer using reflection in C#

Tags:

c#

.net

I have a class with a custom indexer like so

public string this[VehicleProperty property] {   // Code } 

How can I identify the custom indexer in the results of typeof(MyClass).GetProperties()?

like image 660
jbenckert Avatar asked Aug 28 '09 15:08

jbenckert


1 Answers

You can also look for index parameters, using the the PropertyInfo.GetIndexParameters method, if it returns more than 0 items, it's an indexed property:

foreach (PropertyInfo pi in typeof(MyClass).GetProperties()) {     if (pi.GetIndexParameters().Length > 0)     {        // Indexed property...     } } 
like image 56
Christian C. Salvadó Avatar answered Sep 22 '22 02:09

Christian C. Salvadó