C# `IEnumerable<;KeyValuePair<;字符串,字符串>>;`带有子对象(数组、Ienumeralbe)
如果其中一个(子)成员又是某种列表或数组,我应该如何调整这个示例C# `IEnumerable<;KeyValuePair<;字符串,字符串>>;`带有子对象(数组、Ienumeralbe),c#,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 (
/// <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));
}
//
///获取对象的所有公共属性,并将它们放入字典中。
///
公共静态IDictionary ToDictionary(此对象实例)
{
if(实例==null)
抛出新的NullReferenceException();
//如果对象是动态的,它将转换为IDictionary
var result=作为IDictionary的实例;
如果(结果!=null)
返回结果;
return instance.GetType()
.GetProperties()
.ToDictionary(x=>x.Name,x=>x.GetValue(实例));
}
如果你能说出目的会更容易…如果你能说出目的会更容易。。。