我怎么能形成[Serializable()]? [英] how i can , Form the [Serializable()] ?

查看:74
本文介绍了我怎么能形成[Serializable()]?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能形成[Serializable()]?

我想使用BinaryFormatter.

how i can , Form the [Serializable()] ??

i want use of BinaryFormatter.

推荐答案

我了解,您想存储表单状态(大小,所选值,焦点位置,输入数据等)能够稍后还原所有内容.

您可以解决此问题,但不要尝试使用表单本身-这是个坏主意.这将UI和数据方面混为一谈.不要这样做,您会陷入困境!

最好创建单独的纯数据类,仅对该类进行序列化,存储/加载该类的实例,并在形式上/从形式上进行语义映射.无论如何,坚持表单所需要的将永远不会是通用的,所有必不可少的数据都将是语义性的.

—SA
I understand, you want to store form status (sizes, selected values, position of focus, entered data, etc.) to be able to restore all that later, right.

You can solve this problem, but you should not try it with the form itself — bad idea. That is messing up UI and data aspects together. Don''t do it, you will sunk in problems!

Better create separate pure-data class, make serialization for this class only, store/load instances of this class and semantically map in from/to your form. What you need to persist on the form will never be universal anyway, all essential data will be semantic.

—SA


我想做这样的事情:
You would like to do something like this I suppose:
[Serializable]
public class Element
{
 public int field1;
 public string field2;

 public Element()
 {
 }

 public Element(int f1, string f2)
 {
   field1 = f1;
   field2 = f2;
 } 
}

static void Serialize() 
{

  FileStream fs = new FileStream("DataFile.dat", FileMode.Create);
  List<element> elements = new List<element>();
  
  elements.Add(new Element(1,"One"));
  elements.Add(new Element(2,"Two"));

  // Construct a BinaryFormatter and use it to 
  // serialize the data to the stream.
  BinaryFormatter formatter = new BinaryFormatter();
  try 
  {
    formatter.Serialize(fs, elements);
  }
  catch (SerializationException e) 
  {
    Console.WriteLine("Failed to serialize. Reason: " + e.Message);
    throw;
  }
  finally 
  {
    fs.Close();
  }
}



显然,您将必须创建比我的Element类更好的东西,但是如果您创建类似的类以反映表单的属性,则可以通过BindingSource绑定到元素.我认为这基本上就是SAKryukov建议的内容:)

问候
Espen Harlinn



Obviously you will have to create something better than my Element class, but if your create similar classes reflecting the properties of the form you can then bind to the elements through a BindingSource. I think this is basically what SAKryukov suggested :)

Regards
Espen Harlinn


不清楚您的问题.据我所知 [ ^ ]链接可能会帮助您解决问题.
Not clear with your question. As of my knowledge this[^]link might help you to solve your problem.


这篇关于我怎么能形成[Serializable()]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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