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

[C] avoid AmbiguousMatchexception in Bindings #4216

Merged
merged 1 commit into from Oct 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 29 additions & 5 deletions Xamarin.Forms.Core/Xaml/TypeConversionExtensions.cs
Expand Up @@ -196,17 +196,41 @@ internal static MethodInfo GetImplicitConversionOperator(this Type onType, Type
{
#if NETSTANDARD1_0
var mi = onType.GetRuntimeMethod("op_Implicit", new[] { fromType });
#else
var bindingFlags = BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy;
var mi = onType.GetMethod("op_Implicit", bindingFlags, null, new[] { fromType }, null);
#endif
if (mi == null) return null;
if (!mi.IsSpecialName) return null;
if (!mi.IsPublic) return null;
if (!mi.IsStatic) return null;
if (!toType.IsAssignableFrom(mi.ReturnType)) return null;

return mi;
return mi;
#else
var bindingAttr = BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy;
IEnumerable<MethodInfo> mis = null;
try {
mis = new[] { onType.GetMethod("op_Implicit", bindingAttr, null, new[] { fromType }, null) };
} catch (AmbiguousMatchException) {
mis = new List<MethodInfo>();
foreach (var mi in onType.GetMethods(bindingAttr)) {
if (mi.Name != "op_Implicit") break;
var p = mi.GetParameters()?.FirstOrDefault();
if (p == null) continue;
if (!p.ParameterType.IsAssignableFrom(fromType)) continue;
((List<MethodInfo>)mis).Add(mi);
}
}

foreach (var mi in mis) {
if (mi == null) continue;
if (!mi.IsSpecialName) continue;
if (!mi.IsPublic) continue;
if (!mi.IsStatic) continue;
if (!toType.IsAssignableFrom(mi.ReturnType)) continue;

return mi;
}
return null;
#endif

}
}
}
4 changes: 4 additions & 0 deletions Xamarin.Forms.Xaml.UnitTests/Issues/Gh4215.xaml
@@ -0,0 +1,4 @@
<?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.Gh4215">
<Label x:Name="l0" Text="{Binding .}" />
</ContentPage>
53 changes: 53 additions & 0 deletions Xamarin.Forms.Xaml.UnitTests/Issues/Gh4215.xaml.cs
@@ -0,0 +1,53 @@
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 Gh4215 : ContentPage
{
public class Gh4215VM
{
public static implicit operator DateTime(Gh4215VM value) => DateTime.UtcNow;
public static implicit operator string(Gh4215VM value) => "foo";
public static implicit operator long(Gh4215VM value) => long.MaxValue;
public static implicit operator Rectangle(Gh4215VM value) => new Rectangle();
}

public Gh4215()
{
InitializeComponent();
}

public Gh4215(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;
}

[TestCase(true), TestCase(false)]
public void AvoidAmbiguousMatch(bool useCompiledXaml)
{
var layout = new Gh4215();
Assert.DoesNotThrow(() => layout.BindingContext = new Gh4215VM());
Assert.That(layout.l0.Text, Is.EqualTo("foo"));
}
}
}
}
Expand Up @@ -655,6 +655,9 @@
<Compile Include="Issues\Gh4099.xaml.cs">
<DependentUpon>Gh4099.xaml</DependentUpon>
</Compile>
<Compile Include="Issues\Gh4215.xaml.cs">
<DependentUpon>Gh4215.xaml</DependentUpon>
</Compile>
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
Expand Down Expand Up @@ -1207,6 +1210,10 @@
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
<SubType>Designer</SubType>
</EmbeddedResource>
<EmbeddedResource Include="Issues\Gh4215.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
Expand Down