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 535231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:41:42+00:00 2026-05-13T09:41:42+00:00

I have a class, which holds a static dictionary of all existing instances, which

  • 0

I have a class, which holds a static dictionary of all existing instances, which are defined at compile time.

Basically it looks like this:

[DataContract]
class Foo
{
  private static Dictionary<long, Foo> instances = new Dictionary<long, Foo>();

  [DataMember]
  private long id;

  public static readonly Foo A = Create(1);
  public static readonly Foo B = Create(2);
  public static readonly Foo C = Create(3);

  private static Foo Create(long id)
  {
    Foo instance = new Foo();
    instance.id = id;
    instances.Add(instance);
    return instance;
  }

  public static Foo Get(long id)
  {
    return instances[id];
  }    

}

There are other fields, and the class is derived, but this doesn’t matter for the problem.

Only the id is serialized. When an instance of this type is deserialized, I would like to get the instance that has been created as the static field (A, B or C), using Foo.Get(id) instead of getting a new instance.

Is there a simple way to do this? I didn’t find any resources which I was able to understand.

  • 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-13T09:41:42+00:00Added an answer on May 13, 2026 at 9:41 am

    During deserialization it (AFAIK) always uses a new object (FormatterServices.GetUninitializedObject), but to get it to substitute the objects after deserialization (but before they are returned to the caller), you can implement IObjectReference, like so:

    [DataContract]
    class Foo : IObjectReference { // <===== implement an extra interface
        object IObjectReference.GetRealObject(StreamingContext ctx) {
            return Get(id);
        }
        ...snip
    }
    

    done… proof:

    static class Program {
        static void Main() {
            Foo foo = Foo.Get(2), clone;
            DataContractSerializer ser = new DataContractSerializer(typeof(Foo));
            using (MemoryStream ms = new MemoryStream()) { // clone it via DCS
                ser.WriteObject(ms, foo);
                ms.Position = 0;
                clone = (Foo)ser.ReadObject(ms);
            }
            Console.WriteLine(ReferenceEquals(foo, clone)); // true
        }
    }
    

    Note there are some extra notes on this for partial trust scenarios on MSDN, here.

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

Sidebar

Related Questions

I have written an NHibernateSessionFactory class which holds a static Nhibernate ISessionFactory. This is
I have written a class (AbcdBase) which holds several static objects, these include maps
I have a complex class for configuration, which holds all the configuration data, and
I have a FutureContent class which just holds a static reference to a Future
I have a MapTile class which holds a tile sprite and the tile's attribute.
I have a class which is essentially just holds a bunch of constant definitions
In my model, I have a base object class A which holds a set
I have a class that holds a few vectors, I'm not sure which method
I'd like some guidance on which class should hold certain information. If I have
So here's what I mean. Let's say I have a class ErrorMessages which holds

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.