将DataType作为参数发送?

soo*_*ise 5 c# methods types

我正在尝试编写一个使用以下两个参数的方法:

ColumnToSort
ColumnType
Run Code Online (Sandbox Code Playgroud)

我希望能够做到这一点的原因是解释两件事,因为字符串可以给出与将相同的两件事作为数字相比较不同的结果.例如

String: "10" < "2"
Double: 10 > 2 
Run Code Online (Sandbox Code Playgroud)

所以基本上,我希望能够发送double或string数据类型作为方法参数,但我不知道如何做到这一点,但它似乎应该可以在C#中实现.

附录:

我希望我的方法看起来像什么:

InsertRow(customDataObj data, int columnToSort, DataType dataType){
    foreach(var row in listView){
        var value1 = (dataType)listView.Items[i].SubItems[columnToSort];
        var value2 = (dataType)data.Something;
        //From here, it will find where the data object needs to be placed in the ListView and insert it
    }
}
Run Code Online (Sandbox Code Playgroud)

它将如何被称为:

I think the above provides enough of an explanation to understand how it will be called, if there are any specific questions, let me know. 
Run Code Online (Sandbox Code Playgroud)

Bal*_*a R 6

您可以使用Type 参数类型.像这样

void foo(object o, Type t)
{
 ...
}
Run Code Online (Sandbox Code Playgroud)

并打电话

Double d = 10.0;
foo(d, d.GetType());
Run Code Online (Sandbox Code Playgroud)

要么

foo(d, typeof(Double));
Run Code Online (Sandbox Code Playgroud)