WCF DataContract ToString function

感情迁移 提交于 2019-12-29 08:57:07

问题


Can you override the ToString function in a 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 it doesn't seem to work. Anyway to get this working?


回答1:


Remember too that if you own both the server and the client, that often you can use a shared library for data contracts rather than generating a client proxy. If you do that, then you can have the same method on both the server and client as they're exactly the same type.




回答2:


I realize this is old but wanted to provide an answer since I just created a sample app for a coworker that used this idea. All of this work can be done on the consumer/test client side.

If you look at the code on the consumer/test client and, more specifically, the classes that are generated as part of the service reference, you will see the [DataContract] type you are interested in. In order to do this you should make sure that 'Show All Files' is selected. Drill down to the 'Reference.cs' class. This is the top of my test class from 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 {

The important bits that you will need to use are the namespace and the partial class. To make use of these you simply have to create a new class in your test client of the same type, in the same namespace, and override the ToString() method. Here is an example of how to do that from the newly created UCCRecord.cs file on the consumer/test client.

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

Note that I can only reference Key and Timestamp and Value because they are [DataMember] values for the [DataContract].

This is relatively simple if you know what you are looking for. If anything here is not clear, please let me know and I will attempt to clarify.

Thanks




回答3:


Where do you want to be able to invoke ToString()? Methods are not part of the DataContract so they won't be available when you create the proxy for the client.

Of course, nothing is stopping you from coding that method in the client's proxy yourself.



来源:https://stackoverflow.com/questions/1181039/wcf-datacontract-tostring-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!