473,781 Members | 2,729 Online
Bytes | Software Development & Data Engineering Community
+ 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 : IXmlSerializabl e
{
// whatever
}

class MyGeneric<Value Type: MyBase, IXmlSerializabl e
{
MyGeneric(Value type tVal)
{
val = tVal;
}
ValueType val;
}

class Program
{
dictionary<stri ng, MyBasem_Dic = new dictionary<stri ng,MyBase>();

void FillDictionary( )
{
dictionary.Add( "Key1", new MyGeneric<int>( 10));
dictionary.Add( "Key1", new MyGeneric<strin g>("StringValue "));
dictionary.Add( "Key1", new MyGeneric<doubl e>(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

<MyGenericOfInt 32or 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 11775

You should be able to generate an instance using reflection. Something
similar to this.

if (typeof(List<st ring>).IsGeneri cType)
{
Type t = typeof(List<str ing>).GetGeneri cTypeDefinition ();
object obj = Activator.Creat eInstance(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 : IXmlSerializabl e
{
// whatever
}

class MyGeneric<Value Type: MyBase, IXmlSerializabl e
{
MyGeneric(Value type tVal)
{
val = tVal;
}
ValueType val;
}

class Program
{
dictionary<stri ng, MyBasem_Dic = new dictionary<stri ng,MyBase>();

void FillDictionary( )
{
dictionary.Add( "Key1", new MyGeneric<int>( 10));
dictionary.Add( "Key1", new MyGeneric<strin g>("StringValue "));
dictionary.Add( "Key1", new MyGeneric<doubl e>(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

<MyGenericOfInt 32or 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<st ring>).IsGeneri cType)
{
Type t = typeof(List<str ing>).GetGeneri cTypeDefinition ();
Does the typeof accepts a string like "MyApp.MyGeneri c`1[System.String]" or
<MyGenericOfInt 32 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<st ring>).IsGeneri cType)
{
Type t = typeof(List<str ing>).GetGeneri cTypeDefinition ();
object obj = Activator.Creat eInstance(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 : IXmlSerializabl e
{
// whatever
}

class MyGeneric<Value Type: MyBase, IXmlSerializabl e
{
MyGeneric(Value type tVal)
{
val = tVal;
}
ValueType val;
}

class Program
{
dictionary<stri ng, MyBasem_Dic = new dictionary<stri ng,MyBase>();

void FillDictionary( )
{
dictionary.Add( "Key1", new MyGeneric<int>( 10));
dictionary.Add( "Key1", new MyGeneric<strin g>("StringValue "));
dictionary.Add( "Key1", new MyGeneric<doubl e>(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

<MyGenericOfInt 32or 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(st r_valtype);

object o = Activator.Creat eInstance(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<st ring>).IsGeneri cType)
{
Type t = typeof(List<str ing>).GetGeneri cTypeDefinition ();
object obj = Activator.Creat eInstance(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 : IXmlSerializabl e
{
// whatever
}

class MyGeneric<Value Type: MyBase, IXmlSerializabl e
{
MyGeneric(Value type tVal)
{
val = tVal;
}
ValueType val;
}

class Program
{
dictionary<stri ng, MyBasem_Dic = new dictionary<stri ng,MyBase>();

void FillDictionary( )
{
dictionary.Add( "Key1", new MyGeneric<int>( 10));
dictionary.Add( "Key1", new MyGeneric<strin g>("StringValue "));
dictionary.Add( "Key1", new MyGeneric<doubl e>(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

<MyGenericOfInt 32or 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
5831
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 brand new object when deserializing. In my case I have an existing object that I'd like to pass some XML to for the object to repopulate its member variables. Similarly I'd like it to be able to populate an XML string from the values of its member...
1
824
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 Issues » : C:\WINDOWS\Microsoft.NET\Framework\v2.0.50215\Microsoft Avalon and Indigo Beta 1 RC\WinFxKnownIssues.rtf , But it didn’t work even with the advised XML config on both sides: client and server.
14
14314
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
24723
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 s); public void Deserialize(Stream s); Within the MyUserControl class, there is a field of type MyInnerClass
5
47280
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 there a better way? MemoryStream stream = new MemoryStream(); BinaryFormatter formatter = new BinaryFormatter(); Dictionary<string, string> dictionary = new Dictionary<string, string>(); dictionary.Add("name", "andrew"); dictionary.Add("home",...
0
4747
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 System.Collections.Generic.Dictionary is not supported because it implements IDictionary" why is it not straight forward to serialize a dictionary, like it is with a generic List?
2
5745
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. does the fact that Dictionary inherits from ICollection<cause a problem? Currently i am using a class that inherits from ICollection and it has a Dictionary as it's container. but when it gets to creating the serialized object it blows up. here...
1
1770
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>(); product.Add(new Product(1, "some name 1",120)); product.Add(new Product(1, "some name 2",130)); product.Add(new Product(1, "some name 3",140));
4
2200
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 Function, and Deserialize Function, but the thing is that when trying to Serialize and then Deserialize on the same project it's working perfectly, but when i send the message to the server and trying to Deserialize i get en exception "Unable to find...
0
9639
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9474
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10143
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10076
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7486
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6729
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5375
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2870
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.