0

私はそのような関数を定義しました:

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 応答用にボックス化解除することで、さまざまな数の他の関数からの呼び出しを受け入れることができる 1 つの関数を定義したいと考えています。

reinterpret_cast<>C++の関数に似ています。を使用してC#で実行できると思いますが、C#Genericsのその部分がよくわからないので、助けてください。

ありがとう!

4

5 に答える 5