WCF DataContract ToString Function

Can you override the ToString function in WCF DataContrat? Right now I have:

[DataContract]
public class Keyword
{
    public int ID { get; set; }
    public string Name { get; set; }

    public override string ToString()
    {
        return Name;
    }
}

But that does not work. Anyway, for this to work?

+3
source share
3 answers

Remember also that if you have both a server and a client, you can often use a common library for data contracts, rather than generate a client proxy. If you do this, you can have the same method on both the server and the client, as they are exactly the same.

0
source

, , , , . / .

/ , , , , [DataContract]. , " ". "Reference.cs". Reference.cs:

namespace WebApplication1.UCCTestSvcRef {
    using System.Runtime.Serialization;
    using System;


    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
    [System.Runtime.Serialization.DataContractAttribute(Name="UCCRecord", Namespace="http://schemas.datacontract.org/2004/07/UCCTest")]
    [System.SerializableAttribute()]
    public partial class UCCRecord : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged {

, , namespace partial class. , , ToString(). , UCCRecord.cs / .

namespace WebApplication1.UCCTestSvcRef
{
    public partial class UCCRecord
    {
        public override string ToString()
        {
            return "Key: " + Key.ToString() + ", Time: " + Timestamp.ToString("d") + ", Value: " + Value;
        }
    }
}

, Key Timestamp Value, [DataMember] [DataContract].

, , . - , , .

+2

ToString()? DataContract, .

, - .

+1

Source: https://habr.com/ru/post/1713520/


All Articles