我已经定义了这样一个函数:
public static void WriteResponse(ref HttpContext ctx, object sender, Type typeName)
{
    var model = sender as typeName; // it's an error-line, becase of `as typeName`
    var jsonObject = new JavaScriptSerializer().Serialize(model);
    ctx.Response.AddHeader("Access-Control-Allow-Origin", "*");
    ctx.Response.ContentType = "Content-type: application/json";
    ctx.Response.ContentEncoding = Encoding.UTF8;
    ctx.Response.Write(jsonObject);
}
如您所见,它甚至无法编译,因为以下行:
var model = sender as typeName;
我从那个调用这个函数:
internal void GetShortInfo(ref HttpContext ctx, ref Shapefile shapefile)
{
    var model = new Models.SfShortInfo()
    {
        Count = shapefile.Count,
        Type = shapefile.Type.ToString()
    };
    ShapefileService.WriteResponse(ref ctx, (object)model, model.GetType());
}
还有这样的电话:
ShapefileService.WriteResponse(ref ctx, (object)model, model.GetType());
我想从自行设计的 API Web 服务中添加任何功能。
我想,你有一个想法,我正在尝试什么TO DO。
我想定义一个函数,它可以通过将模型实例System.Object装箱并为 JSON 响应拆箱来接受来自各种数量的其他函数的调用。
它类似于reinterpret_cast<>C++ 中的函数。我认为,我可以在 C# 中使用Generics.
谢谢!