Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2006

Tip: Looking for answers? Try searching our database.

XML Serialize / Deserialize dictionary holding various generic typ

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
fstorck - 21 Aug 2006 11:47 GMT
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:

<code>

class MyBase : IXmlSerializable
{
// whatever
}

class MyGeneric<ValueType> : MyBase, IXmlSerializable
{
  MyGeneric(Valuetype tVal)
  {
    val = tVal;
  }
  ValueType val;
}

class Program
{
  dictionary<string, MyBase> m_Dic = new dictionary<string,MyBase>();

  void FillDictionary()
  {
     dictionary.Add("Key1", new MyGeneric<int>(10));
     dictionary.Add("Key1", new MyGeneric<string>("StringValue"));
     dictionary.Add("Key1", new MyGeneric<double>(3.1415));
  }
}
</code>

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

<MyGenericOfInt32> 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
Jared - 21 Aug 2006 12:21 GMT
You should be able to generate an instance using reflection.  Something
similar to this.

if (typeof(List<string>).IsGenericType)
           {
               Type t = typeof(List<string>).GetGenericTypeDefinition();
               object obj = Activator.CreateInstance(t);
           }

> Hi,
>
[quoted text clipped - 50 lines]
> Thanks,
>            Florian
fstorck - 21 Aug 2006 13:01 GMT
Hello Jared,

just for clearification:

>  if (typeof(List<string>).IsGenericType)
>             {
>                 Type t = typeof(List<string>).GetGenericTypeDefinition();

Does the typeof accepts a string like "MyApp.MyGeneric`1[System.String]" or
<MyGenericOfInt32>  for checking whether its generic and then create a type
definition from ? Or should I xml serialize the typedefinition  when ( which
caused an exception using the XMLSerialize class)...

> You should be able to generate an instance using reflection.  Something
> similar to this.
[quoted text clipped - 59 lines]
> > Thanks,
> >            Florian
fstorck - 21 Aug 2006 13:32 GMT
Hi Jared,

I found a solution:

On deserialization, I do the following

               Type t = Type.GetType(str_valtype);
               
               object o = Activator.CreateInstance(t);

So this generates a Type description from a supplied type string.

Thanks for helping!

> You should be able to generate an instance using reflection.  Something
> similar to this.
[quoted text clipped - 59 lines]
> > Thanks,
> >            Florian

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.