c# - Serialization and dynamic type casting -
c# - Serialization and dynamic type casting -
i calling webservice user defined type parameter. next code work fine because type casting (for objectparameter) done known type @ time of time of calling service. unknown type how cast object original type? possible type info parameter of method called , conversion?
// web service proxy service1 ser=new service1(); compositetype mycompositetype=new compositetype(); mycompositetype.stringvalue = "arjun"; iformatter formatter = new binaryformatter(); stream stream = new filestream(@"d:/myfile.bin", filemode.create, fileaccess.write, fileshare.none); formatter.serialize(stream, mycompositetype); stream.close(); iformatter formatter1 = new binaryformatter(); stream stream1 = new filestream(@"d:/myfile.bin", filemode.open, fileaccess.read, fileshare.read); object objectparameter= formatter1.deserialize(stream1); //call remote webservice . casting possible because type known // doesn't work: ser.getdatausingdatacontract(objectparameter); ser.getdatausingdatacontract((compositetype)objectparameter);
you can check 'objectparameter' original type this:
if (objectparameter compositetype) ser.getdatausingdatacontract((compositetype)objectparameter); if (objectparameter compositetype2) ser.getdatausingdatacontract((compositetype2)objectparameter);
but think it's improve check 'objectparameter' type @ service - write 1 base of operations method getdatausingdatacontract object parameter type , within base of operations method phone call method original parameter type:
object objectparameter= formatter1.deserialize(stream1); ser.getdatausingdatacontract(objectparameter);
...
getdatausingdatacontract(object param) { // calling method original 'param' type if (param compositetype) homecoming getdatausingdatacontract((compositetype)param); if (param compositetype2) homecoming getdatausingdatacontract((compositetype2)param); homecoming null; }
c# type-conversion system.reflection
Comments
Post a Comment