本文整理汇总了C#中System.Web.Script.Serialization.JavaScriptSerializer.JsonErrorResponse方法的典型用法代码示例。如果您正苦于以下问题:C# Script.Serialization.JavaScriptSerializer.JsonErrorResponse方法的具体用法?C# Script.Serialization.JavaScriptSerializer.JsonErrorResponse怎么用?C# Script.Serialization.JavaScriptSerializer.JsonErrorResponse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Web.Script.Serialization.JavaScriptSerializer
的用法示例。
在下文中一共展示了Script.Serialization.JavaScriptSerializer.JsonErrorResponse方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: JsonCall
public static string JsonCall(object source, string name, string[] jsonArgs, string[] excludedMethodNames)
{
var jss = new JavaScriptSerializer();
var type = source.GetType();
var method = type.GetMethod(name, BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.FlattenHierarchy);
if(method == null)
return jss.JsonErrorResponse("Method not found");
if(!GetOperationContractMethods(type, excludedMethodNames).Any(m => m.Name == name))
return jss.JsonErrorResponse("Access to this method is not permitted");
var prms = method.GetParameters();
if(prms.Length != jsonArgs.Length)
return jss.JsonErrorResponse("The method takes " + prms.Length + " arguments but " + jsonArgs.Length + " arguments were supplied");
object[] args = new object[prms.Length];
for(var i=0; i < args.Length; i++)
{
var prmType = prms[i].ParameterType;
object val;
try
{
val = jss.Deserialize(jsonArgs[i], prmType);
}
catch(Exception ex)
{
return jss.JsonErrorResponse("Unable to deserialize argument " + i + " to parameter type " + prmType.FullName + ": " + ex.Message);
}
args[i] = val;
}
object returnVal = null;
try
{
if(method.ReturnType == typeof(void))
method.Invoke(source, args);
else
returnVal = method.Invoke(source, args);
}
catch(Exception ex)
{
return jss.JsonErrorResponse("Method call failed: " + ex.Message);
}
try
{
return jss.Serialize(returnVal);
}
catch(Exception ex)
{
return jss.JsonErrorResponse("Failed to serialize return value: " + ex.Message);
}
}