C# …不能在此上下文中使用…序列化时

C# …不能在此上下文中使用…序列化时,c#,serialization,web-services,C#,Serialization,Web Services,我有webservice和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<Cou

我有webservice和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...
}

有趣的是,当集合只有一个元素时[webmethod]可以正常工作,但当它断开的元素越多时,您需要向序列化程序和WSDL指示
Cars
可以是
ModelCar
,使用:

您需要指明从该方法返回的可能类型。在返回对象的方法签名中,序列化程序不知道运行时返回的实际类型

在序列化示例中:

XmlSerializer ser = new XmlSerializer(
    typeof(object), 
    new Type[] { 
        // List all the possible types here that you are using
        typeof(Foo),
        typeof(Bar)
    }
);

我将
object
更改为
objects[]

我仍然有错误:ModelCar[]可能无法在此上下文中使用…
[WebMethod]
[XmlInclude(typeof(ModelCar))]
public object GetObjects(Cars[] cars)
{
    return Translator.ToObjects(Facade.GetObjects(cars);
}
XmlSerializer ser = new XmlSerializer(
    typeof(object), 
    new Type[] { 
        // List all the possible types here that you are using
        typeof(Foo),
        typeof(Bar)
    }
);