gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 23:03:05 24 4
gpt4 key购买 nike

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com