【问题标题】:Do Azure table services entities have an equivalent of NonSerializedAttribute?Azure 表服务实体是否具有等效的 NonSerializedAttribute?
【发布时间】:2011-07-19 18:15:12
【问题描述】:

如果我正在尝试序列化一个普通的 CLR 对象,并且我不希望序列化特定的成员变量,我可以用标记它

[NonSerialized]

属性。如果我正在创建表服务实体,是否可以使用等效属性来告诉 Azure 表服务忽略此属性?

【问题讨论】:

    标签: azure azure-table-storage


    【解决方案1】:

    对于 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

    【讨论】:

      【解决方案2】:

      据我所知,没有同类产品。

      这篇文章说明了如何达到预期的效果 - http://blogs.msdn.com/b/phaniraj/archive/2008/12/11/customizing-serialization-of-entities-in-the-ado-net-data-services-client-library.aspx

      或者,如果您可以在您的财产上使用“内部”而不是“公共”,那么它不会与当前的 SDK 保持一致(但这可能会在未来发生变化)。

      【讨论】:

        【解决方案3】:

        对于 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; }
        }
        

        【讨论】:

          猜你喜欢
          • 2018-10-25
          • 2019-10-12
          • 1970-01-01
          • 1970-01-01
          • 2011-08-15
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多