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

前端 未结 7 1732
我寻月下人不归
我寻月下人不归 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 19:07

    Does the C# 4.0 dynamic keyword get you out of jail (mostly) free? After all - you are already doing the type checking.

    interface IC : IA, IB { }
    
    void bar(object obj)
    {
      if (obj is IA && obj is IB)
      {
        IC x = (dynamic)obj;
        foo(x);
      }
    }
    

    Does that break if foo tries to cast the parameter to T? I don't know.

提交回复
热议问题