Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 初始化泛型类型中的所有属性?_C#_Generics_Reflection - Fatal编程技术网

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:如果属性为字符串,则发生此异常:
没有为此对象定义无参数构造函数。
@Mohammad
Activator.CreateInstance
仅适用于具有无参数构造函数的类型(至少是您调用它的方式)。请参阅我的更新答案以获取替代方案。这将使任何字符串属性初始化为
null
。如果属性没有构造函数该怎么办。e、 g:如果属性为字符串,则发生此异常:
没有为此对象定义无参数构造函数。
@Mohammad
Activator.CreateInstance
仅适用于具有无参数构造函数的类型(至少是您调用它的方式)。请参阅我的更新答案以获取替代方案。这将使任何字符串属性初始化为
null