示例#1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="LocalizationConfiguration"/> class.
        /// </summary>
        public LocalizationConfiguration()
        {
            IServiceProviderEx serviceProvider = ServiceProviderLocator.Instance.LocateProvider();

            _SupportedCultures = new List <CultureInfo>();
            _Logger            = serviceProvider.GetService <ILogger>();
        }
        private static object CreateServiceInstance(IServiceProviderEx provider, Type serviceType, object[] serviceArgs)
        {
#if WINDOWS_STORE || WINDOWS_APP

            // if arguments were specified directly, simply create new object:
            if (serviceArgs != null)
                return Activator.CreateInstance(serviceType, serviceArgs);

            // or loop till find first constructor, for which it was possible to create all required arguments:
            foreach (var method in serviceType.GetTypeInfo().DeclaredConstructors)
            {
                var paramTypes = method.GetParameters();
                var paramValues = new object[paramTypes.Length];
                bool failedToInitialize = false;

                for (int i = 0; i < paramTypes.Length && !failedToInitialize; i++)
                {
                    // get the service for specified argument's type (what returns null on failure):
                    paramValues[i] = provider.GetService(paramTypes[i].ParameterType);
                    failedToInitialize = paramValues[i] == null;
                }

                if (failedToInitialize)
                    continue;

                return method.Invoke(paramValues);
            }

            // notify, that it was impossible to initialize the required service
            throw new ServiceCreationException(serviceType);
#else

            // if arguments specified directly, simply create new object:
            if (serviceArgs != null)
                return Activator.CreateInstance(serviceType, serviceArgs);

            // or loop till find first constructor, for which it was possible to create all required arguments:
            foreach (var method in serviceType.GetConstructors())
            {
                var paramTypes = method.GetParameters();
                var paramValues = new object[paramTypes.Length];
                bool failedToInitialize = false;

                for (int i = 0; i < paramTypes.Length && !failedToInitialize; i++)
                {
                    // get the service for specified argument's type (what returns null on failure):
                    paramValues[i] = provider.GetService(paramTypes[i].ParameterType);
                    failedToInitialize = paramValues[i] == null;
                }

                if (failedToInitialize)
                    continue;

                return method.Invoke(paramValues);
            }

            // notify, that it was impossible to initialize the required service
            throw new ServiceCreationException(serviceType);

#endif
        }
示例#3
0
        private static object CreateServiceInstance(IServiceProviderEx provider, Type serviceType, object[] serviceArgs)
        {
#if WINDOWS_STORE || WINDOWS_APP
            // if arguments were specified directly, simply create new object:
            if (serviceArgs != null)
            {
                return(Activator.CreateInstance(serviceType, serviceArgs));
            }

            // or loop till find first constructor, for which it was possible to create all required arguments:
            foreach (var method in serviceType.GetTypeInfo().DeclaredConstructors)
            {
                var  paramTypes         = method.GetParameters();
                var  paramValues        = new object[paramTypes.Length];
                bool failedToInitialize = false;

                for (int i = 0; i < paramTypes.Length && !failedToInitialize; i++)
                {
                    // get the service for specified argument's type (what returns null on failure):
                    paramValues[i]     = provider.GetService(paramTypes[i].ParameterType);
                    failedToInitialize = paramValues[i] == null;
                }

                if (failedToInitialize)
                {
                    continue;
                }

                return(method.Invoke(paramValues));
            }

            // notify, that it was impossible to initialize the required service
            throw new ServiceCreationException(serviceType);
#else
            // if arguments specified directly, simply create new object:
            if (serviceArgs != null)
            {
                return(Activator.CreateInstance(serviceType, serviceArgs));
            }

            // or loop till find first constructor, for which it was possible to create all required arguments:
            foreach (var method in serviceType.GetConstructors())
            {
                var  paramTypes         = method.GetParameters();
                var  paramValues        = new object[paramTypes.Length];
                bool failedToInitialize = false;

                for (int i = 0; i < paramTypes.Length && !failedToInitialize; i++)
                {
                    // get the service for specified argument's type (what returns null on failure):
                    paramValues[i]     = provider.GetService(paramTypes[i].ParameterType);
                    failedToInitialize = paramValues[i] == null;
                }

                if (failedToInitialize)
                {
                    continue;
                }

                return(method.Invoke(paramValues));
            }

            // notify, that it was impossible to initialize the required service
            throw new ServiceCreationException(serviceType);
#endif
        }