Alex Rivera | Logout

Passing just a type as a parameter in C#

Asked 2012-06-08T20:20:26.220
176

Hypothetically it'd be handy for me to do this:

foo.GetColumnValues(dm.mainColumn, int)
foo.GetColumnValues(dm.mainColumn, string)

where the GetColumns method will call a different method inside depending on the type passed.

Yes, I could do it as a boolean flag or similar, I just wondered if there was a way to perhaps pass this, and then ask:

typeof(arg[1]) or similar...

I could also override the method, use generics, etc - I know there are different ways to do this, I was just curious if this was possible.

Edit
Report

1 Answer

2

You can do this, just wrap it in typeof()

foo.GetColumnValues(typeof(int))

public void GetColumnValues(Type type)
{
    //logic
}
answered 2012-06-08T20:22:31.297

Your Answer