Passing just a type as a parameter in C#

前端 未结 7 802
广开言路
广开言路 2020-11-28 21:21

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

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

where t

7条回答
  •  眼角桃花
    2020-11-28 21:57

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

    Alternatively, you could use a generic method:

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

    and you could then use it like:

    foo.GetColumnValues(dm.mainColumn);
    

提交回复
热议问题