Bug 23414 - Compiling Property Setter Expression is Broken
Summary: Compiling Property Setter Expression is Broken
Status: CLOSED INVALID
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 4.18.0
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2014-09-26 12:46 UTC by Demis Bellot
Modified: 2015-01-13 12:38 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
CLOSED INVALID

Description Demis Bellot 2014-09-26 12:46:27 UTC
This test to create a late-bound property setter works in .NET but fail in Android:

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

public class PropertyExpressionTests
{
    [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"));
    }
}

With this exception:

System.ArgumentException: Method 'Void set_Name(System.String)' declared on type 'PclTest.Android.Test' cannot be called with instance of type 'System.String'
  at System.Linq.Expressions.Expression.ValidateCallInstanceType (System.Type instanceType, System.Reflection.MethodInfo method) [0x00000] in <filename unknown>:0 
  at System.Linq.Expressions.Expression.ValidateStaticOrInstanceMethod (System.Linq.Expressions.Expression instance, System.Reflection.MethodInfo method) [0x00000] in <filename unknown>:0 
  at System.Linq.Expressions.Expression.Call (System.Linq.Expressions.Expression instance, System.Reflection.MethodInfo method, IEnumerable`1 arguments) [0x00000] in <filename unknown>:0 
  at System.Linq.Expressions.Expression.Call (System.Linq.Expressions.Expression instance, System.Reflection.MethodInfo method, System.Linq.Expressions.Expression[] arguments) [0x00000] in <filename unknown>:0 
  at PclTest.Android.Activity1+<>c__DisplayClass8+<<OnCreate>b__7>d__a.MoveNext () [0x0007a] in c:\\src\\Ser
viceStack\\tests\\PclTest.Android\\Activity1.cs:98
Comment 1 Ram Chandra 2014-09-29 10:39:55 UTC
I have checked this issue but I am unable to reproduce this issue.

Steps I followed:

1. Create an Android Unit Test Project in Xamarin Studio.
2. Write the following test case in "TestSample.cs" file

        [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"));
        }

3. Add a new class in "TestSample.cs" file

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

4. Build and deploy the sample to the device/emulator.
5. Tab the "Run Tests" text.

I observed that when I run that test case, I am not getting any error/exception and the test case passed successfully.

Screencast:  http://www.screencast.com/t/gFKv5BqRTdq

Could you please provide a sample application and build information? So that we can reproduce this issue at our end.

You can get build information form here:  Xamarin Studio => Help => About => Show Details => Copy Information

Environment Info:

=== Xamarin Studio ===

Version 5.5 (build 221)
Installation UUID: 851620d2-e950-4682-b459-991ccec9d895
Runtime:
 Microsoft .NET 4.0.30319.18408
 GTK+ 2.24.22 (MS-Windows theme)
 GTK# 2.12.26

=== Xamarin.Android ===

Version: 4.18.0 (Business Edition)
Android SDK: D:\SDK\AndroidSDK
 Supported Android versions:
  1.6    (API level 4)
  2.1    (API level 7)
  2.2    (API level 8)
  2.3    (API level 10)
  3.0    (API level 11)
  3.1    (API level 12)
  3.2    (API level 13)
  4.0    (API level 14)
  4.0.3  (API level 15)
  4.1    (API level 16)
  4.2    (API level 17)
  4.3    (API level 18)
  4.4    (API level 19)
  4.4.87 (API level 20)
  4.5    (API level 21)
Java SDK: C:\Program Files (x86)\Java\jdk1.6.0_31
java version "1.6.0_31"
Java(TM) SE Runtime Environment (build 1.6.0_31-b05)
Java HotSpot(TM) Client VM (build 20.6-b01, mixed mode, sharing)

=== Build Information ===

Release ID: 505000221
Git revision: 5593ae2759cc72daba96ad1a7bcf2b584a26e1cc
Build date: 2014-09-25 15:50:23-04
Xamarin addins: 9c609d1ca384863aafef4336d94b4b55d059d4ff

=== Operating System ===

Windows 6.1.7601.65536 (64-bit)
Comment 2 Demis Bellot 2014-09-29 15:09:32 UTC
Just tried this out on a new Android App on VS.NET, actually it's my bad, I was using the ServiceStack.Text PCL wrapper for ReflectedType() which was falling back to PropertyType instead of the substitutable DeclaringType that's now fixed in this commit: https://github.com/ServiceStack/ServiceStack.Text/commit/d297f738af9ac2831694df9ed99d6d5da4363afe

Thanks for looking into this so quickly!
Comment 3 Ram Chandra 2015-01-13 12:38:34 UTC
As per comment 2, this issue works fine. Hence I am closing this issue.