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

Commit 635cfb3

Browse files
committed
Make .NET Core's InstanceOfType impl match .NET 4.5
1 parent 840bab2 commit 635cfb3

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

src/ServiceStack.Text/PlatformExtensions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -972,9 +972,11 @@ public static MethodInfo GetMethodInfo(this PropertyInfo pi, bool nonPublic = tr
972972
public static bool InstanceOfType(this Type type, object instance)
973973
{
974974
#if (NETFX_CORE || PCL || NETSTANDARD1_1)
975-
return instance.GetType().IsInstanceOf(type);
975+
var result = instance != null && type.GetTypeInfo().IsAssignableFrom(instance.GetType().GetTypeInfo()); //https://stackoverflow.com/a/24712250/85785
976+
return result;
976977
#else
977-
return type.IsInstanceOfType(instance);
978+
var result = type.IsInstanceOfType(instance);
979+
return result;
978980
#endif
979981
}
980982

tests/ServiceStack.Text.Tests.xUnit/UnitTest1.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.IO;
3+
using System.Reflection;
34
using ServiceStack.Web;
45
using Xunit;
56

@@ -19,5 +20,11 @@ public void Can_create_DTO_with_Stream()
1920
var o = typeof(RawRequest).CreateInstance();
2021
var requestObj = AutoMappingUtils.PopulateWith(o);
2122
}
23+
24+
[Fact]
25+
public void Nullable_int_and_object_int_are_of_same_Type()
26+
{
27+
Assert.True(typeof(int?).IsInstanceOfType(1));
28+
}
2229
}
2330
}

tests/ServiceStack.Text.Tests/AutoMappingObjectDictionaryTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using Northwind.Common.DataModel;
23
using NUnit.Framework;
34

45
namespace ServiceStack.Text.Tests
@@ -77,5 +78,34 @@ public void Can_Convert_from_ObjectDictionary_with_Different_Types_with_camelCas
7778
Assert.That(fromDict.Car.Age, Is.EqualTo(10));
7879
Assert.That(fromDict.Car.Name, Is.EqualTo("SubCar"));
7980
}
81+
82+
public class QueryCustomers : QueryDb<Customer>
83+
{
84+
public string CustomerId { get; set; }
85+
public string[] CountryIn { get; set; }
86+
public string[] CityIn { get; set; }
87+
}
88+
89+
[Test]
90+
public void Can_convert_from_ObjectDictionary_into_AutoQuery_DTO()
91+
{
92+
var map = new Dictionary<string, object>
93+
{
94+
{ "CustomerId", "CustomerId"},
95+
{ "CountryIn", new[]{"UK", "Germany"}},
96+
{ "CityIn", "London,Berlin"},
97+
{ "take", 5 },
98+
{ "Meta", "{foo:bar}" },
99+
};
100+
101+
var request = map.FromObjectDictionary<QueryCustomers>();
102+
103+
Assert.That(request.CustomerId, Is.EqualTo("CustomerId"));
104+
Assert.That(request.CountryIn, Is.EquivalentTo(new[]{"UK", "Germany" }));
105+
Assert.That(request.CityIn, Is.EquivalentTo(new[]{ "London", "Berlin" }));
106+
Assert.That(request.Take, Is.EqualTo(5));
107+
Assert.That(request.Meta, Is.EquivalentTo(new Dictionary<string, object> {{"foo", "bar"}}));
108+
}
109+
80110
}
81111
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using NUnit.Framework;
3+
4+
namespace ServiceStack.Text.Tests.Issues
5+
{
6+
public class IsInstanceOfTypeIssues
7+
{
8+
[Test]
9+
public void Nullable_int_and_object_int_are_of_same_Type()
10+
{
11+
Assert.That(typeof(int?).IsInstanceOfType(1));
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)