I have a few generic classes that implement a common non-generic interface. I create my generic objects and add them to a list. How can I use LINQ, or any other method for that matter, to filter the list by the generic type. I do not need to know T at run-time. I added a type property to the interface and used LINQ to filter by it but I was hoping to use the is operator. Here's a simple example I threw together.
Any Ideas?
interface IOperation
{
object GetValue();
}
class Add<T> : IOperation
{
public object GetValue()
{
return 0.0;
}
}
class Multiply<T> : IOperation
{
public object GetValue()
{
return 0.0;
}
}
private void Form1_Load(object sender, EventArgs e)
{
//create some generics referenced by interface
var operations = new List<IOperation>
{
new Add<int>(),
new Add<double>(),
new Multiply<int>()
};
//how do I use LINQ to find all intances off Add<T>
//without specifying T?
var adds =
from IOperation op in operations
where op is Add<> //this line does not compile
select op;
}