C# 递归获取属性和子字段的属性
C# get properties, and properties of child fields recursivly
我正在尝试将对象转换为 ExpandoObject。
到目前为止,我已经得到属性和字段的数组,
Type type = data.GetType();
FieldInfo[] fields = type.GetFields();
PropertyInfo[] props = type.GetProperties();
我可以轻松遍历道具并获取每个道具的值,并将其添加到我的 ExpandoObject 中:
public static ExpandoObject ConvertToExpandoObject(object data)
{
var dynObj = new ExpandoObject();
Type type = data.GetType();
FieldInfo[] fields = type.GetFields();
PropertyInfo[] props = type.GetProperties();
// first the properties...
foreach (var property in props)
{
// add this property
((IDictionary<string, object>)dynObj).Add(property.Name, property.GetValue(data,null));
}
}
但是对于每个字段,我想再次调用转换方法:
foreach (var field in fields)
{
// add this field
((IDictionary<string, object>)dynObj).Add(field.Name,ConvertToExpandoObject(field?? as an object));
}
因为我不知道相关字段的类型,尽管它在 field.FieldType 中,我该如何将字段转换成它自己类型的对象以传递给转换功能?
编辑:
全文作为一个块,希望更清楚:
public static ExpandoObject ConvertToExpandoObject(object data)
{
var dynObj = new ExpandoObject();
Type type = data.GetType();
FieldInfo[] fields = type.GetFields();
PropertyInfo[] props = type.GetProperties();
// first the properties...
foreach (var property in props)
{
// add this property
((IDictionary<string, object>)dynObj).Add(property.Name, property.GetValue(data,null));
}
foreach (var field in fields)
{
// add this field
((IDictionary<string, object>)dynObj).Add(field.Name,ConvertToExpandoObject(field?? as an object));
}
}
编辑 2:
示例对象 - 它们很简单。它现在可能是学术性的,因为我可以很容易地预见到它们什么时候不会……可能会回到绘图板上
class Parent
{
public string parentVar { get; set; }
public Child child { get; set; }
}
class Child
{
public string childvar { get; set; }
}
我换个角度看,
最终目标是在将其序列化以发送到 Web 客户端之前转换为 ExpandoObject。这样可以根据用户授权删除属性..
所以我只是先序列化它,然后删除属性..
ExpandoObject dynObj = JsonConvert.DeserializeObject<ExpandoObject>(JsonConvert.SerializeObject(data));
我正在尝试将对象转换为 ExpandoObject。
到目前为止,我已经得到属性和字段的数组,
Type type = data.GetType();
FieldInfo[] fields = type.GetFields();
PropertyInfo[] props = type.GetProperties();
我可以轻松遍历道具并获取每个道具的值,并将其添加到我的 ExpandoObject 中:
public static ExpandoObject ConvertToExpandoObject(object data)
{
var dynObj = new ExpandoObject();
Type type = data.GetType();
FieldInfo[] fields = type.GetFields();
PropertyInfo[] props = type.GetProperties();
// first the properties...
foreach (var property in props)
{
// add this property
((IDictionary<string, object>)dynObj).Add(property.Name, property.GetValue(data,null));
}
}
但是对于每个字段,我想再次调用转换方法:
foreach (var field in fields)
{
// add this field
((IDictionary<string, object>)dynObj).Add(field.Name,ConvertToExpandoObject(field?? as an object));
}
因为我不知道相关字段的类型,尽管它在 field.FieldType 中,我该如何将字段转换成它自己类型的对象以传递给转换功能?
编辑:
全文作为一个块,希望更清楚:
public static ExpandoObject ConvertToExpandoObject(object data)
{
var dynObj = new ExpandoObject();
Type type = data.GetType();
FieldInfo[] fields = type.GetFields();
PropertyInfo[] props = type.GetProperties();
// first the properties...
foreach (var property in props)
{
// add this property
((IDictionary<string, object>)dynObj).Add(property.Name, property.GetValue(data,null));
}
foreach (var field in fields)
{
// add this field
((IDictionary<string, object>)dynObj).Add(field.Name,ConvertToExpandoObject(field?? as an object));
}
}
编辑 2:
示例对象 - 它们很简单。它现在可能是学术性的,因为我可以很容易地预见到它们什么时候不会……可能会回到绘图板上
class Parent
{
public string parentVar { get; set; }
public Child child { get; set; }
}
class Child
{
public string childvar { get; set; }
}
我换个角度看, 最终目标是在将其序列化以发送到 Web 客户端之前转换为 ExpandoObject。这样可以根据用户授权删除属性..
所以我只是先序列化它,然后删除属性..
ExpandoObject dynObj = JsonConvert.DeserializeObject<ExpandoObject>(JsonConvert.SerializeObject(data));