c# - Rest Call "Unable to Cast Type Base to Class Derived" -


on server side, have following structure -

[datacontract] [knowntype(typeof(derived1))] [knowntype(typeof(derived2))] public class base {  }  [datacontract] public class derived1 : base {     [datamember]     public string prop1 {get;set;}     [datamember]     public string prop2 {get;set;} }  [datacontract] public class derived2 : base {     [datamember]     public string prop3 {get;set;}     [datamember]     public string prop4 {get;set;}         [datamember]     public string prop5 {get;set;} }   [datacontract] public class superclass  {     [datamember]     public list<base> listbase; }  [operationcontract(method="post")] public void somename(superclass superobj); 

on client side, make ajax call operation contract takes in superclass object parameter.

var dataobj = { listbase : [{"prop1":"this is","prop2":"first derived class"},                             {"prop3":"this is","prop4":"second","prop5":"derived class"}                            ]               }; $.ajax({     url : 'myproperlyconstructedserviceurlasspecifiedintheoperationcontract'     data : json.stringify(dataobj),     method : "post",     contenttype:'application/json' }); 

now, while constructing json object on client side, pass objects of both derived1 , derived2 in list<base>.

the error "unable cast object of type base object of type derived1".

is there way in operationcontract on server side can 'know' of derived object cast into? possible? (seems illogical me, i'm unable understand underlying concept...too reading got me confused!)

what should design approach in such situations? example or concept should read situation?


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -