C# 中的条件类型参数
Conditional type argument in C#
我想将对象反序列化为给定的 class 类型,具体取决于 ajax 响应是否成功。
所以我写了下面的方法:
public IAjaxResponse GetResponse<TOk, TFail>()
{
var responseJson = this.response as Dictionary<string, object>;
object obj = null;
if ((string)responseJson["status"] == "ok")
obj = JsonConvert.DeserializeObject<TOk>(responseJson);
else
obj = JsonConvert.DeserializeObject<TFail>(responseJson);
return (IAjaxResponse)obj;
}
现在使用起来非常简单:
var response = GetResponse<ClassWhenOk, ClassWhenFail>();
if (response is ClassWhenFail responseFail) {
Error.Show(responseFail.message);
return;
}
[..]
现在我的问题是:有时,有些通用响应恰好总是 'ok' 状态,所以我不想使用第二种类型的参数失败状态。
所以我想使用类似的东西:
\/ notice one type argument
GetResponse<ClassWhenOk>();
这是不允许的,因为使用这个泛型方法需要 2 个类型参数。
所以我的问题来了:
我能否以某种方式将第二类参数 (TFail
) 标记为 'not required'?或者我应该采用不同的方法?
你的代码没有意义。 responseJson
对象不能同时是 Dictionary<string, string>
和 string
。如果能够 post 真正的代码供我们工作,那就太好了。
这是一个编译后的重构示例,但需要一些工作才能在 运行 时正常运行。尽管如此,您所需要的只是一个替代重载来完成这项工作。
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);
}
这正好避免了代码重复。
我想将对象反序列化为给定的 class 类型,具体取决于 ajax 响应是否成功。
所以我写了下面的方法:
public IAjaxResponse GetResponse<TOk, TFail>()
{
var responseJson = this.response as Dictionary<string, object>;
object obj = null;
if ((string)responseJson["status"] == "ok")
obj = JsonConvert.DeserializeObject<TOk>(responseJson);
else
obj = JsonConvert.DeserializeObject<TFail>(responseJson);
return (IAjaxResponse)obj;
}
现在使用起来非常简单:
var response = GetResponse<ClassWhenOk, ClassWhenFail>();
if (response is ClassWhenFail responseFail) {
Error.Show(responseFail.message);
return;
}
[..]
现在我的问题是:有时,有些通用响应恰好总是 'ok' 状态,所以我不想使用第二种类型的参数失败状态。
所以我想使用类似的东西:
\/ notice one type argument
GetResponse<ClassWhenOk>();
这是不允许的,因为使用这个泛型方法需要 2 个类型参数。
所以我的问题来了:
我能否以某种方式将第二类参数 (TFail
) 标记为 'not required'?或者我应该采用不同的方法?
你的代码没有意义。 responseJson
对象不能同时是 Dictionary<string, string>
和 string
。如果能够 post 真正的代码供我们工作,那就太好了。
这是一个编译后的重构示例,但需要一些工作才能在 运行 时正常运行。尽管如此,您所需要的只是一个替代重载来完成这项工作。
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);
}
这正好避免了代码重复。