Passing just a type as a parameter in C#

237,586

Solution 1

There are two common approaches. First, you can pass System.Type

object GetColumnValue(string columnName, Type type)
{
    // Here, you can check specific types, as needed:

    if (type == typeof(int)) { // ...

This would be called like: int val = (int)GetColumnValue(columnName, typeof(int));

The other option would be to use generics:

T GetColumnValue<T>(string columnName)
{
    // If you need the type, you can use typeof(T)...

This has the advantage of avoiding the boxing and providing some type safety, and would be called like: int val = GetColumnValue<int>(columnName);

Solution 2

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

Alternatively, you could use a generic method:

public void GetColumnValues<T>(object mainColumn)
{
    GetColumnValues(mainColumn, typeof(T));
}

and you could then use it like:

foo.GetColumnValues<string>(dm.mainColumn);

Solution 3

You can pass a type as an argument, but to do so you must use typeof:

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

The method would need to accept a parameter with type Type.


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

If you want this behaviour then you should not pass the type as an argument but instead use a type parameter.

foo.GetColumnValues<int>(dm.mainColumn)

Solution 4

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

Or using generics:

foo.GetColumnValues<int>(dm.mainColumn);
foo.GetColumnValues<string>(dm.mainColumn);

Solution 5

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

foo.GetColumnValues(typeof(int))

public void GetColumnValues(Type type)
{
    //logic
}
Share:
237,586

Related videos on Youtube

Mark Mayo
Author by

Mark Mayo

Completed a first class Honours Degree in Computer Science back in 2002. Been jumping between development and test since, and my niche is probably developing automated test frameworks and systems. But I enjoy venturing into the unknown and trying new things and have worked in a variety of domains, from network communication to air traffic control to gaming platforms. Also love my travelling and photography, and am getting into writing. "In theory, this should work..."

Updated on August 01, 2020

Comments

  • Mark Mayo
    Mark Mayo almost 4 years

    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.

    • Major Productions
      Major Productions almost 12 years
      My thought exactly, depending on what foo actually is. foo.GetColumnValues<int>(dm.mainColumn) may be the way to go.
    • Mark Mayo
      Mark Mayo almost 12 years
      As I said, I realise there are other ways to do this (boolean flag, generics, overriding the method) I just wondered if it was possible as a parameter.
    • Tim Schmelter
      Tim Schmelter almost 12 years
      @MarkMayo: I don't understand the question if you "know that you could also override the method, use generics, etc and you know that there are different ways to do this, you were just curious if this was possible". So you know all this but you are curious if it is possible??
    • Mark Mayo
      Mark Mayo almost 12 years
      @TimSchmelter - in the form I describe. i.e. passing it as the 2nd parameter. As it turns out, Reed has kinda said what I was after - where you use (..., Type type). That's what I was looking for.
    • Chalky
      Chalky almost 10 years
      Good question, upvoted, I see MS using Type as a parameter for built-in operators in VB.NET e.g. trycast, and have often wished I could do that myself in C#/VB - in the fashion you describe.
  • JConstantine
    JConstantine almost 12 years
    I didn't dv you, but it was probably because you're showing how it would be called and didn't specify the function definition
  • Reaper
    Reaper almost 12 years
    I hate it when people need spoon feeding. The answer + a really brief read of MSDN is enough. I suspect that the down-voters are answerers competing of rep - how petty.
  • Joshua G
    Joshua G almost 10 years
    You can also do an extension method, public static T GetColumnValue<T>(this string columnName){...} then you can say foo.GetColumnValues<string>(dm.mainColumn)
  • BadmintonCat
    BadmintonCat over 8 years
    But how do you define a method that has several arguments and one of them should be a generic? Since the generic is defined before the method argument list, how do you know which one should be a generic then?
  • Reed Copsey
    Reed Copsey over 8 years
    @BadmintonCat T Foo<T,U>(string arg1, U arg2) or similar
  • Dave Cole
    Dave Cole over 5 years
    When using the first approach, is there a way to assign a default value to type? (e.g. something like object GetColumnValue(string columnName, Type type = object)? That doesn't quite seem to work for me but it would be useful to know.
  • timothy
    timothy over 3 years
    typeof() is a function that takes in a type like int or char or some class name. eg Type c = typeof(char); .Net does not provide us the same privilege. GetColumnValue<T> as mentioned above seems to be the best one can do.