A
Andrus
I need to create instance of generic type when generic type does not
have parameterless constructor.
I tried code below but got error shown in comment.
How to fix ?
Andrus.
abstract class QueryFactory<TEntity> {
public string O1;
public QueryFactory(string o1) {
O1 = o1;
}
}
class CustomerQueryFactory : QueryFactory<Customer> {
public CustomerQueryFactory(string o1) : base(o1) { }
}
class DocumentForm<TEntity, TQueryFactory>
where TQueryFactory : QueryFactory<TEntity> {
public DocumentForm() {
// Cannot create an instance of the variable type 'TQueryFactory'
// because it does not have the new() constraint
var x = new TQueryFactory("special parameter created in this method");
}
}
class Customer {
string Id { get; set; }
string Name { get; set; }
}
class Test {
static void Main() {
var x = new DocumentForm<Customer, CustomerQueryFactory>();
}
}
have parameterless constructor.
I tried code below but got error shown in comment.
How to fix ?
Andrus.
abstract class QueryFactory<TEntity> {
public string O1;
public QueryFactory(string o1) {
O1 = o1;
}
}
class CustomerQueryFactory : QueryFactory<Customer> {
public CustomerQueryFactory(string o1) : base(o1) { }
}
class DocumentForm<TEntity, TQueryFactory>
where TQueryFactory : QueryFactory<TEntity> {
public DocumentForm() {
// Cannot create an instance of the variable type 'TQueryFactory'
// because it does not have the new() constraint
var x = new TQueryFactory("special parameter created in this method");
}
}
class Customer {
string Id { get; set; }
string Name { get; set; }
}
class Test {
static void Main() {
var x = new DocumentForm<Customer, CustomerQueryFactory>();
}
}