c# - 发送数据类型作为参数?

标签 c# methods types

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

ColumnToSort
ColumnType

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

String: "10" < "2"
Double: 10 > 2 

所以基本上,我希望能够将 double 或字符串数​​据类型作为方法参数发送,但我不知道如何执行此操作,但它似乎在 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
    }
}

如何称呼:

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. 

最佳答案

您可以使用Type 作为参数类型。像这样

void foo(object o, Type t)
{
 ...
}

并调用

Double d = 10.0;
foo(d, d.GetType());

foo(d, typeof(Double));

关于c# - 发送数据类型作为参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5625622/

相关文章:

c# - 如何以MVVM方式为Windows Universal App实现条件导航/重定向

c# - 安装程序自定义操作 - 注册 COM dll - C#

java - 从方法中获取值

python - 从不同的 Python 文件调用方法

Scala:检查2个变量是否属于同一类型

javascript - Signalr Core 1.0.3 - 在页面刷新时保持连接

c# - 在 ASP.NET MVC 3 中将对象从一个操作方法传递到另一个操作方法

java记录行、列位置的方法

c# - 用 lambda 表达式替换字符串作为属性名称

ios - 无法将类型 'Result' 的值转换为预期的参数类型 'EmailContentData'