Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit a762da5

Browse files
authored
Merge pull request #475 from xplicit/fix_pcl
Fix PCL deserializing private set property in base type
2 parents 2e22239 + 1eb5254 commit a762da5

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

src/ServiceStack.Text/Common/DeserializeType.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ public static TypeAccessor Create(ITypeSerializer serializer, TypeConfig typeCon
293293

294294
private static SetPropertyDelegate GetSetPropertyMethod(TypeConfig typeConfig, PropertyInfo propertyInfo)
295295
{
296-
if (propertyInfo.ReflectedType() != propertyInfo.DeclaringType)
296+
if (typeConfig.Type != propertyInfo.DeclaringType)
297297
propertyInfo = propertyInfo.DeclaringType.GetPropertyInfo(propertyInfo.Name);
298298

299299
if (!propertyInfo.CanWrite && !typeConfig.EnableAnonymousFieldSetterses) return null;
@@ -345,7 +345,7 @@ public static TypeAccessor Create(ITypeSerializer serializer, TypeConfig typeCon
345345

346346
private static SetPropertyDelegate GetSetFieldMethod(TypeConfig typeConfig, FieldInfo fieldInfo)
347347
{
348-
if (fieldInfo.ReflectedType() != fieldInfo.DeclaringType)
348+
if (typeConfig.Type != fieldInfo.DeclaringType)
349349
fieldInfo = fieldInfo.DeclaringType.GetFieldInfo(fieldInfo.Name);
350350

351351
return PclExport.Instance.GetSetFieldMethod(fieldInfo);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using NUnit.Framework;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace ServiceStack.Text.Tests.JsonTests
9+
{
10+
[TestFixture]
11+
class InheritanceTest
12+
{
13+
public class BaseClass
14+
{
15+
protected string base64Value = string.Empty;
16+
public string Value { get { return this.base64Value; } private set { this.base64Value = value; } }
17+
18+
public void Set(string val)
19+
{
20+
this.base64Value = val;
21+
}
22+
}
23+
24+
public class Derived : BaseClass
25+
{
26+
}
27+
28+
[Test]
29+
public void Can_deserialize_private_set_in_base_class()
30+
{
31+
var derived = new Derived();
32+
derived.Set("test");
33+
34+
string serialized = derived.ToJson();
35+
var deserialized = serialized.FromJson<Derived>();
36+
37+
Assert.That(deserialized.Value, Is.EqualTo("test"));
38+
}
39+
}
40+
}

tests/ServiceStack.Text.Tests/ServiceStack.Text.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@
193193
<Compile Include="HttpUtilsTests.cs" />
194194
<Compile Include="Issues\JsConfigIssues.cs" />
195195
<Compile Include="JsonTests\InheritAbstractTests.cs" />
196+
<Compile Include="JsonTests\InheritanceTests.cs" />
196197
<Compile Include="JsonTests\JsonEnumTests.cs" />
197198
<Compile Include="JsonTests\InvalidJsonTests.cs" />
198199
<Compile Include="JsonTests\OnDeserializationErrorTests.cs" />

0 commit comments

Comments
 (0)