【发布时间】:2011-04-18 20:17:02
【问题描述】:
我正在尝试创建一个具有只读 Id 字段的类,但是当对象通过 WCF 服务器时,我无法保留该值。
我无法在公共属性上设置[DataMember] 属性,因为没有set 方法,我希望尽可能保持这种状态,因为我不希望通过外部方式更改此值。我无法在私有字段上设置 [DataMember] 属性,因为它在部分信任环境中引发错误。
public class MyClass
{
private int _id;
public int Id
{
get { return _id; }
}
private string _otherProperties;
[DataMember]
public string OtherProperties
{
get { return _otherProperties; }
set { _otherProperties = value; }
}
}
有没有办法在通过 WCF 服务器时保持 Id 字段的值而不公开我的属性?
【问题讨论】:
-
msdn.microsoft.com/en-us/library/ms733127.aspx :如果你有私有方法,它不应该抛出错误。您可以在注释中看到:DataContract 不关心方法的可见性。
标签: wcf datacontract datamember