l'itération sur une propriété indexée (Réflexion)

Je veux itterate sur une propriété indexée que je n'ai accès à la via de réflexion,

mais ( et je dis cela en sachant qu'il y a probablement une embarrassante de réponse simple, MSDN/Google fail =/) je ne peut pas trouver/trouver une façon d'ailleurs l'incrémentation d'un compteur sur le PropertyInfo.GetValue(prop, counter) jusqu'à ce que le TargetInvocationException est levée.

ala:

foreach ( PropertyInfo prop in obj.GetType().GetProperties() )
{
    if ( prop.GetIndexParameters().Length > 0 )
    {
        //get an integer count value, by incrementing a counter until the exception is thrown
        int count = 0;
        while ( true )
        {
            try
            {
                prop.GetValue( obj, new object[] { count } );
                count++;
            }
            catch ( TargetInvocationException ) { break; }
        }

        for ( int i = 0; i < count; i++ )
        {
            //process the items value
            process( prop.GetValue( obj, new object[] { i } ) );
        }
    }
}

maintenant, il y a certains problèmes avec cette... très laid.. solution..

que si il est multi-dimensionnelle ou non indexés par des entiers par exemple...

heres, le code de test, je suis à l'aide de l'essayer et de le faire fonctionner, si quelqu'un en a besoin. Si quelqu'un est intéressé, je suis en train de faire un système de cache personnalisé et .Équivaut à ne pas le couper.

    static void Main()
{
object str = new String( ( "Hello, World" ).ToArray() );
process( str );
Console.ReadKey();
}
static void process( object obj )
{
Type type = obj.GetType();
PropertyInfo[] properties = type.GetProperties();
//if this obj has sub properties, apply this process to those rather than this.
if ( properties.Length > 0 )
{
foreach ( PropertyInfo prop in properties )
{
//if it's an indexed type, run for each
if ( prop.GetIndexParameters().Length > 0 )
{
//get an integer count value
//issues, what if it's not an integer index (Dictionary?), what if it's multi-dimensional?
//just need to be able to iterate through each value in the indexed property
int count = 0;
while ( true )
{
try
{
prop.GetValue( obj, new object[] { count } );
count++;
}
catch ( TargetInvocationException ) { break; }
}
for ( int i = 0; i < count; i++ )
{
process( prop.GetValue( obj, new object[] { i } ) );
}
}
else
{
//is normal type so.
process( prop.GetValue( obj, null ) );
}
}
}
else
{
//process to be applied to each property
Console.WriteLine( "Property Value: {0}", obj.ToString() );
}
}
Quel est le but de object str = new String(("Hello, World").ToArray())?
juste un exemple de la variable à transmettre à ma fonction... a essayer les différentes façons de définir une chaîne/Chaîne et laissé un peu de mal... object str = "Hello, World!"; fonctionne tout aussi bien.
Que faire si j'ai des clés de CHAÎNE, pas entier? Je ne connais pas leurs noms. Comment les trouver et les utiliser?

OriginalL'auteur Dead.Rabit | 2010-11-24