ContentPenalty
ContentPenalty

Reputation: 477

determine method parameter type from generic typed class

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?

Upvotes: 2

Views: 55

Answers (3)

Rich O&#39;Kelly
Rich O&#39;Kelly

Reputation: 41757

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.

Upvotes: 2

ie.
ie.

Reputation: 6101

You should define Method as generic as well:

public void Method<T>(CustomDataGridView<T> sender)
{
   sender.method1();
}

Upvotes: 1

clcto
clcto

Reputation: 9648

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 )

Upvotes: 2

Related Questions