【问题标题】:Rename a property in OData DataServiceContext重命名 OData DataServiceContext 中的属性
【发布时间】:2016-10-25 15:16:53
【问题描述】:

是否可以重命名将为 WCF OData System.Data.Services.Client 序列化的属性?

我有一个名为 Mailbox 的类和一个也称为 Mailbox 的属性。 这在 C# 中是不可能的,因为在这种情况下,名称 Mailbox 是为构造函数保留的。

使用 DataSvcUtil.exe 为 csdl 生成代码给出: error 0042: Name Mailbox cannot be used in type Mailbox. Member names cannot be the same as their enclosing type.

所以我想重命名该属性以进行序列化。我尝试了几个属性,但没有一个起作用。

[global::System.Data.Services.Common.DataServiceKeyAttribute("ID")]
public partial class Mailbox
{
   [DataMember(Name = "Mailbox")]
   [XmlElement(ElementName = "Mailbox")]
   [JsonProperty(PropertyName = "Mailbox")]       
   [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Services.Design", "1.0.0")]
   public string MailBox
   {
     get
     {
       return this._mailbox;
     }
     set
     {
       this.OnsMailboxChanging(value);
       this._mailbox = value;
       this.OnsMailboxChanged() 
     }
   }
   private string _mailbox;
}

【问题讨论】:

  • 你能贴出类本身的代码(不是自动生成的代码)吗?您应该能够使用[DataMember(Name = "Mailbox2")] 或类似的名称为序列化提供不同的名称。
  • 我没有原始课程,因为我不拥有网络服务。我确实有一个 CSDL:pastebin.com/6ascq9YY 然后我使用此命令创建给客户端。 "%windir%\Microsoft.NET\Framework\v4.0.30319\DataSvcUtil.exe" /language:CSharp /in:"%cd%/metadata.csdl" /out:"%cd%/metadata-auto-generated.cs " 创建后,由于编译错误,我必须重命名该属性。但我必须将其重命名为网络服务。

标签: wcf serialization odata


【解决方案1】:

我通过将代码移至 Visual Basic 找到了一种解决方法,因为在 VB 中允许具有同名的属性和类。我不喜欢这个解决方案,但至少我可以继续编码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-25
    • 2013-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-21
    相关资源
    最近更新 更多