Beorne

I'm serializing a class using XmlSerializer. I'm forced to use
XmlSerializer because I'm working on compact framework and this is the
only serialization supported.
When I try to serialize a class with some delegate type member it
gives me an exception saying the member class should have a default
constructor ( ). but the member class is a delegate ...
Example:

[Serializable]
public static class DelegateContainer
{
public delegate void CustomDelegate(void);
}

[Serializable]
public class ClassToSerialize
{
DelegateContainer.CustomDelegate member = null;
}

When I serialize ClassToSerialize it gives me an exception saying
CustomDelegate should have a default constructor.

How could I do
Thank you.


Re: Visual C# General XmlSerializer and delegates

Priya Lakshminarayanan - MSFT

By default the XmlSerializer expects a parameterless constructor in order to serialiaze/deserialize a type. If you implement IXmlSerializable on the type, you can control the serialization yourself.

Thanks,

Priya





Re: Visual C# General XmlSerializer and delegates

Beorne

So I have to implement all the serialization reading and writing
I used serialization just to avoid explicitly implement reading and writing of a very big parameter file ... there is no other way
Thanks.




Re: Visual C# General XmlSerializer and delegates

Beorne

When I try to serialize a class with some delegate type member it
gives me an exception saying the (delegate) member class should have a default
constructor ( ).
Example:

[Serializable]
public static class DelegateContainer
{
public delegate void CustomDelegate(void);

public void voidFunction(void){}
}

[Serializable]
public class ClassToSerialize
{
DelegateContainer.CustomDelegate member =
voidFunction;
}

When I serialize ClassToSerialize it gives me an exception saying
CustomDelegate should have a default constructor.

How could I do to serialize a class with delegate members
Thank you.




Re: Visual C# General XmlSerializer and delegates

sirjis

Try adding a NonSerialized attribute before the declaration of member.

[NonSerialized]
DelegateContainer.CustomDelegate member =
voidFunction;





Re: Visual C# General XmlSerializer and delegates

houtexwebdev

make sure you have the field qualifier in there:

public class MyClass{

public string field1;

private int field2;

[field:NonSerialized()]

public NonSerializableObjectTypeLikeADelegateOrAnEvent field3;

}





Re: Visual C# General XmlSerializer and delegates

Martin Xie - MSFT

Merged the two threads with same topic.