简体   繁体   中英

Is it possible to make a parameter implement two interfaces?

Is it possible to define a function that takes in a parameter that must implement two interfaces?

(The two interfaces are ones I just remembered off the top of my head; not the ones I want to use)

private void DoSomthing(IComparable, ICollection input)
{

}

You can:

1) Define an interface that inherits both required interfaces:

public interface ICombinedInterface : IComparable, ICollection {... }

private void DoSomething(ICombinedInterface input) {... }

2) Use generics:

private void DoSomething<T>(T input)
    where T : IComparable, ICollection
{...}

您可以从这两个接口继承另一个接口,并使您的参数实现该接口。

Well, yes, and no.

You can, as Steve has suggested, create another, third, interface which descends from the two you want, and use that for the parameter type.

However, this will also make it a requirement that the class being used implements that third interface as well.

In other words, this won't work:

public interface I1 { }
public interface I2 { }
public class C : I1, I2 { }

public interface I3 : I1, I2 { }
public class YourClass
{
    public void Test(I3 i) { }
}

...
YourClass yc = new YourClass();
C c = new C();
yc.Test(c); // won't work, C does not implement I3

However, you can trick the compiler into what you want by way of generics.

public class YourClass
{
    public void Test<T>(T i) where T: I1, I2 { }
}

Now it will work. I'm still not 100% sure that won't give you other issues though, I'd have to think about it.

The generic function approach mentioned above is a good one, with one big caveat: in order to typecast an object so that it can be passed to a routine with multiple generic constraints, one must know a type which meets those constraints and is a parent type of the object being cast. A routine that accepts such an object will know such a type (it was passed in as a generic type parameter) but there's no nice way for a class to persist such information and use it later. If a variety of unrelated types implement both IFoo and IBar, it would be difficult to design a routine which could accept a number of unrelated object instances, store them in a list or something, and then pass all the items in the list to a routine with a generic IFoo+IBar parameter.

If such a scenario might be necessary, the best approach would be to have a non-generic counterpart for each generic routine which would accept eg a parameter of type IFoo and cast it to an IBar as needed. One could then store all the items in a List<IFoo> and pass them to the routine. One would lose the type-safety of the generic method, but sometimes perfect type-safety isn't achievable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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