当前位置:网站首页>WCF: expose unset read-only DataMember property- WCF: Exposing readonly DataMember properties without set?

WCF: expose unset read-only DataMember property- WCF: Exposing readonly DataMember properties without set?

2022-07-05 01:29:00 Zafo

problem :

I have a server side class which I make available on the client side through a [DataContract]. I have a server-side class , Can pass [DataContract] Use on client .This class has a readonly field which I'd like to make available through a property. This class has a read-only field , I want to provide this field through an attribute .However, I'm unable to do so because it doesn't seem that I'm allowed to add a [DataMember] property without having both get and set. however , I can't do this , Because it seems that I am not allowed to get and set Add if [DataMember] attribute .

So - is there a way to have a [DataMember] property without setter? therefore - Is there any way? No setter Of [DataMember] Attribute ?

[DataContract]class SomeClass{    private readonly int _id;     public SomeClass() { .. }    [DataMember]    public int Id { get { return _id; } }            [DataMember]    public string SomeString { get; set; }}

Or will the solution be use the [DataMember] as the field - (like eg shown here )? Or use it in the solution [ Data member ] As field - ( For example, image display ad locum )?Tried doing this too, but it doesn't seem to care the field is readonly..? I have also tried to do this , But it doesn't seem to care that the field is read-only .

Edit : Is the only way to make a readonly property by hacking it like this? edit : Is this the only way to make it a read-only property ?(no - I don't want to do this...)( no - I don't want to do that ...)

[DataMember]public int Id{    get { return _id; }    private set { /* NOOP */ }}

Solution :

Reference resources : https://stackoom.com/en/question/7rRd
原网站

版权声明
本文为[Zafo]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202141028370144.html

随机推荐