Пример #1
0
        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));
        }
Пример #2
0
        public static object Resolve(Type serviceType, DependencyFetchTarget fallbackFetchTarget = DependencyFetchTarget.GlobalInstance)
        {
            if (serviceType is null)
            {
                throw new ArgumentNullException(nameof(serviceType));
            }

            var result = DependencyResolver.Resolve(serviceType);

            return(result ?? Get(serviceType, fallbackFetchTarget));
        }
    /// <summary>
    /// Get Xamarin service from type instead of generic parameter
    /// </summary>
    /// <param name="type"></param>
    /// <returns></returns>
    public static object Get(Type type, DependencyFetchTarget DependencyFetchTarget = DependencyFetchTarget.GlobalInstance)
    {
        MethodInfo baseMethod = null;
        MethodInfo getMethod  = null;

        getMethod  = SymbolExtensions.GetMethodInfo(() => DependencyService.Get <object>(DependencyFetchTarget));
        baseMethod = getMethod.GetGenericMethodDefinition();

        MethodInfo genericMethod = baseMethod.MakeGenericMethod(new Type[] { type });

        return(genericMethod.Invoke(null, new object[] { DependencyFetchTarget }));
    }
Пример #4
0
        public static object Get(Type targetType, DependencyFetchTarget fetchTarget = DependencyFetchTarget.GlobalInstance)
        {
            if (targetType is null)
            {
                throw new ArgumentNullException(nameof(targetType));
            }

            Initialize();

            DependencyData dependencyImplementation;

            lock (s_dependencyLock)
            {
                if (!DependencyImplementations.TryGetValue(targetType, out dependencyImplementation))
                {
                    Type implementor = FindImplementor(targetType);
                    DependencyImplementations[targetType] = (dependencyImplementation = implementor != null ? new DependencyData {
                        ImplementorType = implementor
                    } : null);
                }
            }

            if (dependencyImplementation == null)
            {
                return(null);
            }

            if (fetchTarget == DependencyFetchTarget.GlobalInstance)
            {
                if (dependencyImplementation.GlobalInstance == null)
                {
                    lock (dependencyImplementation)
                    {
                        if (dependencyImplementation.GlobalInstance == null)
                        {
                            dependencyImplementation.GlobalInstance = Activator.CreateInstance(dependencyImplementation.ImplementorType);
                        }
                    }
                }
                return(dependencyImplementation.GlobalInstance);
            }
            return(Activator.CreateInstance(dependencyImplementation.ImplementorType));
        }
        public IEnumerable <TSharedInterface> Get <TSharedInterface>(DependencyFetchTarget fetchTarget = DependencyFetchTarget.GlobalInstance) where TSharedInterface : class
        {
            Type type = typeof(TSharedInterface);

            IList <Type> implementationTypes;

            if (!this.dependencyImplementations.TryGetValue(type, out implementationTypes))
            {
                return(null);
            }

            var dependencyGetMi = typeof(DependencyService).GetMethod(nameof(DependencyService.Get), BindingFlags.Public | BindingFlags.Static);

            return(implementationTypes.Select(t =>
            {
                var dependencyGetSpecificTypeMi = dependencyGetMi.MakeGenericMethod(t);
                return dependencyGetSpecificTypeMi.Invoke(null, new object[1]) as TSharedInterface;
            })
                   .Where(t => t != null)
                   .ToList());
        }
Пример #6
0
        public static T Get <T>(DependencyFetchTarget fetchTarget = DependencyFetchTarget.GlobalInstance) where T : class
        {
            Initialize();

            DependencyData dependencyImplementation;

            lock (s_dependencyLock)
            {
                Type targetType = typeof(T);
                if (!DependencyImplementations.TryGetValue(targetType, out dependencyImplementation))
                {
                    Type implementor = FindImplementor(targetType);
                    DependencyImplementations[targetType] = (dependencyImplementation = implementor != null ? new DependencyData {
                        ImplementorType = implementor
                    } : null);
                }
            }

            if (dependencyImplementation == null)
            {
                return(null);
            }

            if (fetchTarget == DependencyFetchTarget.GlobalInstance)
            {
                if (dependencyImplementation.GlobalInstance == null)
                {
                    lock (dependencyImplementation)
                    {
                        if (dependencyImplementation.GlobalInstance == null)
                        {
                            dependencyImplementation.GlobalInstance = Activator.CreateInstance(dependencyImplementation.ImplementorType);
                        }
                    }
                }
                return((T)dependencyImplementation.GlobalInstance);
            }
            return((T)Activator.CreateInstance(dependencyImplementation.ImplementorType));
        }
Пример #7
0
        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));
        }
Пример #8
0
		public static T Resolve<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)] T>(DependencyFetchTarget fallbackFetchTarget = DependencyFetchTarget.GlobalInstance) where T : class
		{
			var result = DependencyResolver.Resolve(typeof(T)) as T;

			return result ?? Get<T>(fallbackFetchTarget);
		}
 protected void Register <TImpl>(Action <TImpl> init, DependencyFetchTarget target) where TImpl : class
 {
     DependencyService.Register <TImpl>();
     init(DependencyService.Get <TImpl>(target));
 }
Пример #10
0
 public static T Get <T>(DependencyFetchTarget fetchTarget = DependencyFetchTarget.GlobalInstance) where T : class
 {
     return((T)Get(typeof(T), fetchTarget));
 }