C# 初始化泛型类型中的所有属性?
我想初始化泛型类型的所有公共属性。C# 初始化泛型类型中的所有属性?,c#,generics,reflection,C#,Generics,Reflection,我想初始化泛型类型的所有公共属性。 我编写了以下方法: public static void EmptyModel<T>(ref T model) where T : new() { foreach (PropertyInfo property in typeof(T).GetProperties()) { Type myType = property.GetType().MakeGenericType(); property.SetV
我编写了以下方法:
public static void EmptyModel<T>(ref T model) where T : new()
{
foreach (PropertyInfo property in typeof(T).GetProperties())
{
Type myType = property.GetType().MakeGenericType();
property.SetValue(Activator.CreateInstance(myType));//Compile error
}
}
publicstaticvoidemptyModel(reft模型),其中T:new()
{
foreach(typeof(T).GetProperties()中的PropertyInfo属性)
{
类型myType=property.GetType().MakeGenericType();
property.SetValue(Activator.CreateInstance(myType));//编译错误
}
}
但它有一个编译错误我该怎么做?这里有三个问题:
- 接受两个参数,一个是对要设置属性的对象的引用(或静态属性的
),另一个是要设置属性的值李>null
将返回property.GetType()
。要获取属性本身的类型,您需要使用PropertyInfo
- 当属性类型上没有无参数构造函数时,代码不会处理这种情况。在这里,如果不从根本上改变您的工作方式,您将无法获得太多的想象力,因此在我的代码中,如果找不到无参数构造函数,我将把属性初始化为
null
public static T EmptyModel<T>(ref T model) where T : new()
{
foreach (PropertyInfo property in typeof(T).GetProperties())
{
Type myType = property.PropertyType;
var constructor = myType.GetConstructor(Type.EmptyTypes);
if (constructor != null)
{
// will initialize to a new copy of property type
property.SetValue(model, constructor.Invoke(null));
// or property.SetValue(model, Activator.CreateInstance(myType));
}
else
{
// will initialize to the default value of property type
property.SetValue(model, null);
}
}
}
公共静态T EmptyModel(ref T model),其中T:new()
{
foreach(typeof(T).GetProperties()中的PropertyInfo属性)
{
类型myType=property.PropertyType;
var constructor=myType.GetConstructor(Type.EmptyTypes);
if(构造函数!=null)
{
//将初始化为属性类型的新副本
SetValue(model,constructor.Invoke(null));
//或者property.SetValue(model,Activator.CreateInstance(myType));
}
其他的
{
//将初始化为属性类型的默认值
SetValue(model,null);
}
}
}
这里有三个问题:
- 接受两个参数,一个是对要设置属性的对象的引用(或静态属性的
),另一个是要设置属性的值李>null
将返回property.GetType()
。要获取属性本身的类型,您需要使用PropertyInfo
- 当属性类型上没有无参数构造函数时,代码不会处理这种情况。在这里,如果不从根本上改变您的工作方式,您将无法获得太多的想象力,因此在我的代码中,如果找不到无参数构造函数,我将把属性初始化为
null
public static T EmptyModel<T>(ref T model) where T : new()
{
foreach (PropertyInfo property in typeof(T).GetProperties())
{
Type myType = property.PropertyType;
var constructor = myType.GetConstructor(Type.EmptyTypes);
if (constructor != null)
{
// will initialize to a new copy of property type
property.SetValue(model, constructor.Invoke(null));
// or property.SetValue(model, Activator.CreateInstance(myType));
}
else
{
// will initialize to the default value of property type
property.SetValue(model, null);
}
}
}
公共静态T EmptyModel(ref T model),其中T:new()
{
foreach(typeof(T).GetProperties()中的PropertyInfo属性)
{
类型myType=property.PropertyType;
var constructor=myType.GetConstructor(Type.EmptyTypes);
if(构造函数!=null)
{
//将初始化为属性类型的新副本
SetValue(model,constructor.Invoke(null));
//或者property.SetValue(model,Activator.CreateInstance(myType));
}
其他的
{
//将初始化为属性类型的默认值
SetValue(model,null);
}
}
}
如果属性没有构造函数该怎么办。e、 g:如果属性为字符串,则发生此异常:没有为此对象定义无参数构造函数。
@MohammadActivator.CreateInstance
仅适用于具有无参数构造函数的类型(至少是您调用它的方式)。请参阅我的更新答案以获取替代方案。这将使任何字符串属性初始化为null
。如果属性没有构造函数该怎么办。e、 g:如果属性为字符串,则发生此异常:没有为此对象定义无参数构造函数。
@MohammadActivator.CreateInstance
仅适用于具有无参数构造函数的类型(至少是您调用它的方式)。请参阅我的更新答案以获取替代方案。这将使任何字符串属性初始化为null
。