473,473 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

XML Serialize / Deserialize dictionary holding various generic typ

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, MyBasem_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

<MyGenericOfInt32or 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
Aug 21 '06 #1
3 11757

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);
}

"fstorck" wrote:
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, MyBasem_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

<MyGenericOfInt32or 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

Aug 21 '06 #2
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)...
"Jared" wrote:
>
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);
}

"fstorck" wrote:
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, MyBasem_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

<MyGenericOfInt32or 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
Aug 21 '06 #3
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!

"Jared" wrote:
>
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);
}

"fstorck" wrote:
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, MyBasem_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

<MyGenericOfInt32or 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
Aug 21 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Ian Tompsett | last post by:
H I was wondering if it possible for an object to serialize/deserialize itself from XML. I'd be guessing that it would need to use the XmlSerializer class, but that seems to want to create a...
1
by: Marc Falesse | last post by:
Hi ! I have a problem with « Indigo », it can’t deserialize methods having a System.Collections.Specialized.NameValueCollection I’ve tried the solution of an approching issue in the « Known...
14
by: vince | last post by:
Can I add (append) to an xml file that already contains a serialized object, and be able to deserialize to either or both objects from the same file...??? How is this done...?? thanks, vince
5
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream...
5
by: Andrew Robinson | last post by:
I need to serialize a dictionary so I can encode the contents. I have the following working but the size seems large. I am guessing that I am serializing the entire object not just the data. Is...
0
by: Tim_Mac | last post by:
hi, i am able to serialize a normal generic List<> no problem. But when i try to serialize a Dictionary<string,string>, i get an exception as shown below: "The type...
2
by: =?Utf-8?B?U2hhd24=?= | last post by:
Hi; I would like to be able to use the XMLSerializer to serialize and deserialize a dictionary. is that possible? i know that you can serialize an object that implements the ICollection interface....
1
by: Tony Johansson | last post by:
Hello! Assume I have a class called Product which is defined with the attribute I create a collection by using the generic class List in this way List<Productproducts = new List<Product>(); ...
4
by: djidan | last post by:
hi, i am a newbe to c#, i'm trying to send en custom object from a client to a server, after reading a lot on the web ifound that i need to use: BinaryFormatter, MemoryStream, and Serialize...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.