Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
[X] Box On Add
Browse files Browse the repository at this point in the history
Do not return false on CanAdd if all that's required is boxing.

- fixes #11620
  • Loading branch information
StephaneDelcroix committed Aug 3, 2020
1 parent 62b5928 commit 62a7dc8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
12 changes: 7 additions & 5 deletions Xamarin.Forms.Build.Tasks/SetPropertiesVisitor.cs
Expand Up @@ -910,7 +910,6 @@ public static IEnumerable<Instruction> SetPropertyValue(VariableDefinition paren
throw new BuildException(MemberResolution, iXmlLineInfo, null, localName);
}


public static IEnumerable<Instruction> GetPropertyValue(VariableDefinition parent, XmlName propertyName, ILContext context, IXmlLineInfo lineInfo, out TypeReference propertyType)
{
var module = context.Body.Method.Module;
Expand Down Expand Up @@ -1316,13 +1315,13 @@ static IEnumerable<Instruction> Get(VariableDefinition parent, string localName,

if (parent.VariableType.IsValueType)
return new[] {
Instruction.Create(OpCodes.Ldloca, parent),
Instruction.Create(OpCodes.Call, propertyGetterRef),
Create(Ldloca, parent),
Create(Call, propertyGetterRef),
};
else
return new[] {
Instruction.Create(OpCodes.Ldloc, parent),
Instruction.Create(OpCodes.Callvirt, propertyGetterRef),
Create(Ldloc, parent),
Create(Callvirt, propertyGetterRef),
};
}

Expand Down Expand Up @@ -1356,6 +1355,9 @@ static bool CanAdd(VariableDefinition parent, XmlName propertyName, INode valueN
if (varValue.VariableType.GetImplicitOperatorTo(paramType, module) != null)
return true;

if (paramType.FullName == "System.Object" && varValue.VariableType.IsValueType)
return true;

return CanAddToResourceDictionary(parent, propertyType, elementNode, lineInfo, context);
}

Expand Down
10 changes: 10 additions & 0 deletions Xamarin.Forms.Xaml.UnitTests/Issues/Gh11620.xaml
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Xamarin.Forms.Xaml.UnitTests.Gh11620">
<ContentPage.Resources>
<x:Array x:Key="myArray" Type="{x:Type x:Object}">
<x:String>Some string</x:String>
<x:Int32>69</x:Int32>
<x:Int32>32</x:Int32>
</x:Array>
</ContentPage.Resources>
</ContentPage>
34 changes: 34 additions & 0 deletions Xamarin.Forms.Xaml.UnitTests/Issues/Gh11620.xaml.cs
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;
using Xamarin.Forms;
using Xamarin.Forms.Core.UnitTests;

namespace Xamarin.Forms.Xaml.UnitTests
{
public partial class Gh11620 : ContentPage
{
public Gh11620() => InitializeComponent();
public Gh11620(bool useCompiledXaml)
{
//this stub will be replaced at compile time
}

[TestFixture]
class Tests
{
[SetUp] public void Setup() => Device.PlatformServices = new MockPlatformServices();
[TearDown] public void TearDown() => Device.PlatformServices = null;

[Test]
public void BoxOnAdd([Values(false, true)] bool useCompiledXaml)
{
var layout = new Gh11620(useCompiledXaml);
var arr = layout.Resources["myArray"];
Assert.That(arr, Is.TypeOf<object[]>());
Assert.That(((object[])arr).Length, Is.EqualTo(3));
Assert.That(((object[])arr)[2], Is.EqualTo(32));
}
}
}
}

0 comments on commit 62a7dc8

Please sign in to comment.