Serialize controls

G

Guest

I want to serialize the properties of controls. I write Property Name and Value to an Hashtable. This hashtable is serialized. So I want to find out which properties of the control are serializable. I tried following but it doesn't work. I still get not serializable properties into my hashtable. Exception of System.Runtime.Serialization.SerializationException [...] is not marked as serializable

PropertyInfo[] pi
Type t
t=ctl.GetType()

pi=t.GetProperties (BindingFlags.Public | BindingFlags.Instance)
foreach(PropertyInfo p in pi

if(p.PropertyType.IsSerializable

AddtoHashtable(p.Name,p.GetValue(ctl,null))

Thanks for your hel
Michi
 
S

Sherif ElMetainy

Hello

The property type can be serializable but the value not serializable.
For example when the property type is object (like Tag property), it is
serializable, but it can hold a value of any type since all types inherit
from object. If the value of the Tag property has non serializable type, you
will get this exception.

So you can change the code to something like that.
foreach(PropertyInfo p in pi)
{
object val = p.GetValue(ctl, null);
if(val != null && val.GetType().IsSerializable)
{
AddtoHashtable(p.Name,val);


You can also check for p.CanWrite must be true, because in case you
deserialize, you can't write to readonly property, so (depending on your
application) it might be useless to serialize a ReadOnly property.


Best regards,
Sherif


MichiSu11 said:
I want to serialize the properties of controls. I write Property Name and
Value to an Hashtable. This hashtable is serialized. So I want to find out
which properties of the control are serializable. I tried following but it
doesn't work. I still get not serializable properties into my hashtable.
Exception of System.Runtime.Serialization.SerializationException [...] is
not marked as serializable.
PropertyInfo[] pi;
Type t;
t=ctl.GetType();

pi=t.GetProperties (BindingFlags.Public | BindingFlags.Instance);
foreach(PropertyInfo p in pi)
{
if(p.PropertyType.IsSerializable)
{
AddtoHashtable(p.Name,p.GetValue(ctl,null));


Thanks for your help
Michi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top