diff options
author | Stephane Delcroix <stephane@delcroix.org> | 2016-09-26 22:40:04 +0200 |
---|---|---|
committer | Jason Smith <jason.smith@xamarin.com> | 2016-09-26 13:40:04 -0700 |
commit | 2d5785731ed0062af80a0e85763316a63f64afa8 (patch) | |
tree | b910d1c716cc0e64611d5f5362abdf57da7cfe59 /Xamarin.Forms.Xaml | |
parent | 16295a31393ff8ea0011a9837f709cb063a245a1 (diff) | |
download | xamarin-forms-2d5785731ed0062af80a0e85763316a63f64afa8.tar.gz xamarin-forms-2d5785731ed0062af80a0e85763316a63f64afa8.tar.bz2 xamarin-forms-2d5785731ed0062af80a0e85763316a63f64afa8.zip |
[Xaml] allow compatible arguments for x:Factory (#382)
Diffstat (limited to 'Xamarin.Forms.Xaml')
-rw-r--r-- | Xamarin.Forms.Xaml/CreateValuesVisitor.cs | 27 |
1 files 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<MethodInfo, bool> 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); } |