非严格的多接口类型参数约束? [英] Non-strict multiple interface type parameter constraints?

查看:124
本文介绍了非严格的多接口类型参数约束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这是一个骗子,请原谅,但我似乎无法找到正确的关键字组合来过滤掉各种类型约束和泛型问题(因为有很多).

Excuse me if this is a dupe, but I couldn't seem to get the right combo of keywords to filter down the various type constraint and generics questions out there (as there are a lot).

我有两个接口,我们称它们为 IOnline IOffline .

I have two interfaces--let's call them IOnline and IOffline.

它们之间有着密切的联系,因为它们描述了几乎相同的合同,但是它们之间的主要区别之一是将使用具体实现的上下文.这不完全是我的情况,但可以很好地说明问题.

They're closely related in that they describe nearly identical contracts, but one of the key differences between them is the context in which the concrete implementations will be used. It's not exactly my circumstances, but it illustrates the problem well.

然后我有了一些针对这些接口的具体实现者的方法.有时,这些方法只想处理一种类型,而不想处理另一种类型.

I then have some methods out there that do work against the concrete implementers of these interfaces. Sometimes these methods only want to deal with one type and not the other.

足够简单:

public void DoStuff<T>(string foo) where T : IOnline {}

踢球者正在为可以在EITHER类型上操作的方法实现代码.我认为这是正确的,但是在阅读编译错误时,我期望将约束解释为如果实现IOnline或IOffline,则允许在此处通用使用任何类型T",实际上被解释为允许任何类型如果它们同时实现,则将在此处通用.".

The kicker is implementing the code for methods that can operate on EITHER type. I thought this would be correct, but in reading the compilation error, my expectation that the constraint would be interpreted as "allow any type T to be used generically here if they implement IOnline OR IOffline", is actually being interpreted as "Allow any type T to be used generically here if they implement BOTH".

public void DoStuff<T>(string foo) where T : IOnline, IOffline {}

尝试实现两个具有相同名称但约束不同的单独方法会失败,因为存在明显的歧义问题-由于参数列表相同(因为所需的行为相同),因此我们不会超载.

Trying to implement two separate methods with the same name but different constraints fails as there's obvious ambiguity issues--we're not overloading since the parameter list is the same (since the desired behavior is identical).

可以为两种不同的方法使用两个不同的名称,每种方法都具有适当的约束,但这似乎很笨拙,并且使下游的其他事情变得很痛苦……可行,但不是理想.

I could use two different names for two different methods, each with the appropriate constraint, but that seems kludgy and makes other things downstream to be a pain in the ass...doable, but not ideal.

我觉得这里一定有我想念的东西...我在普通土地上感觉很舒服,但这是我第一次必须完成我所追求的,我觉得自己只是转动我的车轮atm.

I feel like there must be something I'm missing here... I feel perfectly comfortable in generic land but this is the first time I've ever had to accomplish what I'm after and I feel like I'm just spinning my wheels atm.

推荐答案

像您在第二个示例中那样提供多个约束确实是可加的. 关于通用约束的MSDN页面对此有所了解.

Supplying multiple constraints as in your second example is indeed additive. The MSDN page on generic constraints has a bit about this.

您可以使您的两个接口都从基本接口继承而来,并将方法限制为基本类型吗?

Can you make your two interfaces inherit from a base interface, and constrain the methods to the base type?

这篇关于非严格的多接口类型参数约束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆