How to call a method overload based on closed generic type?

后端 未结 6 2112
时光取名叫无心
时光取名叫无心 2021-02-13 10:20

Suppose I have three methods:

void Foo(MemoryStream v) {Console.WriteLine (\"MemoryStream\");}
void Foo(Stream v)       {Console.WriteLine (\"Stream\");}
void Fo         


        
6条回答
  •  不要未来只要你来
    2021-02-13 11:08

    Perhaps something like this:

    void Bar()
    {
       if(typeof(T) == typeof(Stream))
          Foo(default(T) as Stream);  //just to show the scenario
    }
    

提交回复
热议问题