What I have?
I have an abstract class, QueryExecutor and derived class, SqlQueryExecutor shown below.
abstract class QueryExecutor<T>
{
public abstract T Execute();
}
class SqlQueryExecutor<T> : QueryExecutor<T> where T:ICollection
{
public override T Execute()
{
Type type = typeof(T);
// Do common stuff
if (type == typeof(ProfileNodeCollection))
{
ProfileNodeCollection nodes = new ProfileNodeCollection();
// Logic to build nodes
return (T)nodes;
}
else
{
TreeNodeCollection nodes = new TreeNodeCollection();
Logic to build nodes
return (T)nodes;
}
}
}
What I want to do?
In the implementation of Execute() method, I want to construct the appropriate ICollection object and return it.
What problem am I facing?
In Execute() method, the line, return (T)nodes; shows the following compile time error:
Cannot convert type 'WebAppTest.ProfileNodeCollection' to 'T'
Any idea how can I solve this?
Thanks in advance!