我有以下方法:private /*some type*/ abc(string a, string b){ //... return new { success = false, responseText = "Input-Values not valid" };}对象不是我搜索的类型,因为我无法访问abcReturn.success并且abcReturn.responseText在第二种方法中。还是有其他方法可以访问successand的值responseText?因为在调试时,我可以看到它abcReturn确实包含了 success 和 responseText 的值。我想以这种方式返回它,因为在第三种方法中,我想做return Json(abc(), JsonRequestBehavior.AllowGet);
2 回答

哈士奇WWW
TA贡献1799条经验 获得超6个赞
您不能在 c# 中返回匿名对象,但可以返回一个命名元组,如下所示:
private (bool success, string responseText) abc(string a, string b)
{
return (false, "Input-values not valid");
}

慕沐林林
TA贡献2016条经验 获得超9个赞
为什么不用字典?
using System.Collections.Generic;
private Dictionary<bool, string> abc(string a, string b) {
//...
Dictionary<bool, string> returnVal = new new Dictionary<bool, string>()
{
{"success", false},
{"responseText", "Input-Values not valid"},
};
return returnVal;
}
然后要访问返回的值,您将执行以下操作:returnVal["success"]
- 2 回答
- 0 关注
- 109 浏览
添加回答
举报
0/150
提交
取消