There is a method with the following signature:

public IEnumerable<T> Find<T>(string predicate) where T : IDbModel; 

There is an abstract class (from which other classes will be inherited in the future), which implements this method:

 public IEnumerable<T> Find<T>(string predicate) where T : IDbModel { // do something //need to create instance of type T like this: T obj = new T() } 

Is it possible to create objects of generalized types?

ps I write a very, very modest version of Eloquent ORM as in Laravel

  • one
    maybe this link will help? - Andrei Khotko
  • one
    Add constraint: where T : IDbModel, new() . link - Alexander Petrov

1 answer 1

Yes, add constraint new() to the generic parameter, and you can create objects through new T() :

 public IEnumerable<T> Find<T>(string predicate) where T : IDbModel, new() { T obj = new T(); // ... }