1

Of course the answer to the question above is, "you can't". But I didn't have a better way of describing what I'm looking for. If I did, I wouldn't be asking this question.

My problem stems from a C# project but I'm pretty sure it applies to other languages as well. I have an interface definition like:

public interface IMultipleParentInstrument : IInstrument
{
    /// <summary>
    /// Filters the given <paramref name="query"/> by the <paramref name="parentId"/>
    /// </summary>
    /// <param name="query">Query to filter</param>
    /// <param name="parentId">Id of the parent to filter by</param>
    /// <returns>A filtered query</returns>
    IQueryable<...> WhereHasParent(IQueryable<...> query, int parentId);
}

(... == IMultipleParentInstrument, for the sake of readability)

And the implementation of that method is called like this:

if (typeof (IMultipleParentInstrument).IsAssignableFrom(typeof (TChild)))
    result = (new TChild() as IMultipleParentInstrument)
                .WhereHasParent(repo.GetAllQueryable()
                    .OfType<IMultipleParentInstrument>(), parentId)
                .OfType<TChild>();  

(TChild is a generic parameter. Which, when this IF branch is entered, is derived from IMultipleParentInstrument. The new() contraint is applied to TChild above, so it's bound to have a default constructor)

My issue is that I have to create an instance of TChild to call the WhereHasParent method. I could have made it a static method, but then I wouldn't be able to call it "on the interface" or enfore it's implementation. Plus I'd have to know the exact type to call the static method or use reflection, neither will work in this situation because the exact type is not known (generic method) and reflection will hurt performance too bad (method is used quite a bit).

I'd like to be able to enforce an implementation of the WhereHasParent method for all implementors. But, as you can see, I'd like to be able to use the function as if it were a static method (without creating an instance).

The method implementation needs to be able to filter the IQuerable based on the parentId. Thus, in most cases, it will end up looking like this:

public IQueryable<...> WhereHasParent(IQueryable<...> query, int parentId)
{
    return query.Where(p =>
                !(p is TypeImplementingTheInterface) ||
                (p as TypeImplementingTheInterface).ACollection.Any(m => m.ParentId == parentId));
}

Of course this is a simplified example. Not all ParentId fields are called ParentId, so you can't just guess them with reflection.

I have a feeling I'm approaching the problem the wrong way so I'm hoping someone can point me in the right direction. And I'd like to appologize for the formulation of the question, I really can't think of a better way to put it (feel free to edit if you do).

2
  • Silly question, but here it goes: Why does it absolutely have to be static? Any static method can be converted into a non-static method, and you'd save yourself so much trouble.
    – Neil
    Dec 15, 2014 at 12:43
  • I think your problems began when you started using generics. Try redesigning your application so you don't have any TChildren. Also, assuming you are using EF, I would be really worried about performance of those kind of queries.
    – Euphoric
    Dec 15, 2014 at 13:02

0

Browse other questions tagged or ask your own question.