以下返回值的有效 C# 数据类型是什么,或者如何访问内部属性?

我有以下方法:


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);


慕的地6264312
浏览 110回答 2
2回答

哈士奇WWW

您不能在 c# 中返回匿名对象,但可以返回一个命名元组,如下所示:private (bool success, string responseText) abc(string a, string b){    return (false, "Input-values not valid");}

慕沐林林

为什么不用字典?using System.Collections.Generic;private Dictionary<bool, string> abc(string a, string b) {&nbsp; &nbsp; //...&nbsp; &nbsp; Dictionary<bool, string> returnVal = new new Dictionary<bool, string>()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; {"success", false},&nbsp; &nbsp; &nbsp; &nbsp; {"responseText", "Input-Values not valid"},&nbsp; &nbsp; };&nbsp; &nbsp; return returnVal;}然后要访问返回的值,您将执行以下操作:returnVal["success"]
打开App,查看更多内容
随时随地看视频慕课网APP