序列化匿名委托列表 [英] Serializing a list of anonymous delegates

查看:136
本文介绍了序列化匿名委托列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题可能是非常相似,我的,但我看不到回答我需要它。我有一个类,名为 CASM ,具有列表与LT;作用> 。我想这个序列化类(使用类似的BinaryFormatter 或东西)。这个类,并在动作取值引用的所有类都得到了正确的 [Serializable接口] [ 。非序列化] 属性

This question may be very similar to my one, but I cannot see the answer I need in it. I have a class, called CASM, that has a List<Action>. I want to serialize this class (using the BinaryFormatter or something similar). This class and all classes referenced in the Actions have got correct [Serializable] and [NonSerializable] attributes.

在系列化尝试,问题就来 - 它使这个错误:

The problem comes when serialization is attempted - it gives this error:

Type 'CASM.CASM+<>c__DisplayClass2c' in Assembly 'CASM, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null' is not marked as serializable.

<> c__DisplayClass2c 是一个自动生成的内部类,用于保存不同类型的匿名委托的,我用我的应用程序。然而,正如我们可以从下面的图片看,这不是 [Serializable接口]

This <>c__DisplayClass2c is an autogenerated internal class that holds the different types of anonymous delegate I am using in my application. However, as we can see from the below image, it is not [Serializable]:

什么是改变我的应用程序的最佳方式所以这不工作?让我自己<> c__DisplayClass2c 型类,并使其可序列化?还是有更好的办法

What would be the best way to change my application so this does work? Make my own <>c__DisplayClass2c-type class and make it serializable? Or is there a better way?


编辑:在最后,我只是做我自己的类,而不是自动生成的。我调试帮助为好,实际上有一个描述性的名称,而不是仅仅 b__12()

In the end I just made my own class, instead of the autogenerated one. I helps with debugging as well, actually having a descriptive name rather than just b__12().

推荐答案

这通常使很少意义的序列化的委托。通常情况下,你会选择标记代表领域 [非序列化] ,并在需要时重新创建它。如果你的主要意图的的存储的代表,那么我会推荐一个完全不同的方法的思想,坦率地说。

It usually makes very little sense to serialize a delegate. Normally, you would choose to mark delegate fields as [NonSerialized], and recreate it when needed. If your main intent is to store the delegates, then I would recommend thinking of a completely different approach, frankly.

此外,注意的BinaryFormatter 脆,如果你打算保留数据的任何时间(但可以接受的瞬时数据)

Additionally, note that BinaryFormatter is brittle if you are planning to keep the data for any length of time (but acceptable for transient data)

进一步看,我怀疑我们需要看看一些重复性的代码。

To look further, I suspect we'd need to look at some reproducible code.


更新:其实,我怀疑你的可能的通过编写自己的明确捕获类(而不是编译器生成的)序列化。但我仍然认为这个概念是根本性的缺陷。和手写捕捉类是不好玩。

Update: actually, I suspect you could serialize it by writing your own explicit capture classes (rather than the compiler-generated ones). But I still think the concept is fundamentally flawed. And writing capture classes by hand isn't fun.


要解决点注释;再长期存储 - 因为它是所以该死脆 - 这是从改变简单:

To address the points in comments; re long term storage - because it is so darned brittle - something as simple as changing from:

public int Value {get;set;}

private int value;
public int Value {
    get {return value;}
    set {
        if(value < 0) throw new ArgumentOutOfRangeException();
        this.value = value;
    }
}



会破坏系列化;这将改变组件,类型名称,看着很可笑,等等。

will destroy serialization; as will changing assemblies, type names, "looking at it funny", etc.

重新代表。得到人工捕捉的例子;而不是:

Re the delegates; to give an example of a manual capture; instead of:

int i = ...
Predicate<Foo> test = delegate (Foo x) { return x.Bar == i;}

您可以做:

int i = ...
MyCapture cpt = new MyCapture(i);
Predicate<Foo> test = cpt.MyMethod;



with

[Serializable]
class MyCapture {
    private int i;
    public MyCapture(int i) {this.i = i;}
    public bool MyMethod(Foo x) {return x.Bar == i;}
}

正如你所看到 - 并不总是微不足道的(这是最简单的例子)

As you can see - not always trivial (this is the simplest of examples).

这篇关于序列化匿名委托列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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