如何编写将 returns class 类型指定为参数的 C# 函数?
How to write a C# Function that returns a class type speficifed as a parameter?
我正在尝试为 C# MVC 应用程序编写一个 JSON 帮助程序 class,该应用程序 returns 一个被指定为参数的类型的 class 实例。这样做的目的是简化将 JSON 反序列化为正确类型的模型。不幸的是,我 运行 陷入了一个错误:
'ModelType' is a variable but used as a type.
这是我使用的代码:
public ActionResult api_call() {
// Parse/Map request stream containing a JSON to a C# object
json_helper _o_json_helper = new json_helper();
pollread_read_request _request = _o_json_helper.parse_json_to_object(Assembly.GetExecutingAssembly().GetType("pollread_read_request"), Request.InputStream);
// Create a business logic instance
Pollmaker_business_logic _business_logic = new Pollmaker_business_logic();
// process request using business logic
List<pollread_read_response> _response = _business_logic.api_call(_request);
// return result
return Json(_response, JsonRequestBehavior.AllowGet);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
namespace Umbrella.Abstracts {
public class json_helper {
public Object parse_json_to_object(Type ClassModelType, System.IO.Stream InputStream) {
object _result = Activator.CreateInstance(ClassModelType);
try {
string _json_request = "";
using (System.IO.Stream _oRequestStream = InputStream) {
_oRequestStream.Seek(0, System.IO.SeekOrigin.Begin);
_json_request = new System.IO.StreamReader(_oRequestStream).ReadToEnd();
}
if (!String.IsNullOrEmpty(_json_request)) {
_result = Newtonsoft.Json.JsonConvert.DeserializeObject<ClassModelType> (_json_request);
}
} catch { }
return _result;
}
public json_helper() {}
}
}
您是否也尝试过只发送程序集名称和类型名称?
object _result = Activator.CreateInstance(ClassModelType.Assembly.FullName, ClassModelType.Name);
使用 DeserializeObject 的非泛型重载。
_result = Newtonsoft.Json.JsonConvert.DeserializeObject(_json_request, ClassModelType);
我正在尝试为 C# MVC 应用程序编写一个 JSON 帮助程序 class,该应用程序 returns 一个被指定为参数的类型的 class 实例。这样做的目的是简化将 JSON 反序列化为正确类型的模型。不幸的是,我 运行 陷入了一个错误:
'ModelType' is a variable but used as a type.
这是我使用的代码:
public ActionResult api_call() {
// Parse/Map request stream containing a JSON to a C# object
json_helper _o_json_helper = new json_helper();
pollread_read_request _request = _o_json_helper.parse_json_to_object(Assembly.GetExecutingAssembly().GetType("pollread_read_request"), Request.InputStream);
// Create a business logic instance
Pollmaker_business_logic _business_logic = new Pollmaker_business_logic();
// process request using business logic
List<pollread_read_response> _response = _business_logic.api_call(_request);
// return result
return Json(_response, JsonRequestBehavior.AllowGet);
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
namespace Umbrella.Abstracts {
public class json_helper {
public Object parse_json_to_object(Type ClassModelType, System.IO.Stream InputStream) {
object _result = Activator.CreateInstance(ClassModelType);
try {
string _json_request = "";
using (System.IO.Stream _oRequestStream = InputStream) {
_oRequestStream.Seek(0, System.IO.SeekOrigin.Begin);
_json_request = new System.IO.StreamReader(_oRequestStream).ReadToEnd();
}
if (!String.IsNullOrEmpty(_json_request)) {
_result = Newtonsoft.Json.JsonConvert.DeserializeObject<ClassModelType> (_json_request);
}
} catch { }
return _result;
}
public json_helper() {}
}
}
您是否也尝试过只发送程序集名称和类型名称?
object _result = Activator.CreateInstance(ClassModelType.Assembly.FullName, ClassModelType.Name);
使用 DeserializeObject 的非泛型重载。
_result = Newtonsoft.Json.JsonConvert.DeserializeObject(_json_request, ClassModelType);