-
-
Notifications
You must be signed in to change notification settings - Fork 673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Private fields on base class do not get serialized #820
Comments
I feel like In the meantime, you can get anything to work with a custom formatter. |
Maybe @OlegNadymov can clarify what version of MessagePack produced the error. |
@AArnott @neuecc thank you for the feedback! [DataContract]
public class BaseClass
{
[DataMember]
private int _intField;
public int IntField => _intField;
internal void SetIntField(int intField)
{
_intField = intField;
}
}
[DataContract]
public class DerivedClass : BaseClass
{
[DataMember]
public string Name { get; set; }
} The test: [Fact]
public void PrivateFieldFromBaseClassSerializeTest()
{
// Arrange
var obj = new DerivedClass { Name = "name" };
obj.SetIntField(100);
// Act
var bin = MessagePackSerializer.Serialize(obj, StandardResolverAllowPrivate.Options);
var obj2 = MessagePackSerializer.Deserialize<DerivedClass>(bin, StandardResolverAllowPrivate.Options);
// Assert
Assert.Equal(obj.IntField, obj2.IntField);
} The test result: |
Ok.
Replace iterate method like this. static IEnumerable<FieldInfo> GetAllFields(Type type)
{
if (type.BaseType != null)
{
foreach (var item in GetAllFields(type.BaseType))
{
yield return item;
}
}
// with declared only
foreach (var item in type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
{
yield return item;
}
} |
One more addition. I don't know if anybody uses private properties. But if some developers use private properties and these properties are declared in base classes, then the properties do not get serialized. I think I don't use private properties so I can't realize whether it is necessary to add support for this case or not. What do you think? |
I added a test for private properties on a base class and so far, the tests as already passing. |
@AArnott Private properties on base classes do not get serialized. I mean this case: [DataContract]
public class BaseClass
{
[DataMember]
private string PrivateBaseNote { get; set; }
internal void SetPrivateBaseNote(string note)
{
PrivateBaseNote = note;
}
}
[DataContract]
public class DerivedClass : BaseClass
{
[DataMember]
public int DerivedClassName { get; set; }
} If you serialize @neuecc, anyway, could tell me when you will be able to release the fix for the original issue on NuGet? |
@OlegNadymov In my PR I added a test that shows private properties on the base class do get serialized. |
@AArnott in your PR there are public properties with private fields. But in my example there is private auto-implemented property (without backing fields) Your test: // private field (not property)
[DataMember]
private int baseClassField; My example // private property (not field)
[DataMember]
private string PrivateBaseNote { get; set; } I hope it will be clear for understanding particular trait in my example. |
Ah, crap. I meant for that property in my test to be private. That's what I overlooked. Thanks! |
I was happy to help! |
I have business classes derived from a base class. The base class has several private fields that should to be serialized.
Simplified demo example:
Is it possible to serialize/deserialize
DerivedClass
with_intField
fromBaseClass
? I tried with this code:But I had
_intField = 0
.If it is not possible could you suggest some workaround for my situation.
Important addition: I can't change
BaseClass
.The text was updated successfully, but these errors were encountered: