我想概括一下我编写的解决方案,以便可以应用于类似的问题。将可空类型转换为参考的简洁方法
我有许多不同的对象,都包含可为空的双打。我想以某种方式将它们(双打)传递到字典中,然后将数据直接放入所讨论的对象中。
如果双打是参考类型,这将工作相当简单,但他们不是。
所以我需要一个解决方案来引用他们的参考。我能想到的唯一的事情就是创建自己的包含double的类,但是由于我使用了大量的Double代码,这是很多工作 - 而且我知道你不能扩展值类型。
有关我如何去做的任何想法?
新增 - 下面是我想要做的事情的示例代码示例。这不是实际的代码。
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);
你能解释一下你的问题吗?你究竟想要做什么?你可以给一个代码示例(即使它不起作用)吗? – sloth 2012-08-01 09:21:24
我写了一些示例代码来澄清。 – Aabela 2012-08-01 09:26:02