简体   繁体   English

在C#中,你可以将Or放在“where”接口约束中吗?

[英]In C#, can you put an Or in an “where” interface constraint?

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. 我基本上试图创建一个重载。

As far as I know, you can use AND logic not OR . 据我所知,你可以使用AND逻辑而不是OR

AND (in this case, T must be child of IFilterable and ISemiFilterable ) AND (在这种情况下, T必须是IFilterable ISemiFilterable的子代

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

不,语言不支持。

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 { }

The language doesn't support 'oring' together interfaces/classes in the where clause. 该语言不支持where子句中的“oring”接口/类。

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 . 这与上面的内容不同,但如果您需要一个未包含在IBaseFilterable的特定接口,则在收到对象时可能需要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. 我不知道上下文,但上面可能是你正在寻找的。

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... 唯一的缺点是编译器不允许您使用来自任何派生接口的方法而不进行转换...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM