【发布时间】:2012-08-01 09:17:39
【问题描述】:
我正在尝试概括我编写的解决方案,以便可以将其应用于类似的问题。
我有许多不同的对象,它们都包含可为空的双精度值。我想以某种方式将它们(双打)传递到字典中,然后将数据直接放入相关对象中。
如果双精度类型是引用类型,这将非常简单,但它们不是。
所以我需要一个通过引用来引用它们的解决方案。我唯一能想到的就是创建自己的包含双精度类型的类,但由于我使用了大量双精度类型的代码,所以工作量很大——据我所知,你不能扩展值类型。
有什么想法可以解决吗?
已添加 - 这是我正在尝试做的事情的示例代码示例。这不是实际的代码。
void ReadTable(Dictionary<string,double?> dict)
{
//read some sort of table here by usign the string as the headers
dict["header"] = Convert.toDouble(tableValue);
//etc...
}
MyObject myObject = new MyObject();
//fill it up from the table
Dictionary<string,double?> req = new Dictionary<string,double?>();
req.add("header",myObject.something);
req.add("header2",myObject.somethingElse);
ReadTable(req);
MyOtherObject myOtherObject = new MyOtherObject();
//fill it up from the table
Dictionary<string,double?> req2 = new Dictionary<string,double?>();
req2.add("aheader",myOtherObject.m2something);
req2.add("aheader2",myOtherObject.m2somethingElse);
ReadTable(req2);
【问题讨论】:
-
您能再解释一下您的问题吗?你到底想做什么?能否提供一个代码示例(即使它不起作用)?
-
我写了一些示例代码来澄清一下。
标签: c# pass-by-reference nullable