Skip to content

Commit d636d8e

Browse files
Merge pull request #829 from AArnott/fix828
Fix serialization of private properties on base classes
2 parents e7936c5 + 07aca11 commit d636d8e

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

src/MessagePack.UnityClient/Assets/Scripts/MessagePack/Resolvers/DynamicObjectResolver.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1510,7 +1510,7 @@ public static ObjectSerializationInfo CreateOrNull(Type type, bool forceStringKe
15101510
var searchFirst = true;
15111511
var hiddenIntKey = 0;
15121512

1513-
foreach (PropertyInfo item in type.GetRuntimeProperties())
1513+
foreach (PropertyInfo item in GetAllProperties(type))
15141514
{
15151515
if (item.GetCustomAttribute<IgnoreMemberAttribute>(true) != null)
15161516
{
@@ -1914,6 +1914,23 @@ private static IEnumerable<FieldInfo> GetAllFields(Type type)
19141914
}
19151915
}
19161916

1917+
private static IEnumerable<PropertyInfo> GetAllProperties(Type type)
1918+
{
1919+
if (type.BaseType is object)
1920+
{
1921+
foreach (var item in GetAllProperties(type.BaseType))
1922+
{
1923+
yield return item;
1924+
}
1925+
}
1926+
1927+
// with declared only
1928+
foreach (var item in type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly))
1929+
{
1930+
yield return item;
1931+
}
1932+
}
1933+
19171934
private static bool TryGetNextConstructor(IEnumerator<ConstructorInfo> ctorEnumerator, ref ConstructorInfo ctor)
19181935
{
19191936
if (ctorEnumerator == null || ctor != null)

src/MessagePack.UnityClient/Assets/Scripts/Tests/ShareTests/DynamicObjectResolverTests.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,15 @@ public int BaseClassFieldAccessor
116116
set => this.baseClassField = value;
117117
}
118118

119-
private int baseClassPropertyBackingField;
120-
121119
[DataMember]
120+
#pragma warning disable SA1300 // Element should begin with upper-case letter
121+
private int baseClassProperty { get; set; }
122+
#pragma warning restore SA1300 // Element should begin with upper-case letter
123+
122124
public int BaseClassProperty
123125
{
124-
get => this.baseClassPropertyBackingField;
125-
set => this.baseClassPropertyBackingField = value;
126+
get => this.baseClassProperty;
127+
set => this.baseClassProperty = value;
126128
}
127129
}
128130

0 commit comments

Comments
 (0)