Serializing anonymous delegates in C#

前端 未结 6 1446
我在风中等你
我在风中等你 2020-12-01 02:59

I am trying to determine what issues could be caused by using the following serialization surrogate to enable serialization of anonymous functions/delegate/lambdas.

6条回答
  •  北荒
    北荒 (楼主)
    2020-12-01 03:36

    Since this state is local though that leads to issues when trying to setup a mapping.

    Wouldn't local state present the exact same problems for serialization?

    Suppose the compiler and the framework allowed this to work:

    Other o = FromSomeWhere();
    Thing t = OtherPlace();
    target.OnWhatever = () => t.DoFoo() + o.DoBar();
    target.Save();
    

    I guess t and o had to be serialized too. The methods don't have the state, the instances do.

    Later, you deserialize target. Don't you get new copies of t and o? Won't these copies be out of sync with any changes to the original t and o?

    Also: couldn't your manual example be called this way?

    Other o = FromSomeWhere();
    Thing t = OtherPlace();
    target.OnWhatever = new DoFooBar() {Other = o, Thing = t} .Run;
    target.Save();
    

提交回复
热议问题