简体   繁体   English

C#-通过TCP发送WinForm

[英]C# - Send a WinForm over TCP

I have created a client server application which is currently able to send messages as containers: 我创建了一个客户端服务器应用程序,该应用程序当前能够将消息作为容器发送:

[Serializable]
public class MsgContainer
{
    public string TableName { get; set; }
    public bool SomethingBool { get; set; }
    public DataTable DataTableData { get; set; }
}

The problem: Depending on the request from the user I would like the server to be able to send Forms 问题:根据用户的请求,我希望服务器能够发送表格

public Form requestedForm { get; set; }

The problem with that (as i have read in the web and tried in my application) WinForms are not serializable which is why i receive the following error: 问题(如我在网络上阅读并在应用程序中尝试过的)WinForms无法序列化,这就是为什么我收到以下错误的原因:

System.Runtime.Serialization.SerializationException: 'Type 'System.Windows.Forms.Form' in Assembly 'System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.' System.Runtime.Serialization.SerializationException:程序集'System.Windows.Forms,版本= 4.0.0.0,区域性=中性,PublicKeyToken = b77a5c561934e089'中的类型'System.Windows.Forms.Form'未标记为可序列化。

Is there any workaround to my problem? 我的问题有什么解决方法吗?

I would strongly advise you find a different approach to whatever you're trying to do. 我强烈建议您为尝试做的事情找到不同的方法。 The Form is not serializable. 该表格不可序列化。 You could: 你可以:

  • Make a serializable class to transfer all the form information and regenerate a form based on it. 创建一个可序列化的类,以传输所有表单信息并基于该信息重新生成表单。
  • Write your own serializer and deserializer for a form. 为表格编写自己的序列化器和反序列化器。

Either way you would need to overcome the many following issues, such as: 无论哪种方式,您都需要克服以下许多问题,例如:

  • Components in the form are also not serializable. 形式的组件也不能序列化。
  • Each control may have a value or a binding to a data source that also needs to be transferred. 每个控件可能具有一个值或对数据源的绑定,也需要将其传输。
  • You can include infinite different objects and classes in a form that would be part of your main project. 您可以以主项目一部分的形式包含无限的不同对象和类。 Everything would need to be in a library consumed by both server and client. 一切都需要放在服务器和客户端都使用的库中。

Basically, this would be your worst nightmare and after spending however much time you may spend working on it, you will eventually realise that you have nothing but bin filler. 基本上,这将是您最糟糕的噩梦,在花费了很多时间之后,您最终会意识到除了垃圾箱之外,您什么都没有。

You cannot (reasonably) serialise a form. 您不能(合理地)序列化表格。

A better approach, if viable, would be to build the forms into the client app. 如果可行,更好的方法是将表单构建到客户端应用程序中。 Then have the server instruct the client as to which form to open. 然后,让服务器指示客户端打开哪种表单。 Optionally use an enum for this. (可选)为此使用一个枚举。

public enum FormType
{
    Products,
    Customers
}

public FormType RequestedForm { get; set; }

Sending a Form is a pretty pointless excersise. 发送表格是一件毫无意义的工作。 Forms are just there to Display data. 表单就在那里显示数据。 If you want a certain form to be dispalyed, send the Data it needs rather then the whole Form. 如果要分发某个表格,请发送所需的数据,而不是整个表格。

Honestly it sounds like you either have some very faulty design. 老实说,听起来您的设计有些错误。 Or wanted to do a WebApplication the whole time. 或一直想做一个WebApplication。 Consider that you might be stuck in a X Y Problem . 考虑到您可能陷入了X Y问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM