通过索引属性迭代(反射)反射、索引、属性、迭代

2023-09-03 06:54:11 作者:素笺ベ

我想itterate在索引属性,我只能通过反射访问,

但(我这样说是在充分了解,有可能是一个令人尴尬的答案很简单,MSDN /谷歌失败= /)我找不到/想到了一个办法,除了增加计数器在的PropertyInfo。的GetValue(道具,计),直到 TargetInvocationException 被抛出。

ALA:

 的foreach(的PropertyInfo道具在obj.GetType()。GetProperties中())
{
    如果(prop.GetIndexParameters()长度和GT; 0)
    {
        //得到一个整数计数值,通过增加一个计数器,直至抛出异常
        诠释计数= 0;
        而(真)
        {
            尝试
            {
                prop.GetValue(OBJ,新的对象[] {数});
                算上++;
            }
            赶上(TargetInvocationException){打破; }
        }

        的for(int i = 0; I<计数;我++)
        {
            //过程中,项目价值
            流程(prop.GetValue(OBJ,新的对象[] {I}));
        }
    }
}
 

现在,也有与此有些问题......非常难看..解决方案。

如果它是多维的,或不按整数例如索引...

继承人的考验code我使用的尝试,并得到它的工作,如果有人需要它。如果任何人的兴趣,我做一个自定义缓存系统和.Equals不剪。

 静态无效的主要()
    {
        对象海峡=新的String((你好,世界).ToArray());

        流程(STR);

        Console.ReadKey();
    }

    静态无效的过程(obj对象)
    {
        类型类型= obj.GetType();

        的PropertyInfo []属性= type.GetProperties();

        //如果这个OBJ有子特性,应用该方法的,而不是这个。
        如果(properties.Length大于0)
        {
            的foreach(的PropertyInfo道具的属性)
            {
                //如果它是一个索引类型,请针对每个
                如果(prop.GetIndexParameters()长度和GT; 0)
                {
                    //得到一个整数计数值
                    //问题,如果它不是一个整数索引(词典),如果它是多维的?
                    //只需要能够通过每个值迭代中的索引属性
                    诠释计数= 0;
                    而(真)
                    {
                        尝试
                        {
                            prop.GetValue(OBJ,新的对象[] {数});
                            算上++;
                        }
                        赶上(TargetInvocationException){打破; }
                    }

                    的for(int i = 0; I<计数;我++)
                    {
                        流程(prop.GetValue(OBJ,新的对象[] {I}));
                    }
                }
                其他
                {
                    //为普通型等等。
                    流程(prop.GetValue(OBJ,NULL));
                }
            }
        }
        其他
        {
            //过程被应用到每个属性
            Console.WriteLine(属性值:{0},obj.ToString());
        }
    }
 

解决方案

索引器的吸气剂就像正常的方法,不过它是以方括号的,不是圆的。你不会指望能自动确定方法可接受值的范围内,所以它不是可行的索引器。

NumPy 索引 切片 迭代

I want to itterate over an indexed property that I only have access to via reflection,

but ( and I say this in the full knowledge that there's probably an embarrassingly simple answer, MSDN/Google fail =/ ) I cannot find/think of a way besides incrementing a counter over the PropertyInfo.GetValue(prop, counter) until the TargetInvocationException is thrown.

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 } ) );
        }
    }
}

now, there are some issues with this... very ugly.. solution..

what if it's multi-dimensional or not indexed by integers for example...

heres the test code I'm using to try and get it working if anyone needs it. If anyone's interested I'm making a custom caching system and .Equals doesn't cut it.

    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() );
        }
    }

解决方案

The getter of an indexer is just like normal method, except it takes square brackets, not round ones. You wouldn't expect to be able to automatically determine the range of acceptable values for a method, so it's not feasible for an indexer.