From d9a550b408169580ae2bd0e5634d9fe2f46a8ccc Mon Sep 17 00:00:00 2001 From: Stephane Delcroix Date: Mon, 26 Sep 2016 22:26:08 +0200 Subject: [Xaml] allow compatible arguments for x:Factory --- Xamarin.Forms.Xaml/CreateValuesVisitor.cs | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/Xamarin.Forms.Xaml/CreateValuesVisitor.cs b/Xamarin.Forms.Xaml/CreateValuesVisitor.cs index 04cd62cb..25a936fa 100644 --- a/Xamarin.Forms.Xaml/CreateValuesVisitor.cs +++ b/Xamarin.Forms.Xaml/CreateValuesVisitor.cs @@ -198,12 +198,27 @@ namespace Xamarin.Forms.Xaml var factoryMethod = ((string)((ValueNode)node.Properties[XmlName.xFactoryMethod]).Value); Type[] types = arguments == null ? new Type[0] : arguments.Select(a => a.GetType()).ToArray(); - var mi = nodeType.GetRuntimeMethod(factoryMethod, types); - if (mi == null || !mi.IsStatic) - { - throw new MissingMemberException(String.Format("No static method found for {0}::{1} ({2})", nodeType.FullName, - factoryMethod, string.Join(", ", types.Select(t => t.FullName)))); - } + Func isMatch = m => { + if (m.Name != factoryMethod) + return false; + var p = m.GetParameters(); + if (p.Length != types.Length) + return false; + if (!m.IsStatic) + return false; + for (var i = 0; i < p.Length; i++) { + if ((p [i].ParameterType.IsAssignableFrom(types [i]))) + continue; + var op_impl = p [i].ParameterType.GetRuntimeMethod("op_Implicit", new [] { types [i]}); + if (op_impl == null) + return false; + arguments [i] = op_impl.Invoke(null, new [] { arguments [i]}); + } + return true; + }; + var mi = nodeType.GetRuntimeMethods().FirstOrDefault(isMatch); + if (mi == null) + throw new MissingMemberException($"No static method found for {nodeType.FullName}::{factoryMethod} ({string.Join(", ", types.Select(t => t.FullName))})"); return mi.Invoke(null, arguments); } -- cgit v1.2.3