I have an object model where a large number of entities share a common structure, defined in interfaces. Here’s a sample object and the interfaces
public class PhoneBookCategory : IIdItem, INamedItem
{
public string Id { get; set; }
public string Name { get; set; }
}
public interface IIdItem
{
string Id { get; set; }
}
public interface INamedItem
{
string Name { get; set; }
}
I then have a geric search method that works on the Name
property. I’m starting with an IMongoQueryable<T>
. I then check if T is an INamedItem
:
if (typeof(INamedItem).IsAssignableFrom(typeof(T))
If true, I’d like to run a query on an IMongoQueryable<INamedItem>^. But.. it appears that an ?
IMongoQueryableis not an
IMongoQueryble even if T implements INamedItem. So, how'd I go t about it? The funny thing is, ìMongoQueryable<T>
IS an IMongoQueryable<PhoneBookCategory>
so casting works as expected. But I don’t want to write a search for every type of T (my app has a large number of them), I’d like to handle every T that is an INamedItem in one go.