C# 使用反射创建对象

C# 使用反射创建对象,c#,reflection,null-object-pattern,C#,Reflection,Null Object Pattern,我试图编写一个NullObject创建方法,其中我传入一个实现ICreateEmptyInstance接口的类名(该接口为空),它遍历它的属性,查找实现ICreateEmptyInstance的其他类,并将创建这些类的“Null”实例 public interface ICreateEmptyInstance { } public static class NullObject { public static T Create<T>() where T : ICreateE

我试图编写一个
NullObject
创建方法,其中我传入一个实现
ICreateEmptyInstance
接口的类名(该接口为空),它遍历它的属性,查找实现
ICreateEmptyInstance
的其他类,并将创建这些类的“Null”实例

public interface ICreateEmptyInstance { }

public static class NullObject
{
    public static T Create<T>() where T : ICreateEmptyInstance, new()
    {
        var instance = new T();

        var properties = typeof(T).GetProperties();
        foreach (var property in properties.Where(property => typeof(ICreateEmptyInstance).IsAssignableFrom(property.PropertyType)))
        {
            var propertyInstance = NullObject.Create<property.PropertyType>();
            property.SetValue(instance, propertyInstance);
        }

        return instance;
    }
}
公共接口ICreateEmptyInstance{}
公共静态类空对象
{
公共静态T Create(),其中T:ICreateEmptyInstance,new()
{
var instance=newt();
var properties=typeof(T).GetProperties();
foreach(properties.Where(property=>typeof(ICreateEmptyInstance.IsAssignableFrom(property.PropertyType))中的var属性)
{
var propertyInstance=NullObject.Create();
SetValue(实例,propertyInstance);
}
返回实例;
}
}
我应该可以用这个来称呼它

var myEmptyClass = NullObject.Create<MyClass>();
var myEmptyClass=NullObject.Create();
我遇到的问题是foreach循环的内部,这行代码

var propertyInstance = NullObject.Create<property.PropertyType>();
var-propertyInstance=NullObject.Create();
…显然这不起作用,但是我如何才能创建一个“空对象”来分配给我当前正在创建的实例呢

编辑: 那仿制药呢??我希望创建空实例

  foreach (var property in properties.Where(property => property.GetType().IsGenericType))
  {
       var propertyInstance = Enumerable.Empty<>(); //TODO: how do I get the type for here?
       property.SetValue(instance, propertyInstance);
  }
foreach(properties.Where(property=>property.GetType().IsGenericType)中的var属性)
{
var propertyInstance=Enumerable.Empty();//TODO:如何获取此处的类型?
SetValue(实例,propertyInstance);
}

您可以创建一个非泛型方法并使用它:

public static T Create<T>() where T : ICreateEmptyInstance, new()
{
    return (T) Create(typeof (T));
}

private static object Create(Type type)
{
    var instance = Activator.CreateInstance(type);

    var properties = type.GetProperties();
    foreach (var property in properties.Where(property => typeof(ICreateEmptyInstance).IsAssignableFrom(property.PropertyType)))
    {
        var propertyInstance = NullObject.Create(property.PropertyType);
        property.SetValue(instance, propertyInstance);
    }

    return instance;
}
public static T Create(),其中T:ICreateEmptyInstance,new()
{
返回(T)创建(typeof(T));
}
私有静态对象创建(类型)
{
var instance=Activator.CreateInstance(类型);
var properties=type.GetProperties();
foreach(properties.Where(property=>typeof(ICreateEmptyInstance.IsAssignableFrom(property.PropertyType))中的var属性)
{
var propertyInstance=NullObject.Create(property.PropertyType);
SetValue(实例,propertyInstance);
}
返回实例;
}

可能的副本看起来不错,但我遇到了堆栈溢出异常!我想循环创建了错误的类型,现在来测试一下。集合呢?如何确保实例的列表属性是用空列表创建的?您到底想要什么,为实现
ICreateEmptyInstance
接口的类型创建一个空集合?否,当我创建
实例
并设置其实现
ICreateEmptyInstance
的属性时,我还希望将属于集合或数组的任何属性设置为包含空集合。