Custom Serialization of base class properties - c#

I have a class that is implementing the ISerializable interface for custom serialization. This works great for the properties in this class but the class is a derived class. The problem i'm running into is that the base class properties aren't serialized for me. The base class has the serializable attribue but doesnt implement ISerializable. Is there a way to serialize the base class properties without having to add all of them manually in the derived class's ISerializable .GetObjectData method?

From MSDN
As I mentioned, the ISerializable interface is extremely powerful since it allows a type to take complete control over how instances of the type get serialized and deserialized. This power comes at a cost; the type is now responsible for serializing all of its base type's fields as well. Serializing the base type's fields is easy if the base type also implements the ISerializable interface—you just call the base type's GetObjectData method. Someday you may find yourself defining a type that needs to take control of its serialization, but whose base type does not implement the ISerializable interface. In this case, your class must manually serialize the base type's fields.

Related

Abstract Class Inheritance & Data Contracts

I have an abstract class BaseClass with two attributes I want to share with class ClassA : BaseClass and class ClassB : BaseClass. However, I want both ClassA and ClassB to be marked with DataContractAttribute in a way that the members inherited from BaseClass are also exposed in the contract. However, I don't want BaseClass itself to be exposed as a data contract. Is this possible in WCF (.NET 3.5)?
No, AFAIK that is not possible. Even if you new the properties to add data-member markers it'll still complain:
Type 'BaseClass' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.

Inherit from abstract class in WCF without exposing that class

I have various classes which I want to expose as Complex Types in WCF, so I add [DataContract] and [DataMember] attributes as necessary on those types and properties.
However if I want to have them inherit from an abstract base class (for example Person inherits from abstract EntityBase), I get an error that the type "cannot inherit from a type that is not marked with DataContractAttribute or SerializableAttribute".
The problem is, if I add [DataContract] attribute to the base class, then that base class is exposed to the client via the WSDL. Not a huge deal I guess, but I would prefer my client doesn't know about my internal implementation.
If I add the [Serializable] attribute to the base class, then it seemed to work at first (it could be serialized but EntityBase was not referenced in the WSDL), but now if I add any properties to EntityBase then it will also complain that its properties are not serializable. (For example I add an ICollection then I get an error that RuleViolation is not serializable).
Unfortunately there seems to be no analogue to [IgnoreDataMember] for a [Serializable] type ([NonSerialized applies only to fields, not properties).
So basically I want to declare this base type but don't need any of its members to be serialized; is there any way to set this up in WCF so the client doesn't see this base type?
Did you try not marking your entities with [DataContract] and [DataMember] at all (so that default serialization is used) and instead marking base class properties with [IgnoreDataMember]?
You always have several choices and I'm afraid you will not like any of them.
Create a set of DTO objects and convert entities to DTO. This is generally a best practice if you want to hide your inner implementation.
Create a surrogate class (implement IDataContractSuroggate) for each entity so that you have control over serialization - I'm not sure if this avoids the problem.
Upgrade to .NET 4.0 and use EF with POCO classes (with no EntityBase as parent)
Best regards, Ladislav
I think you have to use the KnownType attribute.
For instance see WCF issues with KnownType for Dictionary
[EDIT] A more complete discussion of this problem and its solution can be found here:
WCF: Interfaces, Generics and ServiceKnownType

serializable to derived classes

Is there an easy way to enforce a derived class must be serialiable?
Suppose I define a interface that needs the derived classes to be serializable. According to this post, I cannot just specify the serializable attribute in the interface, because derived classes don't need to respect that.
I believe I could have the interface inherit from the ISerializable interface, but does that mean that the derived class couldn't use the attribute to specify serialization (as opposed to actually implementing the methods for ISerializable)?
We cannot use [Serializable] as
gives error with an interface.
We may use [Serializable] attribute
with base class but even then this
attribute is not inherited. This does
not seem possible.
Have a look a this link as well.
Enforcing serializable from an
interface without forcing classes to
custom serialize in C#.

Enforce Object Property to be serializable

I am building a class and in the interface for my class(es) I have a property declared
object MyObject { get; set; }
What I want to do is force whatever is stored in MyObject to be serializable. Is there a good way to do this?
Normally, I'd use where : ISerializable, but for serialization you use an attribute, not inheritance, or at least that is my assumption.
I never could figure out how to enforce a class be Serializable. I had to move away from generic classes anyway for other reasons, so through inheritance I just create a property for my object to be serialized and serialize/deserialize it in the set/get accessors and store the serialized string in a string property in the base class, which is inherited.

If Base class is marked Serializable are all child classes marked too?

I have a whole list of entity classes which I need to make Serializable (due to storing session state in SQL, but that's another story).
I have added the attribute [Serializable] and all seems to be fine.
All of my entity classes extend from the same base class.
If I mark the base class as Serializable, does this mean all children are marked as Serializable too?
Thanks
No, attribute is not inherited.
When you extend the class, it's possible to add features that might not be serializable by nature therefore .NET framework cannot assume for you that everything what extends serializable base class is also serializable.
That's why you must explicitly state [Serializable] attribute on every class individually.
Nope, each one will have to be marked as [Serializable] specifically.
Also if you intend to serialize an object to XML which is of a derived type as though it is the base type you'll also need a [XmlInclude] attribute.
EG:
[Serializable]
public class BaseClass : ParentClass
{
}
[Serializable]
[XmlInclude(typeof(BaseClass))]
public class ParentClass
{
}
(Binary serialization, like what is used for sessions, do not need this)

Categories

Resources