Skip to content

Commit 618062b

Browse files
committed
DynamicDictionary Get should deal with nullables
1 parent a53049d commit 618062b

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/Elasticsearch.Net/Responses/Dynamic/DynamicValue.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,14 +474,17 @@ private static object GetDynamicMember(object obj, string memberName)
474474
/// <returns>If value is not null, value is returned, else default value is returned</returns>
475475
public T TryParse<T>(T defaultValue = default(T))
476476
{
477+
var type = typeof(T);
478+
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
479+
type = type.GenericTypeArguments[0];
480+
477481
if (HasValue)
478482
{
479483
try
480484
{
481485
if (_value.GetType().IsAssignableFrom(typeof(T)))
482486
return (T)_value;
483487

484-
var type = typeof(T);
485488
var stringValue = _value as string;
486489

487490
if (type == typeof(DateTime))

src/Tests/Tests/ClientConcepts/LowLevel/LowLevelResponseTypes.doc.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public static string Response()
3131
return @"{
3232
""boolean"" : true,
3333
""string"" : ""v"",
34+
""number"" : 29,
3435
""array"" : [1, 2, 3, 4],
3536
""object"" : {
3637
""first"" : ""value1"",
@@ -81,6 +82,10 @@ [U] public void DynamicResponse()
8182

8283
response.Get<int>("array.1").Should().Be(2);
8384
response.Get<long>("array.1").Should().Be(2);
85+
response.Get<long>("number").Should().Be(29);
86+
response.Get<long?>("number").Should().Be(29);
87+
response.Get<long?>("number_does_not_exist").Should().Be(null);
88+
response.Get<long?>("number").Should().Be(29);
8489

8590
response.Get<string>("array_of_objects.1.second").Should()
8691
.NotBeEmpty()

0 commit comments

Comments
 (0)