summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephane Delcroix <stephane@delcroix.org>2016-09-26 22:26:08 +0200
committerJason Smith <jason.smith@xamarin.com>2016-09-26 13:40:14 -0700
commitd9a550b408169580ae2bd0e5634d9fe2f46a8ccc (patch)
tree0b446ee89a986972995782886b0289a144243f0e
parentdef2ba43b21e2c3c1ef57f14d3e0a4c516e8e0a9 (diff)
downloadxamarin-forms-d9a550b408169580ae2bd0e5634d9fe2f46a8ccc.tar.gz
xamarin-forms-d9a550b408169580ae2bd0e5634d9fe2f46a8ccc.tar.bz2
xamarin-forms-d9a550b408169580ae2bd0e5634d9fe2f46a8ccc.zip
[Xaml] allow compatible arguments for x:Factory
-rw-r--r--Xamarin.Forms.Xaml/CreateValuesVisitor.cs27
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);
}