0

With generic-types, it's possible to give a dynamic type, but is it possible to also give a dynamic value ?

For example, I receive an XML response from a Rest API and I want to Deserialize it into an object:

<tsResponse>
   <value id="hello"/>
</tsResponse>
[XmlRoot("tsResponse")]
public class TsResponse<T, string myNodeName>
{
    [XmlElement(myNodeName)]
    public T Object { get; set; }
}

public class User
{
    [XmlAttribute("id")]
    public string Id { get; set; }
}

To make it works I need to call something like this:

using (StringReader sr = new StringReader(xml))
{
    XmlSerializer serializer = new XmlSerializer(typeof(TsResponse<User>));
    TsResponse<User, "value"> responseXml = (TsResponse<User, "value">)serializer.Deserialize(sr);
}

Is it possible to do something like this ?

Dai
  • 141,631
  • 28
  • 261
  • 374
thibsc
  • 3,747
  • 2
  • 18
  • 38
  • What is the context when you say _"With template, it's possible to give a dynamic type"_? What "template" are you referring to? (Do you mean an XSD Schema and/or XML DTD? If so, then those aren't templates...) – Dai Nov 02 '22 at 15:47
  • @Dai A class template something like `public class A` – thibsc Nov 02 '22 at 15:48
  • 1
    Generic-types aren't templates. Despite many similarities, they are fundamentally different concepts, and C# does not support templates. – Dai Nov 02 '22 at 15:48
  • 1
    That's not possible. Generics are a **compile-time**-thing, whereas you want to provide something at **runtime**. Furthermor `XmlElement` - as attributes in general - only work with compile-time literals, so you cannot provide a dynamic value to them. – MakePeaceGreatAgain Nov 02 '22 at 15:51
  • What you're after isn't directly achievable in C#/.NET as generic-type parameters are _always_ type-names (and never values), but what you're ultimately trying to accomplish [_might_](https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/static-virtual-interface-members) be possible with C# 11's `static interface` members... depending on if it supports `const` members of interface types, then you could pass that to `XmlElement( here )`. Hmmm.... **UPDATE:** Nevermind, only `static abstract` members can, not `const` members. – Dai Nov 02 '22 at 15:52
  • @MakePeaceGreatAgain Generics are both exported and reified in C#/.NET and support runtime instantiation, so calling them "a compile-time thing" is very inaccurate. – Dai Nov 02 '22 at 15:52
  • c# doesn't allow generic parameters to appear in attribute argument lists, apparently because the attribute argument list is evaluated at compile time, but the generic parameter will only be known in runtime. See [Why does C# forbid generic attribute types?](https://stackoverflow.com/q/294216/3744182), [Using the name of a generic type in a class attribute](https://stackoverflow.com/q/56564398/3744182), – dbc Nov 02 '22 at 20:20
  • But perhaps [C#: Best way to have XML element name from generic type name](https://stackoverflow.com/q/38344516/3744182) does what you need? – dbc Nov 02 '22 at 20:22

0 Answers0