determine method parameter type from generic typed class

67 views Asked by At

I have the following situation:

public class CustomDataGridView<T> : DataGridView 
{
   method1();
   ...
}

class ChannelsDataGridView : CustomDataGridView<Channel>
{
   ...
}

class NetworksDataGridView : CustomDataGridView<Network>
{
   ...
}

and I need method:

public void Method(TYPE sender)
{
   sender.method1();
}

What should be the TYPE in this method or how could I achieve this functionality?

3

There are 3 answers

0
clcto On BEST ANSWER

It appears you want a generic method:

public void Method<T>( CustomDataGridView<T> sender )

Note if this is in a generic class that already uses T for a generic parameter than you should use a different letter:

public void Method<U>( CustomDataGridView<U> sender )
0
ie. On

You should define Method as generic as well:

public void Method<T>(CustomDataGridView<T> sender)
{
   sender.method1();
}
1
Rich O'Kelly On

A generic method will do the trick:

public void Method<T>(CustomDataGridView<T> sender)

MSDN has some good formal documentation on these; but for some more interesting use cases Joel Abrahamsson has a good blog post.