C# 覆盖 ToString() 并在 textBox 中显示 [英] C# Override ToString() and display in textBox

查看:68
本文介绍了C# 覆盖 ToString() 并在 textBox 中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这门课:

namespace JimWcfFormTest3
{
[DataContract]

public class GateInfo
{
    [DataMember]
    public int carid { get; set; }

    [DataMember]
    public int paid_at_gate { get; set; }

    [DataMember]
    public int wash_pkg_purch { get; set; }

    [DataMember]
    public string carte { get; set; }

    public override string ToString()
    {
        return "Car ID:  " + carid + "Paid at Gate:  " + paid_at_gate + "Wash Package:  " + wash_pkg_purch + "Ala Carte:  " + carte;
    }
}

}

由这个 WCF 服务调用:

That is called by this WCF Service:

namespace JimWcfFormTest3
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class Service1 : IService1
{
    private List<GateInfo> _gate;

    private Service1()
    {
        _gate = new List<GateInfo>();
    }

    public void Gate_to_Server(GateInfo gatein)
    {
        if (gatein != null) _gate.Add(gatein);
    }

    public List<GateInfo> Server_to_Term()
    {
        return _gate;
    }
}

}

此表单上的此按钮调用的内容:

That is called by this Button on this Form:

    private Service1Client server = new Service1Client();
    private void button1_Click(object sender, EventArgs e)
    {
        int carnum = 2;
        int pay = 1;
        int wash = 5;
        string txt = "TEST";
        var data_out = new GateInfo { carid = carnum, paid_at_gate = pay, wash_pkg_purch = wash, carte = txt };

        server.Gate_to_Server(data_out);

        dataGridView1.DataSource = server.Server_to_Term();

我的 ToString 覆盖是否在正确的位置?如何在表单中正确调用 ToString 覆盖,以便在单击按钮时将其放入文本框中?

Is my ToString override in the right place? How do I properly call the ToString override in the Form, so I can put it in a textBox when the button is clicked?

推荐答案

由于您通过 Web 服务调用此服务,因此 GateInfo 类型正在被序列化回客户端应用程序(您的表单应用程序).如果客户端应用程序没有本机 GateInfo 类型,那么您将使用不包含函数的序列化类型.

Since you are calling this thru a Web Service, the type GateInfo is getting serialized back to the client application (your Forms app). If the client application does not have the native GateInfo type, then you will be using the serialized type, which does not include functions.

要让 ToString 覆盖在客户端工作,您需要将该类包含在您的表单应用程序中.我通常通过将共享数据类型/模型对象放在一个单独的类库中来实现这种类型共享",并让服务器和客户端都使用这个库来映射对象类型.

To have the ToString override work on the client side, you need that class to be included in your Forms application. I typically do this "type sharing" by putting shared data types / model objects in a separate class library and have both the server and client use this lib to map object types to.

如果您使用这种方法,请确保勾选 WCF 服务属性下的选项以在引用的程序集中重用类型.这将使 WCF 客户端生成器知道正确映射该类型.

If you use this approach, make sure you tick the option under the WCF Service Properties to Reuse types in referenced assemblies. This will let the WCF client generator know to map that type correctly.

这篇关于C# 覆盖 ToString() 并在 textBox 中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆