Associating a variable with a class in C# (for JSON) -
I have to capture the JS provided the API for a project, and this means that JSON using a class Deserialising, and I have chosen to go with the DataContract
classes. In any case, different URL APIs are tailored to different JSON outputs.
So what am I asking? To see if the DataContract
sections are public T GetObject & lt; T & gt; There is no better way of URL to create (string yuri)
and it's GetObject & lt; ExampleDataContract & gt; ("Http: // blah /")
.
The code below shows my current endeavor, which I am thinking is not absolutely good idea, not to mention the fact that if I have ever changed the name space, then I I'm a little fun.
PublicT GetObject & lt; T & gt; () {String uri = ""; String type = typef (t). Ostring (); Switch (type) {case "namespace. Example Dot Contract": Yuri = "http://www.example.com/blah.json"; break; } Returns JsonHelper.Deserialize & lt; T & gt; (This.GetJson (uri)); }
There is a problem I see from your perspective: if you have two different Are the URLs, returning both a example data contract
? Using your method, you will be able to get one of them, because the URL determines specifically.
In any case, you are right to get rid of the magic string "namespace example data contract"
is a good idea if
use an option If there is to be done, then some more questions have been shown in more.
PublicT GetObject & lt; T & gt; () {String uri = ""; If (typef (t) == typef (namespace.example.dacontract)) {uri = "http://www.example.com/blah.json"; } And if (typef (t) == ...) {...} Other {... // throw some exception} Return JsonHelper.Deserialize & lt; T & gt; (This.GetJson (uri)); }
Comments
Post a Comment