Defining the fields to be exposed (serialized) not via DataMemberAttribute - c#

I have an autogenerated class, which I want to partially reuse as DataContract. Since the class is periodically auto-regenerated, I wouldn't like to add DataMemberAttribute to its properties, because it will be lost. What are the alternatives? Can I define which properties are going to be serialized programmatically or, may be, via a partial class?
Thanks!

You can add XmlAttributes at runtime with the XmlAttributeOverrides class but I'm not sure if you can do the same with DataMemberAttribute, have not come across an equivalent of XmlAttributeOverrides..

Related

Xml Serialisation

Does anyone know if there is an alternative to using attributes on C# properties to map to XML nodes when using XmlSerializer?
My issue is that I have an object called Article, with some properties (e.g. ID, Title, Body) and I do not want to add attributes directly to it (used elsewhere, etc, etc)...so I created a partial class and re-defined the properties and added the attributes there, but soon discovered that you cannot have duplicate properties in partial classes.
So I was wondering if anyone knew of any way that I could map the properties instead (in a similar fashion to n-hibernate, for example).
I'd appreciate any help.
There's a constructor of the XmlSerializer class that allows you to pass a XmlAttributeOverrides and thus alter the behavior at runtime.
Quote from the documentation:
The overrides parameter can be used to control how fields and
properties are encoded in XML. These settings override any attributes
that already exist on the objects. This can be useful when the source
code cannot be modified or multiple encodings are required for the
same classes.
You could implement IXmlSerializable directly. It requires some more code, but you will have full control without the need for attributes.

Denote properties of class to not be serialized

I'm using silverlight on WP7.
I have a class I am trying to serialize to isolatedstorage. I know the framework will take care of serializing the class automatically for me, assuming every property is serializable.
For this class, I have some properties (such as BitmapImage) that are not serializable. Is there a way to tell the framework to NOT serialize that property (and instead, set it as null when it deserializes it?)
I know implementing XmlSerializable is a possibility, but I don't want to have to set/get each manually. I'd rather it serialize what I tell it to, and I can go and set the other properties after it has deserialized.
thanks
You can use XmlIgnoreAttribute to tell XmlSerializer to ignore your property.

How to store User defined data type through DataContext?

I used DataContractSerializer to save user data but now
I want to use DataContext for my database design.
But the system existed struture as below cannot be stored through DataContext.
class Data
{
public DataType1;
public DataType2;
}
It seems these APIs cannot support storing user defined data type.
I don't want to separate all data members because this system uses these structure every where. If I changed the structure, it is hard to maintain and the DataType1 contains a List<> member. I don't know how to do even though separating
this structure.
Could you please kindly to give me some suggestions?
Thanks.
It seems these APIs cannot support storing user defined data type
If that were true the api would be completely without purpose.
The DataContractSerializer has what appear to be conflicting rules in an attempt to make serialisation more implicit. You can for example serialise a public type that has public properties and a public default constructor without having to decorate it with a DataContract attribute or any of its members with a DataMember attribute.
Looks to me that at the very least you need make Data a public class. Most likely you would need to review your other classes to ensure that either they are implicitly serializable or that you explictly mark them up with the DataContract and DataMember attributes.
Dear all:
But now I used DataContext
DataContext cannot serialize the user defined type even marked [Serializable].
The workaround seems to separate all the members into one class now.....
Thanks.

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

Add DataMemberAttribute "on the fly"

I have some types which are generated by a web service reference. I want to serialize these objects using the DataContractJsonSerializer, so I need to add DataContract and DataMember attributes. Adding DataContract is no problem using partial classes. But the properties have no DataMember attributes, so I only get empty objects. Is there a way to get this to work in case one cannot modify the serialized types?
Unlike XmlSerializer, I don't think (from memory) that there is a ctor for passing in this extra metadata at runtime. Perhaps another viable option is to have a twin DTO class that is attributed, and shuffle the data into there? You can add a conversion method / operator (between the two) in the partial class. Not ideal maybe, but it'll work.

Categories

Resources