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

Commit dbe1f83

Browse files
committed
Only return instance properties in PCL GetPublicProperties
1 parent 1586f7e commit dbe1f83

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

src/ServiceStack.Text/ReflectionExtensions.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -834,7 +834,14 @@ public static ConstructorInfo GetEmptyConstructor(this Type type)
834834
internal static PropertyInfo[] GetTypesPublicProperties(this Type subType)
835835
{
836836
#if (NETFX_CORE || PCL)
837-
return subType.GetRuntimeProperties().ToArray();
837+
var pis = new List<PropertyInfo>();
838+
foreach (var pi in subType.GetRuntimeProperties())
839+
{
840+
var mi = pi.GetMethod ?? pi.SetMethod;
841+
if (mi != null && mi.IsStatic) continue;
842+
pis.Add(pi);
843+
}
844+
return pis.ToArray();
838845
#else
839846
return subType.GetProperties(
840847
BindingFlags.FlattenHierarchy |
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using NUnit.Framework;
3+
4+
namespace ServiceStack.Text.Tests.JsonTests
5+
{
6+
[Flags]
7+
public enum DbModelComparisonTypesEnum : int
8+
{
9+
/// <summary>
10+
/// Compare only the PrimaryKey values
11+
/// </summary>
12+
PkOnly = 1,
13+
/// <summary>
14+
/// Compare only the non PrimaryKey values
15+
/// </summary>
16+
NonPkOnly = 2,
17+
/// <summary>
18+
/// Compare all values
19+
/// (The PrimaryKey and non PrimaryKey values too)
20+
/// </summary>
21+
All = 3 // PkOnly & NonPkOnly
22+
}
23+
24+
public partial class Question
25+
{
26+
public static DbModelComparisonTypesEnum DefaultComparisonType { get; set; }
27+
28+
public Guid Id { get; set; }
29+
public string Title { get; set; }
30+
}
31+
32+
[TestFixture]
33+
public class JsonEnumTests
34+
{
35+
[Test]
36+
public void Can_serialize_dto_with_static_enum()
37+
{
38+
Question.DefaultComparisonType = DbModelComparisonTypesEnum.All;
39+
40+
var dto = new Question
41+
{
42+
Id = Guid.NewGuid(),
43+
Title = "Title",
44+
};
45+
46+
var json = dto.ToJson();
47+
var q = json.FromJson<Question>();
48+
49+
Assert.That(q.Id, Is.EqualTo(dto.Id));
50+
Assert.That(q.Title, Is.EqualTo(dto.Title));
51+
}
52+
}
53+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@
177177
</ItemGroup>
178178
<ItemGroup>
179179
<Compile Include="AttributeTests.cs" />
180+
<Compile Include="JsonTests\JsonEnumTests.cs" />
180181
<Compile Include="JsonTests\InvalidJsonTests.cs" />
181182
<Compile Include="JsonTests\TypeInfoTests.cs" />
182183
<Compile Include="SerializationDelegatePerformanceTests.cs" />
@@ -352,6 +353,7 @@
352353
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
353354
</None>
354355
</ItemGroup>
356+
<ItemGroup />
355357
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
356358
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" />
357359
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

0 commit comments

Comments
 (0)