你的代码没有意义。这个
responseJson
对象不能是
Dictionary<string, string>
和A
string
同时。能够发布真正的代码供我们工作是件好事。
下面是一个重构的例子,它确实可以编译,但是需要一些工作才能在运行时正常运行。尽管如此,你所需要的只是一个替代的过载来让它工作。
public IAjaxResponse GetResponse<TOk, TFail>(string response)
{
var responseJson = new Dictionary<string, object>();
object obj = null;
if ((string)responseJson["status"] == "ok")
obj = Newtonsoft.Json.JsonConvert.DeserializeObject<TOk>(response);
else
obj = Newtonsoft.Json.JsonConvert.DeserializeObject<TFail>(response);
return (IAjaxResponse)obj;
}
public IAjaxResponse GetResponse<TOk>(string response)
{
return (IAjaxResponse)Newtonsoft.Json.JsonConvert.DeserializeObject<TOk>(response);
}
第二种方法甚至可以是:
public IAjaxResponse GetResponse<TOk>(string response)
{
return GetResponse<TOk, FailDontCare>(response);
}
这样就避免了代码重复。