Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 412633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T18:06:16+00:00 2026-05-12T18:06:16+00:00

This question may be very similar to my one, but I cannot see the

  • 0

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.

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]:

alt text http://bayimg.com/image/maebbaacj.jpg

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?


EDIT: 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().

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T18:06:16+00:00Added an answer on May 12, 2026 at 6:06 pm

    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.

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

    to

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

    you might do:

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may sound like a very generic question but here it goes. I have
This is very similar to a previous question (and may be the exact same
This may be very simple question,But please help me. i wanted to know what
this may be a very simple question, but is it possible to force a
This may be a very mundane question, but this is the first jQuery plugin
This may seem a very silly question. Consider this: I have a simple Boolean
This may seem to be an academic question, but still I would be very
This question is similar to this one , but hopefully it will pick up
Firstly, I realise that this is a very similar question to this one: Which
This question may sound to be similar to other questions, but when I check

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.