2

当我尝试序列化派生自的实体时收到此错误消息TableEntity

类型“MyClass”不能继承自未用 DataContractAttribute 或 SerializableAttribute 标记的类型。考虑使用 DataContractAttribute 或 SerializableAttribute 标记基类型“Microsoft.WindowsAzure.Storage.Table.TableEntity”,或从派生类型中删除它们。

错误消息非常清楚地说明出了什么问题。

所以我的问题是,我怎样才能DataContractAttribute在课堂上不被装饰TableEntity

代码:

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

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

我需要序列MyClass化为 abyte[]并将其保存到表存储中。

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

如果有人对如何将其序列化为 a 有任何建议byte[],请告诉我。

[编辑]

我决定创建自己的可序列化TableEntity

[DataContract]
public class MyOwnTableEntity : ITableEntity
{
    private TableEntity te = new TableEntity();

    public MyOwnTableEntity ()
    {
    }

    public MyOwnTableEntity (string partitionKey, string rowKey)
    {
        this.PartitionKey = partitionKey;
        this.RowKey = rowKey;
    }

    public string PartitionKey { get; set; }

    public string RowKey { get; set; }

    public DateTimeOffset Timestamp { get; set; }

    public string ETag { get; set; }

    public virtual void ReadEntity(IDictionary<string, EntityProperty> properties, OperationContext operationContext)
    {
        te.ReadEntity(properties, operationContext);
    }

    public virtual IDictionary<string, EntityProperty> WriteEntity(OperationContext operationContext)
    {
        return te.WriteEntity(operationContext);
    }
}

然后我从这个类派生,但它无法将属性写入MyClass存储MyClassAsByteArray表。那是因为我创建了一个TableEntity.

如何将传递给和传入的参数转发给实际ReadEntity类中WriteEntity的方法?MyOwnTableEntityReadEntityWriteEntityTableEntity

微软已经编写了代码,所以我想防止在这里重新发明轮子。

编辑TableEntity.cs

4

2 回答 2

1

回到 MSSQL 并放弃了 Azure 表存储作为解决方案。只是因为它有太多的限制。

于 2013-09-07T08:08:55.907 回答
0

一种方法是覆盖它:

public new byte[] SomeByteArray { get; set; }

然后你可以用IgnoreDataMember属性忽略它。

于 2013-08-26T14:28:11.060 回答