ProtoBuf is not serializing a field in inherited class [duplicate] - c#

This question already has answers here:
How to Serialize Inherited Class with ProtoBuf-Net
(2 answers)
Closed 6 years ago.
I have a base class called PrivilegeType and it's inherited by various other classes.
Everything works fine except for one specific field called PossibleValues which doesn't get serialized/deserialized well.
Base class definition:
[ProtoContract]
[ProtoInclude(13, typeof(PrivilegeEnum))]
public abstract class PrivilegeType {
...
}
PrivilegeEnum definition:
[ProtoContract]
public class PrivilegeEnum : PrivilegeEnumLike<PrivilegeEnumValue> {
...
}
PrivilegeEnumLike<T> definition:
[ProtoContract]
public abstract class PrivilegeEnumLike<T> : PrivilegeType<T>
{
[ProtoMember(2)]
public Dictionary<string, PrivilegeEnumValue> PossibleValues;
...
}
PrivilegeEnumValue definition:
[ProtoContract]
public class PrivilegeEnumValue
{
[ProtoMember(1)]
public string Value;
[ProtoMember(2)]
public string Text;
[ProtoMember(3)]
public HashSet<PrivilegeEnumValue> ImpliedValues = new HashSet<PrivilegeEnumValue>();
...
}
The thing is, that field is defined inPrivilegeEnumLike<T> which is not included in ProtoInclude list itself, but its subclasses are. I can't add PrivilegeEnumLike<> to ProtoInclude list because I guess it doesn't make sense.

Here's the rule:
Class hierarchy must be implemented correctly.
This means you need to use ProtoInclude on parent classes, NOT necessarily base classes
Otherwise, any inherited class between your subclass and baseclass will not get serialized.
What I did:
Base class:
[ProtoContract]
[ProtoInclude(12, typeof(PrivilegeEnumLike<PrivilegeEnumValue>))]
[ProtoInclude(13, typeof(PrivilegeEnumLike<PrivilegeEnumValue[]>))]
public abstract class PrivilegeType
Middle class:
[ProtoContract]
[ProtoInclude(20, typeof(PrivilegeEnum))]
[ProtoInclude(21, typeof(PrivilegeEnumSet))]
public abstract class PrivilegeEnumLike<T> : PrivilegeType<T>
Everything works correctly now.
Please let me know if there's any better solution to this.

Related

Get derived (generic) class members from base class (or shared interface) reference

I am struggling to formulate my question properly, I hope I can clarify it through the following description:
I have an abstract generic base class that is supposed to describe a specific container item, upon other things, and another abstract class of the same name to reference the generic classes by. I also created an interface to reference them by, both work but still result in the same issue eventually. For this example i am using the shared base class
public abstract class ItemEditorState
{
}
and the derived generic class
public abstract class ItemEditorState<T> : ItemEditorState where T : Item
{
public abstract SimpleDatabase<T> Items { get; set; }
public abstract void DoStuff();
...
}
So far so good.
Now I have classes deriving from ItemEditorState<T> corresponding to different Items.
public class EditorStateItemA : ItemEditorState<ItemA>
{
private ItemADatabase _itemADatabase; //ItemADatabase is public class ItemADatabase : SimpleDatabase<ItemA> {}
public override SimpleDatabase<ItemA> Items { get => _itemADatabase; set => _itemADatabase = value; }
public override void DoStuff(){}
...
}
So far so good again.
Now I have a static manager class holding a List<ItemEditorState> of references to all ItemEditorStates, such as EditorStateItemA, EditorStateItemB etc.
public static class ItemEditorStateManager
{
public static List<ItemEditorState> itemEditorStates = new List<ItemEditorState>();
public int GetState(int index) => itemEditorStates[index];
}
But since I reference the derived class instances by the shared abstract base class (or in another case the IItemEditorState interface), I can't access any members.
What I would like to do is access the Items list of any of EditorStateItemA or EditorStateItemB within the list in the ItemEditorStateManagerfrom the ItemEditorStateManager.GetState(index) method.
I know I would probably have to cast it to the proper class first, but I don't know the specific type at that point. I am sure my architecture is off, but I can't wrap my head around it.

base class collection defined by derived class

I'm not even close to as experienced in C# as I am in C++, but trying to get better.
In C#, does there exist a way to create a base class that contains a property which is a List and then in the derived class define what T is for that concrete type?
public class Base
{
public List<T> Data { get; set; }
}
public class Derived : Base
{
// Declare to the world you use Base.data<Elephant>
// Callers of my Data property get Elephants
}
I imagine not, since you can no longer act on the interface in the base class since you wouldn't know what type you are getting until you know what type the actual instance is, but maybe there is some magical thing in C# that is similar to this?
You can make the base class generic like this:
public class Base<T>
{
public List<T> Data { get; set; }
}
And then when you create the derived class, you can specify T like this:
public class Derived : Base<Elephant>
{
}
For the consumers of Derived, the type of the Data property is List<Elephant>.

Generic base class with multiple children

I currently have a small object hierarchy that looks like this:
public class BaseClass {
// this class is empty and exists only so the others can extend it and share the
// same base type
}
public class ChildA : BaseClass {
public Subject<AssociatedClassA> Results;
}
public class ChildB : BaseClass {
public Subject<AssociatedClassB> Results;
}
In my design I would like to enforce that every class that extends from BaseClass should contain a Subject<SomeType> called Results. I'm wondering if there is a way that I can move Results into the base class or an interface such that I can supply the generic type for the Subject when constructing the base class. For example, it would be awesome if I could do something like this:
ChildA<AssociatedClassA> instance = new ChildA<AssociatedClassA>();
Or even better since there should really only be one template parameter that matches with ChildA if when I constructed it that could be taken care of for me:
ChildA instance = new ChildA();
// Results is automatically set to Subject<AssociatedClassA>
I'm stuck trying to implement this now as if I try to move Results into the base class the Subject requires a template parameter which I can't necessarily supply. There could potentially be more than 2 derived classes and I don't like the idea that someone extending this system has to know to add Results manually to each child class.
Following the suggestions of the 2 answers below this solves my desire to move Results into the base class, however I've run into another issue in that I was hoping to be able to use BaseClass as a generic parameter to methods such that any of the derived classes could be used. For example:
public void ProcessBaseClass(BaseClass base) {
// base could be ChildA or ChildB here
}
This no longer works since BaseClass now requires a type argument. Is there any way that I can have the best of both worlds here or am I stuck due to my design choices?
If appropriate, you can make the parent generic:
public class BaseClass<T> {
public Subject<T> Results;
}
public class ChildA : BaseClass<AssociatedClassA> {
}
public class ChildB : BaseClass<AssociatedClassB> {
}
You can make the base class itself generic:
public class BaseClass<T> {
public T Results { get; protected set; }
}

modifying access permissions from an abstract class in the derived class [duplicate]

This question already has answers here:
Adding a set accessor to a property in a class that derives from an abstract class with only a get accessor
(3 answers)
Closed 9 years ago.
So I have an abstract class called AbstactSearchWithTwoLevelCache that was provided to me. All of its abstract properties only have read access (with a get;). I am not permitted to add a set; to the those abstract properties. Is there a way to change this in the derived class, SearchWithTwoLevelCache? In other words, is there a way to set these properties in the derived class?
If you mark the property with new, you define a new property, like this:
abstract class BaseClass
{
public int Property
{
get { ... }
}
}
class NewClass : BaseClass
{
public new int Property
{
get { return base.Property; }
set { ... }
}
}
EDIT:
The above works if the property in the base class is not abstract. When it is abstract, this will not work since you need to implement it. One option you do have is to create a class in between, like this:
abstract class BaseClass
{
public abstract int Property { get; }
}
class Between : BaseClass
{
public override int Property
{
get { ... }
}
}
class NewClass : Between
{
public new int Property
{
get { return base.Property; }
set { ... }
}
}
This however in no way is an elegant solution. Then, the real answer becomes that you cannot really do this (at least not without the above work around).
I can't imagine a way you can override them directly. Indirectly when you override the abstract class properties, the Getters can retrieve from a private field, and then you can create new properties that have Setters that set those fields.
Kind of a rig I know. Maybe there is a more elegant way around this.

Abstract fields aren't allowed. How do I force a derived class to instantiate/initialize a field?

I have an abstract base class for a generic 'Device' type and would like to require the derived specific device classes to initialize a list based on their respective differences.
When one sets the derived class to active it must go through the list to set the parameters to active as well. Each derived class will insert their own parameters into the list.
Here is the relevant code:
Base class:
abstract public class Device : Common, IDisposable
{
abstract public Boolean bActive
{
get;
set;
}
abstract List<Parameters> ActiveParameters;
...
}
I don't wish to initialize the List in the base class because I do not know which parameters are going to be inserted into the list from the derived class. This code produces an error saying that I cannot have an abstract field and I understand that but is there a way to require a derived class to initialize a field from the base class?
This code produces an error saying that I cannot have an abstract field and I understand that but is there a way to require a derived class to initialize a field from the base class?
If this is required to be implemented, it should be part of your API. To handle this, you can make this a protected property. This will force it to be implemented by all derived classes.
protected abstract List<Parameters> ActiveParameters { get; }
If this is about initializing the list, then you can force that by putting that list into the constructor (or all constructors, if you have more of them) of the base class.
Something like:
public abstract class Device : Common, IDisposable
{
protected Device(List<Parameters> activeParameters)
{
ActiveParameters = activeParameters;
}
protected List<Parameters> ActiveParameters { get; private set; }
}
The derived class will be then forced to do something like:
public class ConcreteDevice : Device
{
public ConcreteDevice()
: base(new List<Parameters>())
{}
}

Categories

Resources