Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

How can I add the
[DataContract] attribute
on TableEntity class which I don't have any influence on?

CSS
[DataContract]
public class MyClass : TableEntity
{
    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public string Email { get; set; }
}

// I need to serialize MyClass as a byte[] and save that into the table storage.

public class MyClassAsByteArray : TableEntity
{
   public byte[] SomeByteArray { get; set; }
}


Or what would be a work-around?

See implementation of TableEntity the following link: TableEntity.cs[^]
Posted
Updated 27-Aug-13 3:58am
v3
Comments
Jameel VM 27-Aug-13 9:52am    
What 's you pblm?Actually what you are trying to achieve?
Yustme 27-Aug-13 9:59am    
Serializing/saving the MyClass user defined CLR object as a byte[] into the Azure Storage Table. Which I need to be able to deserialize when I need it again.

I updated my question, both classes now derive from TableEntity. Overlooked it when I placed the code here.

1 solution

If the class is the base class, adding it to the contract is not enough, because how the serialization assembly can "know" about all the derived classes? You really need to add one more kind of attribute, System.Runtime.Serialization.KnownTypeAttribute, one per really used terminal derived class. Please see:
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.knowntypeattribute.aspx[^].

By the way, it's good to add [DataConract] attribute with your unique namespace parameter.

—SA
 
Share this answer
 
Comments
Yustme 27-Aug-13 12:39pm    
But I can't add the [DataConract] attribute at all to the TableEntity. It's not my class.
Let alone adding System.Runtime.Serialization.KnownTypeAttribute to it.

Or did I misunderstood you?
Sergey Alexandrovich Kryukov 28-Aug-13 0:00am    
I mean the attributes to be applied to MyClass.
—SA
Yustme 28-Aug-13 3:14am    
But that still gives me this error message:

Type 'Microsoft.WindowsAzure.Storage.Table.TableEntity' in Assembly 'Microsoft.WindowsAzure.Storage, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable.

I don't control Microsoft.WindowsAzure.Storage.Table.TableEntity. So how can I mark it as serializable?
Sergey Alexandrovich Kryukov 28-Aug-13 12:05pm    
Oh, this is [Serializeable], related to a different serialization technology, not using Data Contract. Obsolete, I would say, but what to do? Perhaps it forces you to switching back to old serialization. Just read about it:

Please see: http://msdn.microsoft.com/en-us/library/7ay27kt9.aspx.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900