Casting an object to two interfaces at the same time, to call a generic method

前端 未结 7 1735
我寻月下人不归
我寻月下人不归 2020-12-09 18:27

I want to call a generic method that constrains the input type T to implement two interfaces:

interface IA { }
interface IB { }
void foo(T t) where          


        
7条回答
  •  自闭症患者
    2020-12-09 18:58

    I agree with the other responders that you probably have a design issue if you need to do this, but you could accomplish it with a proxy object that implements both interfaces and delegates the calls to the two casted interface instances of the unknown Object. Now, when you call this method, you can construct the proxy for any type that supports both interfaces.

提交回复
热议问题