5

if i have this code:

public interface IJobHelper
{
    List<T> FilterwithinOrg<T>(IEnumerable<T> entities) where T : IFilterable;
}

is there anything that support doing something like this:

public interface IJobHelper
{
    List<T> FilterwithinOrg<T>(IEnumerable<T> entities) where T : IFilterable or ISemiFilterable
}

so it will accept anything that supports one of two interfaces. I am basically trying to create an overload.

Daniel Imms
  • 47,944
  • 19
  • 150
  • 166
leora
  • 188,729
  • 360
  • 878
  • 1,366
  • Why not make a generic filterable class, then inherit that class with more specific filterable classes. You can then use the where clause with the generic filterable class and constrain to multiple classes. – Austin Brunkhorst Apr 22 '13 at 03:36
  • http://stackoverflow.com/questions/3679562/generic-methods-and-method-overloading – Turbot Apr 22 '13 at 03:40

5 Answers5

3

As far as I know, you can use AND logic not OR.

AND (in this case, T must be child of IFilterable and ISemiFilterable)

public interface IJobHelper
{
    List<T> FilterwithinOrg<T>(IEnumerable<T> entities) where T : IFilterable, ISemiFilterable
}
Vano Maisuradze
  • 5,829
  • 6
  • 45
  • 73
2

No, the language does not support that.

Gjeltema
  • 4,122
  • 2
  • 24
  • 31
2

The easy way I would say, is if both your interfaces where children of the same parent interface.

public interface IFilterable { }

public interface IFullyFilterable : IFilterable { }

public interface ISemiFilterable : IFilterable { }

... where T : IFilterable { }
LightStriker
  • 19,738
  • 3
  • 23
  • 27
2

The language doesn't support 'oring' together interfaces/classes in the where clause.

You need will need to state them separately with different method names so the signatures are different.

public interface IJobHelper
{
    List<T> FilterwithinOrg<T>(IEnumerable<T> entities) 
        where T : IFilterable
    List<T> SemiFilterwithinOrg<T>(IEnumerable<T> entities) 
        where T : ISemiFilterable
}

Alternatively you can implement a common interface on both interfaces. This is not the same thing as above though as it may require a cast when you receive the object back if you need a specific interface that isn't contained within IBaseFilterable.

public interface IBaseFilterable { }
public interface IFilterable : IBaseFilterable { }
public interface ISemiFilterable : IBaseFilterable { }

public interface IJobHelper
{
    List<T> FilterwithinOrg<T>(IEnumerable<T> entities)
        where T : IBaseFilterable
}

I don't know the context but the above is probably what you're looking for.

Daniel Imms
  • 47,944
  • 19
  • 150
  • 166
  • I tried the first thing by just listing it twice in the interface with different Where constraints but i get "same method is already declared" error . . any ideas? – leora Apr 22 '13 at 03:42
  • Ah true, I'll fix the answer. You'll need to give them different names – Daniel Imms Apr 22 '13 at 03:49
0

You can just add another empty base interface that both these interfaces are derived from...

 interface baseInterface {}
 interface IFilterable: baseInterface {}
 interface ISemiFilterable: baseInterface {}

and then demand that the type in the generic type constraint be the base interface

 List<T> FilterwithinOrg<T>(IEnumerable<T> entities) where T : baseInterface 

The only downside is the compiler will not allow you to use methods from either of the derived interfaces without casting...

Charles Bretana
  • 143,358
  • 22
  • 150
  • 216