Azure 表服务实体是否具有相当于 NonSerializedAttribute 的属性?

发布于 2024-10-25 15:10:20 字数 148 浏览 1 评论 0原文

如果我尝试序列化普通的 CLR 对象,并且不希望序列化特定的成员变量,则可以使用该

[NonSerialized]

属性对其进行标记。如果我要创建表服务实体,是否可以使用等效属性告诉 Azure 表服务忽略此属性?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丢了幸福的猪 2024-11-01 15:10:20

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

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.

宫墨修音 2024-11-01 15:10:20

据我所知,没有类似的东西。

这篇文章介绍了如何实现所需的效果 - http://blogs.msdn.com/b/phaniraj/archive/2008/12/11/customizing-serialization-of-entities-in-the-ado -net-data-services-client-library.aspx

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

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).

原来分手还会想你 2024-11-01 15:10:20

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

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; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文