3 using System.Reflection;
4 using Tizen.NUI.XamlBinding;
6 namespace Tizen.NUI.XamlBinding
8 internal static class DependencyResolver
10 static Func<Type, object[], object> Resolver { get; set; }
12 public static void ResolveUsing(Func<Type, object[], object> resolver)
17 public static void ResolveUsing(Func<Type, object> resolver)
19 Resolver = (type, objects) => resolver.Invoke(type);
22 internal static object Resolve(Type type, params object[] args)
24 var result = Resolver?.Invoke(type, args);
28 if (!type.IsInstanceOfType(result))
30 throw new InvalidCastException("Resolved instance is not of the correct type.");
37 internal static object ResolveOrCreate(Type type, params object[] args)
39 var result = Resolve(type, args);
41 if (result != null) return result;
45 // This is by no means a general solution to matching with the correct constructor, but it'll
46 // do for finding Android renderers which need Context (vs older custom renderers which may still use
47 // parameterless constructors)
48 if (type.GetTypeInfo().DeclaredConstructors.Any(info => info.GetParameters().Length == args.Length))
50 return Activator.CreateInstance(type, args);
54 return Activator.CreateInstance(type);