我想消除以下代碼片段中的類型測試。
有沒有人有任何想法我可以做到這一點?C#中的後期綁定#
public override T Value { get { // I want to use late binding here so that the following call is made: // bOK = _FolderParmT(ref _value, strFParamVal)); // Let the compiler figure out at runtime which func to call based T's type. // Until I do that I'll have to parse the type. // Be sure to update the constructor if you add a new type T TVal = _value; if (_value is string) { string v = null; bOK = _FolderParmT(ref v, strFParamVal); if (bOK) TVal = ((T)((object)(v))); } else if (_value is System.String[]) { string[] v = null; bOK = _FolderParmT(ref v, strFParamVal); if (bOK) TVal = ((T)((object)(v))); } else if (_value is double) { double v = double.MinValue; bOK = _FolderParmT(ref v, strFParamVal); if (bOK) TVal = ((T)((object)(v))); } else if (_value is int) { int v = int.MinValue; bOK = _FolderParmT(ref v, strFParamVal); if (bOK) TVal = ((T)((object)(v))); } else if (_value is long) { long v = long.MinValue; bOK = _FolderParmT(ref v, strFParamVal); if (bOK) TVal = ((T)((object)(v))); } else if (_value is bool) { bool v = false; bOK = _FolderParmT(ref v, strFParamVal); if (bOK) TVal = ((T)((object)(v))); } if (bOK) { base.SendMessage("\"" + _strFolderParameter + "\" setting: \"" + strFParamVal +"\" was used"); return TVal; }
你的問題/目標是什麼?性能?易於維護?您的目標與選擇合適的解決方案相關。 – dvdvorle 2011-03-12 22:03:23