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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:04:45+00:00 2026-05-26T12:04:45+00:00

We have the following.. public class Foo { public string Name { get; private

  • 0

We have the following..

public class Foo
{
   public string Name { get; private set;}

   private Foo(string name)
   {
      Name = name;
   }

   public Foo Instance1 = new Foo("Hello");
   public Foo Instance2 = new Foo("World");
}

And then referencing this we would have..

[ProtoContract]
public class Bar 
{
   [ProtoMember(1)]
   public Foo Foo { get; private set; }

   public Bar(Foo foo)
   {
      Foo = foo;
   }
}

So when I deserialize a Bar I need it to get a reference to either Foo.Instance1 or Foo.Instance2..

The question is can I do this and if so how?

  • 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-26T12:04:46+00:00Added an answer on May 26, 2026 at 12:04 pm

    There are several ways this could be approached. The simplest would be to add a shim property, i.e.

    public Foo Foo { get; private set; }
    
    [ProtoMember(1)]
    private SomeBasicEnum FooSerialization {
       /* shim between Foo and SomeBasicEnum in get/set */
    }
    

    However, if you have lots of Foo properties this could be a pain. So instead, v2 offers “surrogate” types – i.e. as long as it can locate a conversion operator between 2 types, it will happily swap them for you automatically. In this case we’d want to swap to an enum, and since you can’t add operators to enums you’d have to add the operator to Foo:

    public static implicit operator Foo(FooSurrogate value)
    {
        switch (value)
        {
            case FooSurrogate.Nil: return null;
            case FooSurrogate.Instance1: return Foo.Instance1;
            case FooSurrogate.Instance2: return Foo.Instance2;
            default: throw new InvalidEnumArgumentException("value");
        }
    }
    public static implicit operator FooSurrogate(Foo value)
    {
        if (value == null) return FooSurrogate.Nil;
        if (value == Foo.Instance1) return FooSurrogate.Instance1;
        if (value == Foo.Instance2) return FooSurrogate.Instance2;
        throw new InvalidEnumArgumentException("value");
    }
    

    and have a simple enum somewhere:

    public enum FooSurrogate
    {
        Nil, Instance1, Instance2
    }
    

    And configure it (somewhere at app-startup):

    RuntimeTypeModel.Default.Add(typeof(Foo), false).SetSurrogate(
                       typeof(FooSurrogate));
    

    And we’re good to go. A minor tweak is also required because Bar lacks a parameterless constructor; 2 options here:

    1. add a private Bar() {} that it can use
    2. explicitly tell it not to use a constructor: [ProtoContract(SkipConstructor = true)]

    Add a test rig:

    static void Main()
    {
        RuntimeTypeModel.Default.Add(typeof(Foo), false).SetSurrogate(
                  typeof(FooSurrogate));
        var obj = new Bar(Foo.Instance1);
        var clone = Serializer.DeepClone(obj);
    
        bool same = ReferenceEquals(obj.Foo, clone.Foo);
        Debug.Assert(same); // passes
    }
    

    I could probably also make it possible to use external operator-esque methods to avoid having to have the operator on Foo, which is a bit ugly.

    A final option would be for me to add IObjectReference support, but frankly (and especially in this case), using a basic enum for the implementation is tidier and more efficient.

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

Sidebar

Related Questions

public class Foo { public string Name { get; private set;} // <-- Because
Given the following Model, public class A { public string Name { get; set;
Given the class: public class Foo { public string Name { get; set; }
I have the following pojo: public class Foo { @Size(min=0,max=10) private String bar =
I have the following (Java) code: public class TestBlah { private static final String
I have the following class: public class foo { public Dictionary<string, string> data =
I have the following code: public class MyElement { String name; String type; MyElement(String
ok say i have the following model: public class bar{ public string bar {get;
I have the following code: [test.h] class MyClass { public: string Name; MyClass(); void
I have approximately the following picture: public class Foo { public Foo(Bar bar, String

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.