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

Commit

Permalink
Add fallback for Android and property setter tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mythz committed Sep 26, 2014
1 parent 5c0ac5f commit cd0760c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
20 changes: 14 additions & 6 deletions src/ServiceStack.Text/PclExport.Net40.cs
Expand Up @@ -238,15 +238,23 @@ public override PropertySetterDelegate GetPropertySetterFn(PropertyInfo property
propertySetMethod.Invoke(o, new[] { convertedValue });
}

var instance = Expression.Parameter(typeof(object), "i");
var argument = Expression.Parameter(typeof(object), "a");
try
{
var instance = Expression.Parameter(typeof(object), "i");
var argument = Expression.Parameter(typeof(object), "a");

var instanceParam = Expression.Convert(instance, propertyInfo.ReflectedType());
var valueParam = Expression.Convert(argument, propertyInfo.PropertyType);
var instanceParam = Expression.Convert(instance, propertyInfo.ReflectedType());
var valueParam = Expression.Convert(argument, propertyInfo.PropertyType);

var setterCall = Expression.Call(instanceParam, propertyInfo.SetMethod(), valueParam);
var setterCall = Expression.Call(instanceParam, propertySetMethod, valueParam);

return Expression.Lambda<PropertySetterDelegate>(setterCall, instance, argument).Compile();
return Expression.Lambda<PropertySetterDelegate>(setterCall, instance, argument).Compile();
}
catch //fallback for Android
{
return (o, convertedValue) =>
propertySetMethod.Invoke(o, new[] { convertedValue });
}
}

public override PropertyGetterDelegate GetPropertyGetterFn(PropertyInfo propertyInfo)
Expand Down
49 changes: 48 additions & 1 deletion tests/ServiceStack.Text.Tests/AutoMappingTests.cs
@@ -1,5 +1,7 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.Serialization;
using NUnit.Framework;
using ServiceStack.Text.Tests.DynamicModels;
Expand Down Expand Up @@ -565,4 +567,49 @@ public void Can_change_ignored_properties()
Assert.That(dto.ToJson(), Is.EqualTo("{\"Id\":0,\"JsonIgnoreId\":0}"));
}
}

public class Test
{
public string Name { get; set; }
}

public class PropertyExpressionTests
{
[Test]
public void Can_call_typed_setter_Expressions()
{
var nameProperty = typeof(Test).GetProperty("Name");
var setMethod = nameProperty.GetSetMethod();

var instance = Expression.Parameter(typeof(Test), "i");
var argument = Expression.Parameter(typeof(string), "a");

var setterCall = Expression.Call(instance, setMethod, argument);
var fn = Expression.Lambda<Action<Test, string>>(setterCall, instance, argument).Compile();

var test = new Test();
fn(test, "Foo");
Assert.That(test.Name, Is.EqualTo("Foo"));
}

[Test]
public void Can_call_object_setter_Expressions()
{
var nameProperty = typeof(Test).GetProperty("Name");

var instance = Expression.Parameter(typeof(object), "i");
var argument = Expression.Parameter(typeof(object), "a");

var instanceParam = Expression.Convert(instance, nameProperty.ReflectedType());
var valueParam = Expression.Convert(argument, nameProperty.PropertyType);

var setterCall = Expression.Call(instanceParam, nameProperty.SetMethod(), valueParam);

var fn = Expression.Lambda<Action<object, object>>(setterCall, instance, argument).Compile();

var test = new Test();
fn(test, "Foo");
Assert.That(test.Name, Is.EqualTo("Foo"));
}
}
}

0 comments on commit cd0760c

Please sign in to comment.