/// <summary> /// Registers type /// </summary> /// <param name="typeId">Type identifier</param> public void RegisterType(string typeId, params object[] args) { if (args == null) { throw new TypeRegistrationException(string.Format("{0} is null (parameters required)", nameof(args))); } if (typeId == null) { throw new TypeRegistrationException(string.Format("{0} is null (type name required)", nameof(typeId))); } if (IsLocked) { throw new TypeRegistrationException(string.Format("{0} is not registered (container locked)", typeId)); } var runtimeTypes = Parser.FindRuntimeTypes(typeId, ArrayExtensions.Empty <string>(), Types.Values).ToArray(); if (runtimeTypes.Length == 1) { var runtimeType = runtimeTypes[0]; runtimeType.SetRuntimeInstance(Options.GetInstance); runtimeType.RegisterConstructorParameters(args.Length == 0 ? ArrayExtensions.ToArray <object>(runtimeType.Count) : args); return; } throw new TypeRegistrationException(string.Format("{0} is not registered (parameter type mismatch)", typeId)); }
/// <summary> /// Finds the specified identifier. /// </summary> /// <param name="typeId">The identifier.</param> /// <param name="args">The arguments.</param> /// <param name="types">The types.</param> /// <returns></returns> public IEnumerable <IRuntimeType> FindRuntimeTypes(string typeId, IEnumerable <string> args, IEnumerable <IRuntimeType> types) { var id = Regex.Replace(typeId, @"\s", ""); var func = Regex.Match(id, @"([^()]+)(?:\((.*)\)){0,1}$"); var constructor = id == func.Groups[1].Value ? Format.GetConstructor(id, args) : id; var cached = Cache.Where((p) => p.Value == constructor).Select((p) => p.Key); var cachedCount = cached.Count(); if (cachedCount > 0) { return(cached); } var count = types.Count(); if (count > 0) { var name = func.Groups[1].Value; var pars = Regex.Matches(func.Groups[2].Value, @"([^,]+\(.+?\))|([^,]+)"); IRuntimeType CacheRuntimeType(IRuntimeType runtimeType) { if (!Cache.ContainsKey(runtimeType)) { Cache.Add(runtimeType, constructor); } return(runtimeType); } return(types.Where((p) => p.MatchParameters(name, args, pars)).Select(CacheRuntimeType)); } return(ArrayExtensions.Empty <IRuntimeType>()); }
/// <summary> /// Gets the type of the parameters. /// </summary> /// <param name="args">The arguments.</param> /// <returns></returns> public static IEnumerable <Type> GetTypes(object[] args) => args == null || args.Length == 0 ? ArrayExtensions.Empty <Type>() : args.Select(GetType);
/// <summary> /// Gets the full name of the parameters. /// </summary> /// <param name="args">The arguments.</param> /// <returns></returns> public static IEnumerable <string> GetNames(string[] args) => args == null || args.Length == 0 ? ArrayExtensions.Empty <string>() : args;
/// <summary> /// Creates the instance. /// </summary> /// <param name="type">The identifier.</param> /// <returns></returns> /// <exception cref="TypeInstantiationException"></exception> public object GetInstance(Type type) => GetInstance(ToString(type), ArrayExtensions.Empty <object>());
/// <summary> /// Creates the instance. /// </summary> /// <param name="typeFullName">The identifier.</param> /// <returns></returns> /// <exception cref="TypeInstantiationException"></exception> public object GetInstance(string typeFullName) => GetInstance(typeFullName, ArrayExtensions.Empty <object>());