0

Dear StackOverflow lovers,

I'm having my XmlRoot overwritten whenever I try to serialize a object from a generic class.

=> I get as XmlRoot: classname + _x0060_1 (by the way, there is a 1 because there is only one Type parameters given, in other word it could be any number of type parameter needed)

Even if I use

[XmlRoot("BaseSyncOneWayInput")]

or even

XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = typeof(T).Name;

Here is a .NET Fiddle to reproduce to bug: https://dotnetfiddle.net/X9fRkA

  • Should find your answer here: https://stackoverflow.com/a/1237827/1462295 -> https://dotnetfiddle.net/vEm141 – BurnsBA Sep 26 '17 at 15:41
  • thanks! I c'ant wait to try this back at work tomorow – Nathan Smiechowski Sep 26 '17 at 20:24
  • If you use that answer, you should cache the `XmlSerializer` to avoid a memory leak as explained [here](https://stackoverflow.com/questions/23897145/memory-leak-using-streamreader-and-xmlserializer). – dbc Sep 26 '17 at 23:22
  • @dbc is that still the case? It appears the constructor now implements a cache: https://github.com/Microsoft/referencesource/blob/90b323fe52bec428fe4bd5f007e9ead6b265d553/System.Xml/System/Xml/Serialization/XmlSerializer.cs (e.g. line 184) – BurnsBA Sep 27 '17 at 11:22
  • Thank you @BurnsBA that does the trick – Nathan Smiechowski Sep 27 '17 at 13:58
  • 1
    @BurnsBA - it's still [documented](https://learn.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlserializer?view=netcore-2.0#Remarks) to be true even in .Net core 2.0: *If you use any of the other constructors, multiple versions of the same assembly are generated and never unloaded, which results in a memory leak and poor performance. ... you must cache the assemblies in a Hashtable*. – dbc Sep 27 '17 at 17:58
  • isn't what the using is for ? to dispose – Nathan Smiechowski Sep 28 '17 at 09:17

1 Answers1

0

I don't see the issue. I added a test class to your code and modified you main() method to use class. Results work.

        static void Main(string[] args)
        {
            MyListClass[] myListClass = new MyListClass[3];
            Console.WriteLine(Pull<string>("","",myListClass));
        }
    }
    public class MyListClass
    {
        string myProperty { get; set; }
    }
jdweng
  • 33,250
  • 2
  • 15
  • 20