Search code examples
c#web-servicestostringderived-class

Passing a derived class to a web service method, as the base class


I have a C# class imported from a web service. Due to debugging issues and arrays, I needed this class to have a ToString() override or DebuggerDisplayAttribute, and the only way I found was adding the former to a derived class:

class ExtWebServiceDataClass: WebServiceDataClass
{
    public override string ToString()
    {
        return String.Format("stuff");
    }
}

Unfortunately, the webservice methods demand an array of base objects and do not accept an array of derived objects (XML serialization error):

System.InvalidOperationException: Error generating the XML document.

→ System.InvalidOprrationException: The type ExtWebServiceDataClass was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.

Is there any way to make it accept it from the client side? An attribute that says it's just like the base class, just serialize it as such?

Or an easy way to mass-convert from the derived class to the base class?

Or more simply, another way to have a ToString override or DebuggerDisplayAttribute on the client side?


Solution

  • WebServiceDataClass is declared as a partial class in a generated file (like most generated classes are). You can define your DebuggerDisplayAttribute or ToString in another file in the same project, like so:

    [DebuggerDisplay("MyString")]
    public partial class WebServiceDataClass
    {
    }