By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,386 Members | 1,676 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,386 IT Pros & Developers. It's quick & easy.

XML Serialize / Deserialize dictionary holding various generic typ

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a

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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.