I’ve got a method I wish to pass an SqlExpression and connection string to load data for a given type. Problem is I can’t seem to nail down the syntax. My thought is to be able to call a static method like:
OrmLiteConfig.DialectProvider = ServiceStack.OrmLite.MySqlDialect.Provider;
SqlExpressionVisitor<SampleItem> ev = OrmLiteConfig.DialectProvider.ExpressionVisitor<SampleItem>();
var result = SomeClass.Query<SampleItem>(ev, connectionString);
With a method declared in SomeClass as:
public static List<T> Query<T>(SqlExpressionVisitor<T> ev, string connectionString)
{
IDbConnection conn = connectionString.OpenDbConnection();
var result = conn.Select<T>(ev);
return result;
}
However, the way I am calling the Select method gives me the syntax error:
“Error 10 ‘T’ must be a non-abstract type with a public parameterless constructor in order to use it as parameter ‘T’ in the generic type or method “
I am new to generics (obviously).
So, you need to ensure that
Thas a parameterless constructor. That simply means applying the following generic constraint:Your class likely already meets the conditions, you just need to ensure that someone else can’t pass in a non-constructable type.
The reason for the error is that the
Selectmethod you’re calling adds these same constraints. You are essentially maintaining the same requirements for your method.