`IEnumerable <keyvaluepair <string,string =“”>&gt;`带子对象(Array,Ienumeralbe)

时间:2016-04-18 11:28:08

标签: c#

如果其中一个(子)成员又是某种列表或数组,我该如何调整此示例?

/// <summary>
///     Gets all public properties of an object and and puts them into dictionary.
/// </summary>
public static IDictionary<string, object> ToDictionary(this object instance)
{
    if (instance == null)
        throw new NullReferenceException();

    // if an object is dynamic it will convert to IDictionary<string, object>
    var result = instance as IDictionary<string, object>;
    if (result != null)
        return result;

    return instance.GetType()
        .GetProperties()
        .ToDictionary(x => x.Name, x => x.GetValue(instance));
}

0 个答案:

没有答案