0

オブジェクトのすべてのデータを 2 次元配列にダンプする必要があります

これは宣言です:

GetItemCall oGetItemCall = new GetItemCall(oContext);

次に、oGetItemCall.Item.ConditionIDまたはoGetItemCall.Item.ListingDetails.EndTimeなどを使用できます。

しかし、oGetItemCallオブジェクトには多くの変数があり、それらを読みやすい 1 つの 2 次元配列に追加したいと考えています。これを行う方法はありますか?

4

3 に答える 3

0

アレイが必要ですか?リストのような、より柔軟な構造を使用しないのはなぜですか。したがって、オブジェクトをリストに変換してみてください。リストには、インデックスを介して簡単にアクセスできます。この場合は、オブジェクトのプロパティ名を使用します 。リフレクションは問題を解決することができます。こちらをご覧ください。

于 2012-06-25T20:58:22.230 に答える
0

なぜそれをしたいのかは明らかではありませんが、どちらかがそうすべきです。

        string[,] some = new string[100,2]; 
        Hashtable table = new Hashtable(); 

        // array
        some[0,0] = "Key";
        some[0,1] = "value";

        //  hashtable 
        table["key"] = "value";
于 2012-06-25T21:02:13.317 に答える
0

アイテムとそのすべてのプロパティと値を確認しますか? 反射を使用します。

これは完全ではなく、決して完全ではありませんが、これが必要な場合は、良い出発点になります。タイプ名などを追加するために簡単に拡張できます。

public static List<string> ReflectObject(object o)
{
    var items = new List<string>();

    if (o == null)
    {
        items.Add("NULL"); // remove if you're not interested in NULLs.
        return items;
    }

    Type type = o.GetType();

    if (type.IsPrimitive || o is string)
    {
        items.Add(o.ToString());
        return items;
    }

    items.Add(string.Format("{0}{1}{0}", " ----- ", type.Name));

    if (o is IEnumerable)
    {
        IEnumerable collection = (IEnumerable)o;
        var enumerator = collection.GetEnumerator();

        while (enumerator.MoveNext())
        {
            foreach (var innerItem in ReflectObject(enumerator.Current))
            {
                items.Add(innerItem);
            }
        }
    }
    else
    {
        var properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);

        foreach (var property in properties)
        {
            object value = property.GetValue(o, null);

            foreach (var innerItem in ReflectObject(value))
            {
                items.Add(string.Format("{0}: {1}", property.Name, innerItem));
            }
        }
    }

    return items;
}

次のように使用できます。

Test t = new Test();
t.MyProperty1 = 123;
t.MyProperty2 = "hello";
t.MyProperty3 = new List<string>() { "one", "two" };
t.MyTestProperty = new Test();
t.MyTestProperty.MyProperty1 = 987;
t.MyTestProperty.MyTestProperty = new Test();
t.MyTestProperty.MyTestProperty.MyProperty2 = "goodbye";

var items = MyReflector.ReflectObject(t);

foreach (var item in items)
{
    Console.WriteLine(item);
}

これにより、次のようになります。

----- Test -----
MyProperty1: 123
MyProperty2: hello
MyProperty3:  ----- List`1 -----
MyProperty3: one
MyProperty3: two
MyTestProperty:  ----- Test -----
MyTestProperty: MyProperty1: 987
MyTestProperty: MyProperty2: NULL
MyTestProperty: MyProperty3: NULL
MyTestProperty: MyTestProperty:  ----- Test -----
MyTestProperty: MyTestProperty: MyProperty1: 0
MyTestProperty: MyTestProperty: MyProperty2: goodbye
MyTestProperty: MyTestProperty: MyProperty3: NULL
MyTestProperty: MyTestProperty: MyTestProperty: NULL
于 2012-06-25T21:57:55.700 に答える