有没有办法声明一个在.Net中实现多个接口的变量? [英] Is there a way to declare a variable that implements multiple interfaces in .Net?

查看:124
本文介绍了有没有办法声明一个在.Net中实现多个接口的变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此Java问题类似。我想指定一个变量实现多个接口。例如

Similar to this Java question. I would like to specify that a variable implements multiple interfaces. For instance

private {IFirstInterface, ISecondInterface} _foo;

public void SetFoo({IFirstInterface, ISecondInterface} value)
{
    _foo = value;
}

要求:


  • 我无法为大多数传入Foo的类型添加接口。所以我不能创建一个继承自IFirstInterface和ISecondInterface的第三个接口。

  • 我想避免在可能的情况下使包含类通用,因为Foo的类型没有多少在编译时,用户不太可能知道它。

  • 我需要在以后使用foo访问两个接口中的方法。

  • 我想以编译器安全的方式执行此操作,即在尝试使用它之前不尝试强制转换为接口。如果foo没有实现这两个接口,相当多的功能将无法正常工作。

  • I don't have the ability to add an interface to most type that would be passed in to Foo. So I can't create a third interface that inherits from IFirstInterface and ISecondInterface.
  • I would like to avoid making the containing class generic if possible because the type of Foo doesn't have much to do with the class and the user isn't likely to know it at compile time.
  • I need to use foo to access methods in both interfaces at a later time.
  • I would like to do this in a compiler safe way, i.e. no trying to cast to the interface just before trying to use it. If foo does not implement both interfaces quite a bit of functionality won't work properly.

这可能吗?

编辑:出于各种原因,我想要这几次。在这个例子中,因为我正在将一组属性从ObservableCollections更改为ReadOnlyObservableCollections。我有一个帮助器类,它创建从源ObservableCollection到另一个Collection的投影。由于ReadOnlyObservableCollection不从ObservableCollection继承而且我只需要IList和INotifyCollectionChanged中的操作,我希望将我的源集合存储为这两个接口的组合而不是需要ObservableCollection。

Edit: I've wanted this a few times for various reasons. In this instance it's because I am changing a set of properties from ObservableCollections to ReadOnlyObservableCollections. I have a helper class that creates a projection from a source ObservableCollection to another Collection. Since ReadOnlyObservableCollection does not inherit from ObservableCollection and I only need operations in IList and INotifyCollectionChanged I was hoping to store my source collection as a combination of these two interfaces rather than requiring an ObservableCollection.

推荐答案

如果你想约束你的方法 SetFoo 只采用实现这两个接口的参数,那么你 in 运气:

If you want to constrain your method SetFoo to take only parameters that implement these two interfaces, then you are in luck:

public void SetFoo<T>(T value) where T : IFirstInterface, ISecondInterface
{
    _foo = value;
}

从此时开始,只要您想访问 _foo 成员作为这两个接口之一,您只需通过强制转换来访问它:(IFirstInterface)_foo (ISecondInterface)_foo ,分别。

From this point on, any time you want to access your _foo member as one of either of these two interfaces, you'll just have to access it via casts: (IFirstInterface)_foo or (ISecondInterface)_foo, respectively.

你确实说你想避免在编译时使用强制转换安全;但我认为,如果你找到一种方法来确保使用上面的 SetFoo 初始化 _foo ,你可以投出所有你想要安心。

You did say you'd like to avoid resorting to casts for compile-time safety; but I think, if you figure out a way to ensure that _foo gets initialized using SetFoo above, you can cast all you want with peace of mind.

我刚才意识到下面描述了你在问题中明确说明的内容没有想做。所以,我说,按照上面的说法。

I just realized the below describes something you specifically stated in the question you didn't want to do. So, I say, go with the above.

另一个想法是,因为它看起来像这个 _foo object是一个类成员(我将这个假设基于变量名中的 _ ),你可以用这种方式定义你的类:

Another idea would be, since it looks like this _foo object is a class member (I'm basing this assumption on the _ in the variable name), you could define your class in this way:

class YourClassThatHasAFoo<T> where T : IFirstInterface, ISecondInterface {
    private T _foo;

    public void SetFoo(T value) {
        _foo = value;
    }
}

这实际上是否有意义显然取决于你的具体情况。你肯定要考虑一下,因为有这种限制的类有时会导致你可能没想到的问题(例如 SetFoo 现在必须采用 T 类型的参数,而不仅仅是实现两个接口的任何类。)

Whether this actually makes sense depends, obviously, on your specific scenario. You'll definitely have to think it through, as classes with constraints like this sometimes lead to issues you might not have expected down the road (such as that SetFoo now must take a parameter of type T, not just any class that implements your two interfaces).

这篇关于有没有办法声明一个在.Net中实现多个接口的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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