我可以将成员定义/约束为实现两个接口,而不是泛型吗? [英] Can I define/constrain a member as implementing two interfaces, without generics?

查看:154
本文介绍了我可以将成员定义/约束为实现两个接口,而不是泛型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码显示了我想要做的事情;也就是说,我想约束anObject,这样它就可以用作各种方法的参数,使用IInterfaceOne或IInterfaceTwo,它们都不会继承另一个。

The following code shows what I would like to do; that is, I would like to constrain anObject, so that it can be used as a parameter to various methods with use IInterfaceOne or IInterfaceTwo, where neither inherits from the other.

public interface IInterfaceOne { }
public interface IInterfaceTwo { }

public class Implementation : IInterfaceOne, IInterfaceTwo
{
}

public interface IInterfaceOneAndTwo : IInterfaceOne, IInterfaceTwo { }

public class UsingImplementation
{
    IInterfaceOneAndTwo anObject = (IInterfaceOneAndTwo)(new Implementation()); //fails because Implementation doesnt acctually implement IInterfaceOneAndTwo
}

然而这个例子失败了,因为IInterfaceOneAndTwo是一个接口本身,而实现并没有实现它。

This example fails however as IInterfaceOneAndTwo is an interface in its own right, and Implementation does not implement it.

我知道如果我使用泛型我可以约束它们,但我想知道,如果有办法这样做没有泛型?

I know if I used generics I could constrain them, but I am wondering, if there is a way to do this without generics?

有没有办法说 anObject 应该实现 IInterfaceOne IInterfaceTwo ,不使用 IInterfaceOneAndTwo

Is there a way to say anObject shall implement IInterfaceOne and IInterfaceTwo, without using IInterfaceOneAndTwo?

推荐答案

传入泛型类参数和泛型方法参数可以组合类型,但是没有用于表示复合的变量或字段的工具类型。此外,为了将对象传递给组合多个约束的泛型类型的参数,必须将对象强制转换为实际上实现所有这些约束的类型。这可能很困难。

"Incoming" generic class parameters and generic method parameters can combine types, but there is no facility for variables or fields to represent "composite" types. Further, in order to pass an object to a parameter of a generic type which combines multiple constraints, the object must be cast to a type which in fact implements all of those constraints. This can be difficult.

例如,假设类 Foo Bar 都实现 Intf1 Intf2 。人们希望编写一个函数 AddToList< T>(T作为T),其中T:Intf1,Intf2 。这样的函数将非常乐意接受 Foo Bar 类型的对象。但是,假设有人希望使用这样的函数将所有对象添加到同一个列表中(可能是 Foo Bar ,以及任何其他类型的碰巧实现 Intf1 Intf2 )以及稍后将这些对象传递给一个函数,该函数的参数同样受到限制,以实现 Intf1 Intf2 。可以转换为 Foo 碰巧是 Foo 的任何对象,并转换为 Bar 碰巧是 Bar 的任何对象,但是如果编写的其他类型也处理 Intf1 Intf2 ,很难处理它们。

For example, suppose class Foo and Bar both implement Intf1 and Intf2. One wishes to write a function AddToList<T>(thing as T) where T:Intf1,Intf2. Such a function will perfectly happily accept objects of type Foo or Bar. Suppose, however, one wishes to use such a function to add all objects to the same list (which might be a mix of Foo, Bar, and any number of other types that also happen to implement Intf1 and Intf2) and then later pass those objects to a function whose parameter is likewise constrained to implement both Intf1 and Intf2. One could cast to Foo any object which happened to be a Foo, and cast to Bar any object which happened to be a Bar, but if other types are written which also handle Intf1 and Intf2, it would be difficult to deal with them.

有可能解决问题,有点笨拙,没有使用反射或其他这样的技巧。使用方法 ActUpon< thingType> ActUpon(thingType thing)定义接口 IActUpon< Base1,Base2> where thingType:Base1,Base2 。这种方法的实现将能够将参数 thing 传递给需要通用方法参数约束到 Base1 和<的其他方法code>和Base2 。这种方法最大的困难在于必须为每个可能的约束条件编写单独的代码,并且在许多地方使用lambda表达式的情况下,必须编写 IActUpon的实现。 ...

It is possible to solve the problem, somewhat awkwardly, without using Reflection or other such tricks. Define an interface IActUpon<Base1, Base2> with a method ActUpon<thingType>ActUpon(thingType thing) where thingType: Base1, Base2. Implementations of such a method will be able to pass parameter thing to other methods requiring generic method parameter constrained to Base1 and Base2. The biggest difficulties with such an approach are that one must write separate code for each possible number of constraints, and that in many places where one would have used a lambda expression one will instead have to write an implementation of IActUpon....

这篇关于我可以将成员定义/约束为实现两个接口,而不是泛型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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