Permalink
Switch branches/tags
tag-test3 tag-test2 tag-test1 rui-3.0.0 release-3.3.0-rc0 release-3.2.0 release-3.2.0-sr1 release-3.1.0-sr3 release-3.1.0-sr2 release-3.1.0-sr1.1 release-3.1.0-sr1 release-3.1.0-rc1 release-3.0.0-sr4.1 release-3.0.0-sr4 release-3.0.0-sr3 release-3.0.0-sr2 release-3.0.0-sr1 release-3.0.0-rc2 release-3.0.0-rc1 release-2.5.1 release-2.5.1-sr4 release-2.5.1-sr3 release-2.5.1-sr2 release-2.5.1-sr1 release-2.5.0 release-2.5.0-sr5 release-2.5.0-sr4 release-2.5.0-sr3 release-2.5.0-sr2 release-2.5.0-sr1 release-2.4.99-rc1 release-2.4.0 release-2.4.0-sr6 release-2.4.0-sr5 release-2.4.0-sr4 release-2.4.0-sr3 release-2.4.0-sr2 release-2.4.0-sr1 release-2.4.0-rc2 release-2.3.5 release-2.3.4 release-2.3.4-sr6 release-2.3.4-sr5 release-2.3.4-sr4 release-2.3.4-sr3 release-2.3.4-sr2 release-2.3.4-sr1 release-2.3.3 release-2.3.3-sr2 release-2.3.3-sr1.1 release-2.3.3-sr1 release-2.3.3-hf1 release-2.3.2 release-2.3.2-hf1 release-2.3.1 release-2.3.0 release-2.3.0-hf1 release-2.2.0 release-2.2.0-hf1 beta-cycle6buildtest beta-9.99.99-rui beta-9.9.999-rui beta-9.9.99-rui beta-9.9.15.5-rui beta-9.9.9.9-rui beta-9.9.9-rui beta-9.9.8-rui beta-3.4.0-pre1 beta-3.3.0-pre3 beta-3.3.0-pre2 beta-3.3.0-pre1 beta-3.2.0-pre3 beta-3.2.0-pre2 beta-3.2.0-pre1.1 beta-3.2.0-pre1 beta-3.1.0-pre4.1 beta-3.1.0-pre4 beta-3.1.0-pre3 beta-3.1.0-pre2.3 beta-3.1.0-pre2.2 beta-3.1.0-pre2 beta-3.1.0-pre1.2 beta-3.1.0-pre1 beta-3.0.0-rui beta-3.0.0-pre4.4 beta-3.0.0-pre4.3 beta-3.0.0-pre4.2 beta-3.0.0-pre4.1 beta-3.0.0-pre4 beta-3.0.0-pre3 beta-3.0.0-pre2.2 beta-3.0.0-pre2 beta-3.0.0-pre1.2 beta-3.0.0-pre1 beta-2.5.1-pre3 beta-2.5.1-pre2 beta-2.5.1-pre1 beta-2.5.0-pre3 beta-2.5.0-pre2 beta-2.5.0-pre1
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
135 lines (111 sloc) 3.65 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Xamarin.Forms.Internals;
namespace Xamarin.Forms
{
public static class DependencyService
{
static bool s_initialized;
static readonly List<Type> DependencyTypes = new List<Type>();
static readonly Dictionary<Type, DependencyData> DependencyImplementations = new Dictionary<Type, DependencyData>();
public static T Resolve<T>(DependencyFetchTarget fallbackFetchTarget = DependencyFetchTarget.GlobalInstance) where T : class
{
var result = DependencyResolver.Resolve(typeof(T)) as T;
return result ?? Get<T>(fallbackFetchTarget);
}
public static T Get<T>(DependencyFetchTarget fetchTarget = DependencyFetchTarget.GlobalInstance) where T : class
{
Initialize();
Type targetType = typeof(T);
if (!DependencyImplementations.ContainsKey(targetType))
{
Type implementor = FindImplementor(targetType);
DependencyImplementations[targetType] = implementor != null ? new DependencyData { ImplementorType = implementor } : null;
}
DependencyData dependencyImplementation = DependencyImplementations[targetType];
if (dependencyImplementation == null)
return null;
if (fetchTarget == DependencyFetchTarget.GlobalInstance)
{
if (dependencyImplementation.GlobalInstance == null)
{
dependencyImplementation.GlobalInstance = Activator.CreateInstance(dependencyImplementation.ImplementorType);
}
return (T)dependencyImplementation.GlobalInstance;
}
return (T)Activator.CreateInstance(dependencyImplementation.ImplementorType);
}
public static void Register<T>() where T : class
{
Type type = typeof(T);
if (!DependencyTypes.Contains(type))
DependencyTypes.Add(type);
}
public static void Register<T, TImpl>() where T : class where TImpl : class, T
{
Type targetType = typeof(T);
Type implementorType = typeof(TImpl);
if (!DependencyTypes.Contains(targetType))
DependencyTypes.Add(targetType);
DependencyImplementations[targetType] = new DependencyData { ImplementorType = implementorType };
}
static Type FindImplementor(Type target)
{
return DependencyTypes.FirstOrDefault(t => target.IsAssignableFrom(t));
}
static void Initialize()
{
if (s_initialized)
{
return;
}
Assembly[] assemblies = Device.GetAssemblies();
if (Internals.Registrar.ExtraAssemblies != null)
{
assemblies = assemblies.Union(Internals.Registrar.ExtraAssemblies).ToArray();
}
Initialize(assemblies);
}
internal static void Initialize(Assembly[] assemblies)
{
if (s_initialized)
{
return;
}
Type targetAttrType = typeof(DependencyAttribute);
// Don't use LINQ for performance reasons
// Naive implementation can easily take over a second to run
foreach (Assembly assembly in assemblies)
{
Attribute[] attributes;
try
{
attributes = assembly.GetCustomAttributes(targetAttrType).ToArray();
}
catch (System.IO.FileNotFoundException)
{
// Sometimes the previewer doesn't actually have everything required for these loads to work
Log.Warning(nameof(Registrar), "Could not load assembly: {0} for Attibute {1} | Some renderers may not be loaded", assembly.FullName, targetAttrType.FullName);
continue;
}
if (attributes.Length == 0)
continue;
foreach (DependencyAttribute attribute in attributes)
{
if (!DependencyTypes.Contains(attribute.Implementor))
{
DependencyTypes.Add(attribute.Implementor);
}
}
}
s_initialized = true;
}
class DependencyData
{
public object GlobalInstance { get; set; }
public Type ImplementorType { get; set; }
}
}
}