我正在尝试概括我编写的解决方案,以便可以将其应用于类似的问题。
我有许多不同的对象,它们都包含可为空的双精度数。我想以某种方式将它们(双打)传递到字典中,然后将数据直接放入相关对象中。
如果双打是引用类型,这将非常简单,但它们不是。
所以我需要一个通过引用来引用它们的解决方案。我唯一能想到的就是创建我自己的包含双精度类型的类,但这需要大量工作,因为我使用了许多双精度类型的代码——据我所知,你不能扩展值类型。
关于我如何去做的任何想法?
添加 - 这是我正在尝试做的事情的示例代码示例。这不是实际的代码。
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);