C# 覆盖 ToString() 并在 textBox 中显示

编程入门 行业动态 更新时间:2024-10-28 22:26:46
本文介绍了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 中显示

本文发布于:2023-11-05 14:49:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561136.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:并在   ToString   textBox

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!