如果我试图序列化一个普通的 CLR 对象,并且我不想序列化一个特定的成员变量,我可以用
[NonSerialized]
属性。如果我正在创建一个表服务实体,是否有一个等效的属性可以用来告诉 Azure 表服务忽略此属性?
如果我试图序列化一个普通的 CLR 对象,并且我不想序列化一个特定的成员变量,我可以用
[NonSerialized]
属性。如果我正在创建一个表服务实体,是否有一个等效的属性可以用来告诉 Azure 表服务忽略此属性?
对于 2.1 版,有一个新的 Microsoft.WindowsAzure.Storage.Table.IgnoreProperty 属性。有关详细信息,请参阅 2.1 发行说明:http: //blogs.msdn.com/b/windowsazurestorage/archive/2013/09/07/announcing-storage-client-library-2-1-rtm.aspx。
我所知道的没有等价物。
这篇文章说明了如何达到预期的效果 - http://blogs.msdn.com/b/phaniraj/archive/2008/12/11/customizing-serialization-of-entities-in-the-ado-net-data -services-client-library.aspx
或者,如果您可以在您的财产上使用“内部”而不是“公共”,那么它不会与当前的 SDK 保持一致(但这可能会在未来发生变化)。
对于 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; }
}