Я ищу эффективный способ получить все свойства / поля сложного типа, которые внутренне имеют свойства другого сложного типа.
* ** 1003 тысяча два * Пример
class A
{
public int Id{get;set;}
public string Name{get;set;}
public List<string> Branch{get;set;}
public BuildData build {get;set;}
public SampleData sample {get;set;}
public DummyData dData {get;set}
//constructor
public A(){}
}
class BuildData{
private string _buildData;
public string param1{get;set;}
public string[] paramArgs{get;set;}
}
class SampleData {
public string Test{get;set;}
public List<DummyData> dummyArgs{get;set;}
}
class DummyData {
public int Val{get;set;}
public string GUIData{get;set;}
}
Мне нужно использовать class A
и получить все его элементы (поля, реквизиты), которые также должны включать элементы других сложных типов, таких как DummyData, BuildData, SampleData.
Я пытался использовать Reflection, но не смог получить всех участников.
public void PrintProperties(object obj, ref String value)
{
if (obj == null)
{
return;
}
Type objType = obj.GetType();
PropertyInfo[] properties = objType.GetProperties();
foreach (PropertyInfo property in properties)
{
object propValue = property.GetValue(obj, null);
var elems = propValue as IList;
if (elems != null)
{
foreach (var item in elems)
{
PrintProperties(item, ref value); }
}
else
{
if (property.PropertyType.Assembly == objType.Assembly)
{
value += string.Format("*Name - {0}; Type - {1}*", property.Name, property.PropertyType);
PrintProperties(propValue, ref value);
}
else
{
value += string.Format("Name - {0}; defaultValue-{1}; Type - {2};", property.Name, propValue, property.PropertyType);
}
}
}
}