C# generics method invocation

Posted by Firat KÜÇÜK on Stack Overflow See other posts from Stack Overflow or by Firat KÜÇÜK
Published on 2010-03-29T17:52:09Z Indexed on 2010/03/29 18:03 UTC
Read the original article Hit count: 279

Filed under:
|
|

Hi, i have some polymorphic methods and i want to call via using an intermediate method. Following class is the simplified version of my program.

class Program {

  public class A {
  }

  public class B {
  }

  public class C {
  }

  public void SomeMethod(A value) {
    Console.WriteLine("A value");
  }

  public void SomeMethod(B value) {
    Console.WriteLine("B value");
  }

  public void SomeMethod(C value) {
    Console.WriteLine("C value");
  }

  static void Main(string[] args) {
    Program p = new Program();

    // code block

    p.IntermediateMethod<A>(new A());
    p.IntermediateMethod<B>(new B());
    p.IntermediateMethod<C>(new C());
  }

  public void IntermediateMethod<T>(T value) {

    // code block

    SomeMethod(value);

    // code block
  }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about generics