Do Azure table services entities have an equivalent of NonSerializedAttribute?

吃可爱长大的小学妹 提交于 2019-11-28 02:59:23

问题


If I'm trying to serialize a normal CLR object, and I do not want a particular member variable to be serialized, I can tag it with the

[NonSerialized]

attribute. If I am creating a table services entity, is there an equivalent attribute I can use to tell Azure table services to ignore this property?


回答1:


For Version 2.1 there is a new Microsoft.WindowsAzure.Storage.Table.IgnoreProperty attribute. See the 2.1 release notes for more information: http://blogs.msdn.com/b/windowsazurestorage/archive/2013/09/07/announcing-storage-client-library-2-1-rtm.aspx.




回答2:


There's no equivalent I know of.

This post says how you can achieve the desired effect - http://blogs.msdn.com/b/phaniraj/archive/2008/12/11/customizing-serialization-of-entities-in-the-ado-net-data-services-client-library.aspx

Alternatively, if you can get away with using "internal" rather than "public" on your property then it will not get persisted with the current SDK (but this might change in the future).




回答3:


For version 2.0 of the Table Storage SDK there is a new way to achieve this.

You can now override the WriteEntity method on TableEntity and remove any entity properties that have an attribute on them. I derive from a class that does this for all my entities, like:

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
{
}

Then you can make use of this class for your entities like

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

     public string MySerializedProperty { get; set; }

     [NotSerialized]
     public List<string> MyNotSerializedProperty { get; set; }
}


来源:https://stackoverflow.com/questions/5379393/do-azure-table-services-entities-have-an-equivalent-of-nonserializedattribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!