1

I want to create simple factory class which implements interface like this:

IFactory 
{
   TEntity CreateEmpty<TEntity>(); 
}

In this method I want to return an instance of type TEntity (generic type). Example:

TestClass test = new Factory().CreateEmpty<TestClass>(); 

Is it possible? Does interface is correct?

I've tried something like this:

private TEntity CreateEmpty<TEntity>() {
   var type = typeof(TEntity);
   if(type.Name =="TestClass") {
      return new TestClass();
   }
   else {
     ...
   }
}

But it doesn't compile.

3 Answers 3

6

You need to specify the new() constraint on the generic type parameter

public TEntity CreateEmpty<TEntity>() 
    where TEntity : new()
{
    return new TEntity();
}

The new constraint specifies that the concrete type used must have a public default constructor, i.e. a constructor without parameters.

public TestClass
{
    public TestClass ()
    {
    }

    ...
}

If you don't specify any constructors at all, then the class will have a public default constructor by default.

You cannot declare parameters in the new() constraint. If you need to pass parameters, you will have to declare a dedicated method for that purpose, e.g. by defining an appropriate interface

public interface IInitializeWithInt
{
     void Initialize(int i);
}

public TestClass : IInitializeWithInt
{
     private int _i;

     public void Initialize(int i)
     {
         _i = i;
     }

     ...
}

In your factory

public TEntity CreateEmpty<TEntity>() 
    where TEntity : IInitializeWithInt, new()
{
    TEntity obj = new TEntity();
    obj.Initialize(1);
    return obj;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for comprehensive answer.
2
interface IFactory<TEntity> where T : new()
{
   TEntity CreateEmpty<TEntity>(); 
}

Comments

2

This method will help you, pass parameters in that order, in which they in constructor:

private T CreateInstance<T>(params object[] parameters)
{
    var type = typeof(T);

    return (T)Activator.CreateInstance(type, parameters);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.