2011-03-21 40 views

回答

5

對於Table Storage SDK的2.0版本,有一種新的方法來實現這一點。

您現在可以重寫TableEntity上的WriteEntity方法,並刪除任何具有其屬性的實體屬性。我從一類,這是否對我的所有實體派生,如:

public class CustomSerializationTableEntity : TableEntity 
{ 
    public CustomSerializationTableEntity() 
    { 
    } 

    public CustomSerializationTableEntity(string partitionKey, string rowKey) 
     : base(partitionKey, rowKey) 
    { 
    } 

    public override IDictionary<string, EntityProperty> WriteEntity(Microsoft.WindowsAzure.Storage.OperationContext operationContext) 
    { 
     var entityProperties = base.WriteEntity(operationContext); 

     var objectProperties = this.GetType().GetProperties(); 

     foreach (PropertyInfo property in objectProperties) 
     { 
      // see if the property has the attribute to not serialization, and if it does remove it from the entities to send to write 
      object[] notSerializedAttributes = property.GetCustomAttributes(typeof(NotSerializedAttribute), false); 
      if (notSerializedAttributes.Length > 0) 
      { 
       entityProperties.Remove(property.Name); 
      } 
     } 

     return entityProperties; 
    } 
} 

[AttributeUsage(AttributeTargets.Property)] 
public class NotSerializedAttribute : Attribute 
{ 
} 

然後你就可以使用這個類爲您的實體,如

public class MyEntity : CustomSerializationTableEntity 
{ 
    public MyEntity() 
    { 
    } 

    public string MySerializedProperty { get; set; } 

    [NotSerialized] 
    public List<string> MyNotSerializedProperty { get; set; } 
} 
相關問題