c# - Dealing with an object of certain type, that can be known in runtime only? -



c# - Dealing with an object of certain type, that can be known in runtime only? -

i have question, have asp.net web app can service multiple clients.

the difference app needs convert json string .net object of type <t> in order pass object wcf service.

for purpose using newtonsoft.json.converter:

t input = jsonconvert.deserializeobject<t>(jsoninput);

the problem that type <t> unknown @ design time. after deserialization need have generic strongly-typed object can pass wcf service. reason wcf service different each client, , parameter input may have different construction each client.

var client = new calcengine.calculatorclient(); var input = new calcengine.calcinputtypes(); var result = client.calculate(input);

here input of type calcengine.calcinputtypes. clienta , clientb calcengine.calcinputtypes may have different structure.

what best way accomplish that?

thanks.

for example, suppose input either of next 2 json messages:

{ "type": "gizmo", "value": { "name": "banana" } } { "type": "widget", "value": { "widget_id": 7, "data": [4, 2, 6, 3] } }

you decide how deserialize incoming message inspecting type , deserializing value known .net type. can using jtoken:

var input = jtoken.parse(jsoninput); var type = input["type"].toobject<string>(); if (type == "gizmo") { var gizmo = input["value"].toobject<gizmo>(); } else if (type == "widget") { var widget = input["value"].toobject<widget>(); }

c# asp.net .net json jint

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -