0

I have created a generic response class and xmlroot element of that class will change based on Class type. What change I need to do in order for class to dynamically update value? Trimmed down version of code:

[XmlRoot($"ResponseOfType{typeof(T)}", 
         Namespace = "http://schemas.datacontract.org/2004/07/Dell.CDA.Entitlement.Contracts.Models")]
public class Response<T>
{
    public Response()
    {

    }
    [XmlElement("result1", Order = 0)]
    public int? result1{ get; set; }
    [XmlElement("result2", Order = 0)]
    public int? result2{ get; set; }
}

ResponseOfType{typeof(T)} -- this line is throwing error, I don't want to hardcode the value, and I am not using any serializer rather depending upon c# to produce XML to send response back as XML.

dbc
  • 104,963
  • 20
  • 228
  • 340
  • The xml is the body of a response. XML is text and you would need to add the xml text (string) to the body of the response. – jdweng Jul 07 '23 at 09:32
  • You cannot do this, it is not supported by the .NET CLR. See e.g. [Using the name of a generic type in a class attribute](https://stackoverflow.com/q/56564398) or [call generic type T as an attribute in c#. i:e `[typeof(List)]`](https://stackoverflow.com/q/35336029). You will need some sort of workaround such as using a custom `XmlSerializer` or implementing `IXmlSerializable` + adding `[XmlSchemaProvider]`. Would either of those work for you? How complex is your actual `Response` model in practice? How is the type `T` used? – dbc Jul 24 '23 at 19:16

0 Answers0