By Gizazas


2018-02-13 13:35:52 8 Comments

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:

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.'

Is there any workaround to my problem?

3 comments

@Steve Harris 2018-02-13 13:47:50

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.

@Christopher 2018-02-13 13:47:11

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. Consider that you might be stuck in a XY Problem.

@Stephen Kennedy 2018-02-13 13:46:11

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; }

Related Questions

Sponsored Content

21 Answered Questions

[SOLVED] Cast int to enum in C#

  • 2008-08-27 03:58:21
  • lomaxx
  • 1073498 View
  • 2672 Score
  • 21 Answer
  • Tags:   c# enums casting

9 Answered Questions

[SOLVED] What are the correct version numbers for C#?

56 Answered Questions

[SOLVED] What is the difference between String and string in C#?

38 Answered Questions

63 Answered Questions

[SOLVED] How do I calculate someone's age in C#?

  • 2008-07-31 23:40:59
  • Jeff Atwood
  • 497544 View
  • 1743 Score
  • 63 Answer
  • Tags:   c# .net datetime

41 Answered Questions

2 Answered Questions

[SOLVED] How to mark a List or IEnumerable serializable?

  • 2017-02-07 19:56:47
  • Blaise
  • 992 View
  • 1 Score
  • 2 Answer
  • Tags:   c# asp.net-mvc

0 Answered Questions

Serialize a class that implements IEnumerable

0 Answered Questions

ListBox.ObjectCollection not marked as serializable

4 Answered Questions

[SOLVED] marking a WinForms Button as serializable

Sponsored Content