如何用反射初始化 List<T> 和元素?
How to initialize a List<T> and elements with reflection?
TL;DR
我坚持使用反射初始化我的 List<T>
s 和元素。
描述
我正在尝试使用反射以编程方式初始化整个导出 class。
这个 class 包含很多子 classes 和列表。
这个函数的目的是为了XML导出快速生成填充的class。
为什么我要使用反射,你可能会问,那是因为我有很多不同的 classes 的巨大种类。
仅仅为了测试目的而将它们全部写出来会很痛苦。
代码(58行)
public static object Populate(object object_Orginal)
{
PropertyInfo[] PropertyInfos = object_Orginal.GetType().GetProperties();
for (int iIndex = 0; iIndex < PropertyInfos.Length; iIndex++)
{
PropertyInfo PropertyInfo_Tmp = PropertyInfos[iIndex];
if (PropertyInfo_Tmp.GetSetMethod() == null)
{
continue;
}
// Is it right to exclude them?
if (PropertyInfo_Tmp.Name == "Capacity" || PropertyInfo_Tmp.Name == "Count")
{
continue;
}
Type Type_Tmp = PropertyInfo_Tmp.PropertyType;
if (Type_Tmp == typeof(int))
{
PropertyInfo_Tmp.SetValue(object_Orginal, 1);
}
// [...] a few more basic types
// >>> Here I'm completly stuck - and yea it's a mess
else if (Type_Tmp.Name == "List`1") // typeof(List<>))
{
object list = Activator.CreateInstance(Type_Tmp);
MethodInfo add = Type_Tmp.GetMethod("Add");
IEnumerable<Attribute> a = PropertyInfo_Tmp.GetCustomAttributes();
PropertyInfo[] propertyInfo = list.GetType().GetProperties();
foreach (PropertyInfo property in propertyInfo)
{
object d = Populate(property);
property.SetValue(list, d);
}
//foreach (Attribute item in a)
//{
// add.Invoke(list, new object[] { Populate(item) });
//}
//add.Invoke(list, new[] { item });
//prop.SetValue(x, list, null);
}
// <<<
else
{
ConstructorInfo ConstructorInfo_Property = Type_Tmp.GetConstructor(Type.EmptyTypes);
object object_Property = ConstructorInfo_Property.Invoke(new object[0]);
object_Property = Populate(object_Property);
PropertyInfo_Tmp.SetValue(object_Orginal, object_Property);
}
}
return object_Orginal;
}
原始代码来自
我尝试了 few different 种方法,但无法正确实施。
主要目标是初始化 List<T>
,然后向列表中添加一个或多个项目并递归初始化它们。
通过查看您的代码,我假设您的 T 类 是具有所有属性的默认构造函数和设置器的简单 DTO?
尝试Objectfiller,它似乎做你想要的:
The ObjectFiller.NET will help you filling your classes with
meaningfull but random data. You'll never have to fill your complex
class hierarchies by hand - ObjectFiller.NET will do that for you!
并且还可以填充 IEnumerable,它很容易转换为 List
TL;DR
我坚持使用反射初始化我的 List<T>
s 和元素。
描述
我正在尝试使用反射以编程方式初始化整个导出 class。
这个 class 包含很多子 classes 和列表。
这个函数的目的是为了XML导出快速生成填充的class。
为什么我要使用反射,你可能会问,那是因为我有很多不同的 classes 的巨大种类。
仅仅为了测试目的而将它们全部写出来会很痛苦。
代码(58行)
public static object Populate(object object_Orginal)
{
PropertyInfo[] PropertyInfos = object_Orginal.GetType().GetProperties();
for (int iIndex = 0; iIndex < PropertyInfos.Length; iIndex++)
{
PropertyInfo PropertyInfo_Tmp = PropertyInfos[iIndex];
if (PropertyInfo_Tmp.GetSetMethod() == null)
{
continue;
}
// Is it right to exclude them?
if (PropertyInfo_Tmp.Name == "Capacity" || PropertyInfo_Tmp.Name == "Count")
{
continue;
}
Type Type_Tmp = PropertyInfo_Tmp.PropertyType;
if (Type_Tmp == typeof(int))
{
PropertyInfo_Tmp.SetValue(object_Orginal, 1);
}
// [...] a few more basic types
// >>> Here I'm completly stuck - and yea it's a mess
else if (Type_Tmp.Name == "List`1") // typeof(List<>))
{
object list = Activator.CreateInstance(Type_Tmp);
MethodInfo add = Type_Tmp.GetMethod("Add");
IEnumerable<Attribute> a = PropertyInfo_Tmp.GetCustomAttributes();
PropertyInfo[] propertyInfo = list.GetType().GetProperties();
foreach (PropertyInfo property in propertyInfo)
{
object d = Populate(property);
property.SetValue(list, d);
}
//foreach (Attribute item in a)
//{
// add.Invoke(list, new object[] { Populate(item) });
//}
//add.Invoke(list, new[] { item });
//prop.SetValue(x, list, null);
}
// <<<
else
{
ConstructorInfo ConstructorInfo_Property = Type_Tmp.GetConstructor(Type.EmptyTypes);
object object_Property = ConstructorInfo_Property.Invoke(new object[0]);
object_Property = Populate(object_Property);
PropertyInfo_Tmp.SetValue(object_Orginal, object_Property);
}
}
return object_Orginal;
}
原始代码来自
我尝试了 few different 种方法,但无法正确实施。
主要目标是初始化 List<T>
,然后向列表中添加一个或多个项目并递归初始化它们。
通过查看您的代码,我假设您的 T 类 是具有所有属性的默认构造函数和设置器的简单 DTO? 尝试Objectfiller,它似乎做你想要的:
The ObjectFiller.NET will help you filling your classes with meaningfull but random data. You'll never have to fill your complex class hierarchies by hand - ObjectFiller.NET will do that for you!
并且还可以填充 IEnumerable,它很容易转换为 List