如何重新解读C#中的演员阵容?
我定义了这样一个函数:如何重新解读C#中的演员阵容?,c#,templates,generics,boxing,reinterpret-cast,C#,Templates,Generics,Boxing,Reinterpret Cast,我定义了这样一个函数: 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);
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服务添加到任何函数中
我想,你已经有了一个想法,我想做什么
我想定义一个函数,它可以通过将模型实例装箱到System.Object
并为JSON响应取消装箱来接受来自不同数量其他函数的调用
<> >类似于C++中的代码> RealTytCase函数。我想,我可以用C#和泛型
,但我对C#的这一部分不太了解,所以我建议您提供帮助
谢谢 这就行了。。但是,您应该知道,这比您可能期望的更安全(如果您打开了
reinterpret\u cast
):
如果只是随机抛出类型,则可以期望抛出异常。这将起作用。。但是,您应该知道,这比您可能期望的更安全(如果您打开了
reinterpret\u cast
):
如果只是随机抛出类型,则可以期望抛出异常。通常的方法是静态多态性:
public static void WriteResponse<T>(ref HttpContext ctx, object sender)
where T : class
{
var model = sender as T;
// ...
}
或者,你可以使整个演员阵容含蓄(感谢@Aik指出这一点)
publicstaticvoidwriteresponse(参考HttpContext-ctx,T模型)
{
// ...
}
通常的方法是静态多态性:
public static void WriteResponse<T>(ref HttpContext ctx, object sender)
where T : class
{
var model = sender as T;
// ...
}
或者,你可以使整个演员阵容含蓄(感谢@Aik指出这一点)
publicstaticvoidwriteresponse(参考HttpContext-ctx,T模型)
{
// ...
}
通过在C中使用泛型,您可以将其编码为:
public static void WriteResponse<T>(HttpContext ctx, T sender)
{
var jsonObject = new JavaScriptSerializer().Serialize(sender);
ctx.Response.AddHeader("Access-Control-Allow-Origin", "*");
ctx.Response.ContentType = "Content-type: application/json";
ctx.Response.ContentEncoding = Encoding.UTF8;
ctx.Response.Write(jsonObject);
}
internal void GetShortInfo(HttpContext ctx, Shapefile shapefile)
{
var model = new Models.SfShortInfo
{
Count = shaefile.Count,
Type = shapefile.Type.ToString()
};
ShapefileService.WriteResponse(ctx, model);
}
publicstaticvoidwriteresponse(HttpContext-ctx,T发送方)
{
var jsonObject=new JavaScriptSerializer().Serialize(发送方);
ctx.Response.AddHeader(“访问控制允许来源”,“*”);
ctx.Response.ContentType=“内容类型:应用程序/json”;
ctx.Response.ContentEncoding=Encoding.UTF8;
ctx.Response.Write(jsonObject);
}
内部void GetShortInfo(HttpContext ctx,Shapefile Shapefile)
{
var模型=新模型。SfShortInfo
{
Count=shaefile.Count,
Type=shapefile.Type.ToString()
};
ShapefileService.WriterResponse(ctx,模型);
}
这样,就不需要执行任何强制转换,因为它将作为它已经需要知道的类型进入函数。通过在C中使用泛型,您可以这样编码它:
public static void WriteResponse<T>(HttpContext ctx, T sender)
{
var jsonObject = new JavaScriptSerializer().Serialize(sender);
ctx.Response.AddHeader("Access-Control-Allow-Origin", "*");
ctx.Response.ContentType = "Content-type: application/json";
ctx.Response.ContentEncoding = Encoding.UTF8;
ctx.Response.Write(jsonObject);
}
internal void GetShortInfo(HttpContext ctx, Shapefile shapefile)
{
var model = new Models.SfShortInfo
{
Count = shaefile.Count,
Type = shapefile.Type.ToString()
};
ShapefileService.WriteResponse(ctx, model);
}
publicstaticvoidwriteresponse(HttpContext-ctx,T发送方)
{
var jsonObject=new JavaScriptSerializer().Serialize(发送方);
ctx.Response.AddHeader(“访问控制允许来源”,“*”);
ctx.Response.ContentType=“内容类型:应用程序/json”;
ctx.Response.ContentEncoding=Encoding.UTF8;
ctx.Response.Write(jsonObject);
}
内部void GetShortInfo(HttpContext ctx,Shapefile Shapefile)
{
var模型=新模型。SfShortInfo
{
Count=shaefile.Count,
Type=shapefile.Type.ToString()
};
ShapefileService.WriterResponse(ctx,模型);
}
这样,就不需要进行任何转换,因为它将以它已经需要的类型进入函数。这是一个不需要修复的问题,但需要避免。事实上,使用将模型强制转换为一种类型或另一种类型是无用的,因为当您将其传递到
序列化时,它将再次成为对象
只需从代码中完全删除这一行:
var model = sender as typeName;
JavaScriptSerializer
将处理所有细节。这是一个不需要解决的问题,但需要避免。事实上,使用将模型强制转换为一种类型或另一种类型是无用的,因为当您将其传递到序列化时,它将再次成为对象
只需从代码中完全删除这一行:
var model = sender as typeName;
JavaScriptSerializer
将处理所有细节。您根本不需要强制转换。JavaScriptSerializer.Serialize方法具有以下原型:
public string Serialize(Object obj)
这意味着您的“未装箱”模型将再次装箱,您不必这样做。在内部,函数使用反射,并且能够读取所有模型元数据,即使它作为对象传递给方法
对HttpContext
使用ref
是不必要的,而且有点危险,因为HttpContext
是引用类型,您可以在调用的方法中更改引用。我从未见过使用ref关键字传递引用类型的好理由。它主要对值类型有用
您的解决方案可以如下所示:
public static void WriteResponse(HttpContext ctx, object model)
{
string 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);
}
第二点是,您不必将模型强制转换为对象
,您可以直接传递模型:
ShapefileService.WriteResponse(ctx, model);
如果在WriteResponse
方法中确实需要强类型,则可以使用泛型方法:
public static void WriteResponse<T>(HttpContext ctx, T model)
{
string 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);
}
ShapefileService.WriteResponse<Models.SfShortInfo>(ctx, model);
publicstaticvoidwriteresponse(HttpContext-ctx,T-model)
{
字符串jsonObject=newJavaScriptSerializer().Serialize(模型);
ctx.Response.AddHeader(“访问控制允许来源”,“*”);
ctx.Response.ContentType=“内容类型:应用程序/json”;
ctx.Response.ContentEncoding=Encoding.UTF8;
ctx.Response.Write(jsonObject);
}
ShapefileService.WriterResponse(ctx,模型);
您根本不需要强制转换。JavaScriptSerializer.Serialize方法具有以下原型:
public string Serialize(Object obj)
这意味着您的“未装箱”模型将再次装箱,您不必这样做。在内部,函数使用反射,并且能够读取所有模型元数据,即使它作为对象传递给方法
对HttpContext
使用ref
是不必要的,而且有点危险,因为HttpContext
是引用类型,您可以在调用的方法中更改引用。我从来没有看到过使用ref传递引用类型的好理由