Is there a way to declare a variable that implements multiple interfaces in .Net?

Posted by Bryan Anderson on Stack Overflow See other posts from Stack Overflow or by Bryan Anderson
Published on 2010-05-03T03:18:09Z Indexed on 2010/05/03 3:28 UTC
Read the original article Hit count: 369

Filed under:
|

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

Requirements:

  • 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.

Is this possible?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about interfaces