سؤال

I'm trying to write a NullObject creation method, where I pass in a class name that implements an ICreateEmptyInstance interface (that is empty) and it walks it's properties looking for other classes that implement ICreateEmptyInstance and will create "Null" instances of those.

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;
    }
}

I should be able to call it with this

var myEmptyClass = NullObject.Create<MyClass>();

Where I'm having issues is inside of the foreach loop, with this line

var propertyInstance = NullObject.Create<property.PropertyType>();

...obviously that doesn't work, but how can I accomplish creating a "null object" to assign to the instance I'm currently creating.

EDIT: And what about generics?? I'd like empty instances created

  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);
  }
هل كانت مفيدة؟

المحلول

You can create a non-generic method and use it:

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;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top