我想概括一下我編寫的解決方案,以便可以應用於類似的問題。將可空類型轉換爲參考的簡潔方法
我有許多不同的對象,都包含可爲空的雙打。我想以某種方式將它們(雙打)傳遞到字典中,然後將數據直接放入所討論的對象中。
如果雙打是參考類型,這將工作相當簡單,但他們不是。
所以我需要一個解決方案來引用他們的參考。我能想到的唯一的事情就是創建自己的包含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