gpt4 book ai didi

c# - 实现接口(interface)并使用接口(interface)现有实现中的代码

转载 作者:太空狗 更新时间:2023-10-30 01:20:38 24 4
gpt4 key购买 nike

我正在尝试实现 ITableEntity 接口(interface),以便我可以在其上添加 [DataContract] 属性。但如果我自己实现此接口(interface),我必须为 ReadEntityWriteEntity 方法提供主体。

但是有一个类已经实现了 ITableEntity 接口(interface),并为 ReadEntityWriteEntity 方法提供了主体,即 TableEntity.cs .

如何使接口(interface)的实现使用 TableEntity 类中的方法?

[编辑]

[DataContract]
public class SerializableTableEntity : ITableEntity
{
private TableEntity tableEntity;

public string ETag { get; set; }
public string PartitionKey { get; set; }
public string RowKey { get; set; }
public DateTimeOffset Timestamp { get; set; }

public SerializableTableEntity()
{
tableEntity = new TableEntity();
}

public void ReadEntity(IDictionary<string, EntityProperty> properties, Microsoft.WindowsAzure.Storage.OperationContext operationContext)
{
tableEntity.ReadEntity(properties, operationContext);
}

public IDictionary<string, EntityProperty> WriteEntity(Microsoft.WindowsAzure.Storage.OperationContext operationContext)
{
return tableEntity.WriteEntity(operationContext);
}
}

最佳答案

存储表中的每个属性都是空白的,因为 WriteEntity 和 ReadEntity 使用空白对象来存储和写入数据。

您将对象的序列化委托(delegate)给“tableEntity”,但其中没有任何属性。

建议:您需要在派生自 TableEntity 的类中实现 SerializedTableEntity 的所有属性,在 SerializedTableEntity 实体中包含该类型的变量,并将每个成员的属性从 SerializedTableEntity 获取/设置委托(delegate)给这个新对象。

这有意义吗?

编辑:按要求提供代码示例(尽管您不会喜欢它)

    [DataContract]
public class SerializableTableEntity : ITableEntity
{
private CustomEntity tableEntity;

public string ETag {
{
get
{
return tableEntity.ETag;
}
set
{
tableEntity.Etag = value;
}
}

public string PartitionKey
{
get
{
return tableEntity.PartitionKey;
}
set
{
tableEntity.PartitionKey = value;
}
}

public string RowKey
{
get
{
return tableEntity.RowKey;
}
set
{
tableEntity.RowKey = value;
}
}

public DateTimeOffset Timestamp
{
get
{
return tableEntity.Timestamp;
}
set
{
tableEntity.Timestamp = value;
}
}

public string PropertyOne
{
get
{
return tableEntity.PropertyOne;
}
set
{
tableEntity.PropertyOne = value;
}
}


public SerializableTableEntity()
{
tableEntity = new CustomEntity();
}

public void ReadEntity(IDictionary<string, EntityProperty> properties, Microsoft.WindowsAzure.Storage.OperationContext operationContext)
{
tableEntity.ReadEntity(properties, operationContext);
}

public IDictionary<string, EntityProperty> WriteEntity(Microsoft.WindowsAzure.Storage.OperationContext operationContext)
{
return tableEntity.WriteEntity(operationContext);
}
}

public class CustomEntity : TableEntity
{
public string PropertyOne { get; set; }
}

关于c# - 实现接口(interface)并使用接口(interface)现有实现中的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18487931/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com