Exemple #1
0
        /// <summary>
        /// Creates the instance.
        /// </summary>
        /// <param name="type">The identifier.</param>
        /// <param name="args">The arguments.</param>
        /// <returns></returns>
        /// <exception cref="TypeInstantiationException"></exception>
        IRuntimeType GetRuntimeType(Type type, object[] args)
        {
            var runtimeTypes = Types.Values.Where((p) => p.ActivatorType == type && p.Count == args.Length && p.RuntimeTypes.IsAssignableFrom(Format.GetTypes(args))).ToArray();
            var runtimeType  = runtimeTypes.Length == 1 ? runtimeTypes[0] : Parser.FindRuntimeTypes(type.ToString(), Format.GetNames(args), Types.Values).FirstOrDefault();

            if (runtimeType == null)
            {
                throw new TypeRegistrationException(string.Format("{0} is not registered (parameter type mismatch)", type));
            }
            return(runtimeType);
        }
Exemple #2
0
        /// <summary>
        /// Creates the instance.
        /// </summary>
        /// <param name="typeFullName">The identifier.</param>
        /// <param name="args">The arguments.</param>
        /// <returns></returns>
        /// <exception cref="TypeInstantiationException"></exception>
        public object GetInstance(string typeFullName, object[] args)
        {
            typeFullName = IsNotNull(typeFullName);
            args         = IsNotNull(args);
            var runtimeType = TypeInvariants.ContainsKey(typeFullName) ? TypeInvariants[typeFullName] : null;

            if (runtimeType != null)
            {
                return(GetInstance(args, runtimeType));
            }
            if (Types.ContainsKey(typeFullName))
            {
                return(Types[typeFullName].CreateInstance(args));
            }
            var runtimeTypes = Types.Values.Where((p) => p.TypeFullName == typeFullName && p.Count == args.Length && p.RuntimeTypes.IsAssignableFrom(Format.GetTypes(args))).ToArray();

            if (runtimeTypes.Length == 0)
            {
                runtimeTypes = GetRuntimeTypes(Parser, typeFullName, args);
            }
            runtimeType = runtimeTypes.Length == 1 ? runtimeTypes[0] : null;
            if (runtimeType != null)
            {
                return(GetInstance(args, runtimeType));
            }
            runtimeTypes = Types.Values.Where((p) => p.TypeFullName == typeFullName && p.Count == args.Length && p.RuntimeTypes.IsOfType(Format.GetTypes(args))).ToArray();
            runtimeType  = runtimeTypes.Length == 1 ? runtimeTypes[0] : null;
            if (runtimeType != null)
            {
                return(GetInstance(args, runtimeType));
            }
            throw new TypeInstantiationException(string.Format("{0} is not instantiated (no matching constructors available)", typeFullName));
        }