...may not be used in this context...while serialization
I've webservice and WebMethod
[webMethod]
[XMLInclude(typeof(ContractCar[]))
public object GetObjects(Cars[] cars)
{
return Translator.ToObjects(Facade.GetObjects(cars);
}
public static object GetObjects(Cars cars)
{
List<Car> cars =new List<Country(...fillingcollection)
return cars.ToArray(),
}
public static object ToObjects(object collection)
{
if(collection is Car[])
{
return ConvertModelCarsToContractCars(collection),
}
public ContractCar[] ConvertModelCarsToContractCars(Cars[] collection)
{
...there is rewriting pool...
}
And I get exception at side of client:
There was an error generating the XML document.
I'm using this function to check collection which I would send to the client and it works.
public static void SerializeContainer(object obj)
{
try
{
// Make sure even the construsctor runs inside a
// try-catch block
XmlSerializer ser = new XmlSerializer(typeof(object));
TextWriter w = new StreamWriter(开发者_如何转开发@"c:\list.xml");
ser.Serialize(w, obj);
w.Close();
}
catch (Exception ex)
{
DumpException(ex);
}
}
Interesting is when collection has only One element [webmethod] works fine, but when is more it brokes
I changed object
into objects[]
.
You need to indicate to the serializer and in the WSDL that a Cars
can be a ModelCar
using the XmlIncludeAttribute:
[WebMethod]
[XmlInclude(typeof(ModelCar))]
public object GetObjects(Cars[] cars)
{
return Translator.ToObjects(Facade.GetObjects(cars);
}
You need to indicate what is the possible type that you are returning from the method. As in the method signature you are returning object, the serializer doesn't know about the actual type you are returning at runtime.
And in your serialization example:
XmlSerializer ser = new XmlSerializer(
typeof(object),
new Type[] {
// List all the possible types here that you are using
typeof(Foo),
typeof(Bar)
}
);
精彩评论