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

Commit cd0760c

Browse files
committed
Add fallback for Android and property setter tests
1 parent 5c0ac5f commit cd0760c

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

src/ServiceStack.Text/PclExport.Net40.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -238,15 +238,23 @@ public override PropertySetterDelegate GetPropertySetterFn(PropertyInfo property
238238
propertySetMethod.Invoke(o, new[] { convertedValue });
239239
}
240240

241-
var instance = Expression.Parameter(typeof(object), "i");
242-
var argument = Expression.Parameter(typeof(object), "a");
241+
try
242+
{
243+
var instance = Expression.Parameter(typeof(object), "i");
244+
var argument = Expression.Parameter(typeof(object), "a");
243245

244-
var instanceParam = Expression.Convert(instance, propertyInfo.ReflectedType());
245-
var valueParam = Expression.Convert(argument, propertyInfo.PropertyType);
246+
var instanceParam = Expression.Convert(instance, propertyInfo.ReflectedType());
247+
var valueParam = Expression.Convert(argument, propertyInfo.PropertyType);
246248

247-
var setterCall = Expression.Call(instanceParam, propertyInfo.SetMethod(), valueParam);
249+
var setterCall = Expression.Call(instanceParam, propertySetMethod, valueParam);
248250

249-
return Expression.Lambda<PropertySetterDelegate>(setterCall, instance, argument).Compile();
251+
return Expression.Lambda<PropertySetterDelegate>(setterCall, instance, argument).Compile();
252+
}
253+
catch //fallback for Android
254+
{
255+
return (o, convertedValue) =>
256+
propertySetMethod.Invoke(o, new[] { convertedValue });
257+
}
250258
}
251259

252260
public override PropertyGetterDelegate GetPropertyGetterFn(PropertyInfo propertyInfo)

tests/ServiceStack.Text.Tests/AutoMappingTests.cs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
4+
using System.Linq.Expressions;
35
using System.Runtime.Serialization;
46
using NUnit.Framework;
57
using ServiceStack.Text.Tests.DynamicModels;
@@ -565,4 +567,49 @@ public void Can_change_ignored_properties()
565567
Assert.That(dto.ToJson(), Is.EqualTo("{\"Id\":0,\"JsonIgnoreId\":0}"));
566568
}
567569
}
570+
571+
public class Test
572+
{
573+
public string Name { get; set; }
574+
}
575+
576+
public class PropertyExpressionTests
577+
{
578+
[Test]
579+
public void Can_call_typed_setter_Expressions()
580+
{
581+
var nameProperty = typeof(Test).GetProperty("Name");
582+
var setMethod = nameProperty.GetSetMethod();
583+
584+
var instance = Expression.Parameter(typeof(Test), "i");
585+
var argument = Expression.Parameter(typeof(string), "a");
586+
587+
var setterCall = Expression.Call(instance, setMethod, argument);
588+
var fn = Expression.Lambda<Action<Test, string>>(setterCall, instance, argument).Compile();
589+
590+
var test = new Test();
591+
fn(test, "Foo");
592+
Assert.That(test.Name, Is.EqualTo("Foo"));
593+
}
594+
595+
[Test]
596+
public void Can_call_object_setter_Expressions()
597+
{
598+
var nameProperty = typeof(Test).GetProperty("Name");
599+
600+
var instance = Expression.Parameter(typeof(object), "i");
601+
var argument = Expression.Parameter(typeof(object), "a");
602+
603+
var instanceParam = Expression.Convert(instance, nameProperty.ReflectedType());
604+
var valueParam = Expression.Convert(argument, nameProperty.PropertyType);
605+
606+
var setterCall = Expression.Call(instanceParam, nameProperty.SetMethod(), valueParam);
607+
608+
var fn = Expression.Lambda<Action<object, object>>(setterCall, instance, argument).Compile();
609+
610+
var test = new Test();
611+
fn(test, "Foo");
612+
Assert.That(test.Name, Is.EqualTo("Foo"));
613+
}
614+
}
568615
}

0 commit comments

Comments
 (0)