The myType
in the two statements play very different roles:
public object sqlLeggi(string sql, Type myType){
Here, myType
is a Type
object, referencing an instance of the Type class.
var results = cmd.ExecuteQuery< myType > ();
Here, myType
is a Type identifier, which is a syntactic construct referring to a specific type, one that would actually be named myType
in this case.
Now, there is usually two ways to handle this specific problem:
- Look at the object type in
cmd
and see if there is an overload or alternative method toExecuteQuery
that takes aType
object parameter instead - Make your method generic so that you don't have a
Type
object to begin with.
The first case would presumably be written in this way:
var results = cmd.ExecuteQuery(myType);
The second like this:
public myType sqlLeggi<myType>(string sql{
SQLiteCommand cmd = DB.CreateCommand(sql);
var results = cmd.ExecuteQuery< myType > ();
[..]
}
Note that:
- I made the method return
myType
instead ofobject
myType
is now specified as a generic parameter to the method:sqlLeggi<myType>
Naming convention in this case would dictate that your generic type parameter be named T
or something beginning with T
so here is my advice:
public T sqlLeggi<T>(string sql{
SQLiteCommand cmd = DB.CreateCommand(sql);
var results = cmd.ExecuteQuery<T>();
[..]
}