Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. XML Serialize / Deserialize generic dictionary holding various generic types

XML Serialize / Deserialize generic dictionary holding various generic types

Scheduled Pinned Locked Moved C#
helpquestioncsharpxml
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Florian Storck
    wrote on last edited by
    #1

    Hi, I'm kind of stuck with an serializing / deserializing problem using a generic dictionary holding references to various generic types. It goes as follows: class MyBase : IXmlSerializable { // whatever } class MyGeneric : MyBase, IXmlSerializable { MyGeneric(Valuetype tVal) { val = tVal; } ValueType val; } class Program { dictionary m_Dic = new dictionary(); void FillDictionary() { dictionary.Add("Key1", new MyGeneric(10)); dictionary.Add("Key1", new MyGeneric("StringValue")); dictionary.Add("Key1", new MyGeneric(3.1415)); } } Ok, I hope you can see the idea behind it. It's mainly thought to hold a variety of different types whithout specifying a parameter enum which selects the appropiate value via a huge switch statement on lots of overloads to the value Get/Set property. While I'm able to serialize the dictionary without a problem to an XML file, I'm stuck deserializing it. The problem: how can I can generate a generic from a textinformation like or MyApp.MyGeneric`1[System.String] ? The first classification is generated by the .Net serializer and the second is generated from typeof(...) . Any ideas are very appreciated. Thanks, Florian

    F 1 Reply Last reply
    0
    • F Florian Storck

      Hi, I'm kind of stuck with an serializing / deserializing problem using a generic dictionary holding references to various generic types. It goes as follows: class MyBase : IXmlSerializable { // whatever } class MyGeneric : MyBase, IXmlSerializable { MyGeneric(Valuetype tVal) { val = tVal; } ValueType val; } class Program { dictionary m_Dic = new dictionary(); void FillDictionary() { dictionary.Add("Key1", new MyGeneric(10)); dictionary.Add("Key1", new MyGeneric("StringValue")); dictionary.Add("Key1", new MyGeneric(3.1415)); } } Ok, I hope you can see the idea behind it. It's mainly thought to hold a variety of different types whithout specifying a parameter enum which selects the appropiate value via a huge switch statement on lots of overloads to the value Get/Set property. While I'm able to serialize the dictionary without a problem to an XML file, I'm stuck deserializing it. The problem: how can I can generate a generic from a textinformation like or MyApp.MyGeneric`1[System.String] ? The first classification is generated by the .Net serializer and the second is generated from typeof(...) . Any ideas are very appreciated. Thanks, Florian

      F Offline
      F Offline
      Florian Storck
      wrote on last edited by
      #2

      For the interested ones, I found a solution: Type t = Type.GetType(typestring); object o = Activator.CreateInstance(t); This works, if you serialize the type on saving with GetType().ToString().

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups