C#C
C#2y ago
13 replies
holipop

Getting the typeof this to use in a generic method

I have a class with a method that requires a different class that has a generic method. How can I get the type of the first class to pass into the generic method?

public class Foo
{
  void GenericMethod<T> () {}
}

public class Bar
{
  void Pass (Foo foo)
  {
    // I want to get Bar to use as T but I'm unsure how to search for this problem and this doesn't work.
    Type T = this.GetType();
    foo.GenericMethod<T>();
  }
}
Was this page helpful?