私は反射のみ経由へのアクセス権を持っているインデックス付きプロパティをオーバーitterateしたい、、(リフレクション)
が、インデックス付きプロパティを反復処理(と私はおそらく、あきれるほど単純な答えがあることを完全な知識でこれを言いますMSDN/Google fail = /)TargetInvocationException
がスローされるまで、PropertyInfo.GetValue(prop, counter)
以上のカウンターをインクリメントする以外に方法を見つける/考えられません。
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 }));
}
}
}
今、いくつかの問題が..この...非常に醜い..溶液である
それは多次元または、例えば、整数で索引付けされていない何場合..
私が試してみて誰かがそれを必要とするなら、それを働かせるために使っているテストコードです。興味があれば私はカスタムキャッシュシステムを作っていて、.Equalsはそれをカットしません。
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());
}
}
(( "こんにちは、世界")。てToArray())'? –
単なる一例変数が...私の関数に渡す文字列/文字列を定義するさまざまな方法を試したと '...厄介な1のビットの上に残されたオブジェクトSTR =「こんにちは、世界!」;'ちょうど同様に働く。 –
整数ではなくSTRINGキーがある場合はどうすればいいですか?私は彼らの名前を知らない。それらを見つけて使用するには? – Alexander