/// <summary> /// Adds a custom <see cref="TypeReader" /> to this <see cref="CommandService" /> for the supplied object /// type. /// If <paramref name="type" /> is a <see cref="ValueType" />, a nullable <see cref="TypeReader" /> for the /// value type will also be added. /// </summary> /// <param name="type">A <see cref="Type" /> instance for the type to be read.</param> /// <param name="reader">An instance of the <see cref="TypeReader" /> to be added.</param> /// <param name="replaceDefault"> /// Defines whether the <see cref="TypeReader"/> should replace the default one for <see cref="Type" /> if /// it exists. /// </param> public void AddTypeReader(Type type, TypeReader reader, bool replaceDefault) { if (replaceDefault && HasDefaultTypeReader(type)) { _defaultTypeReaders.AddOrUpdate(type, reader, (k, v) => reader); if (type.GetTypeInfo().IsValueType) { var nullableType = typeof(Nullable <>).MakeGenericType(type); var nullableReader = NullableTypeReader.Create(type, reader); _defaultTypeReaders.AddOrUpdate(nullableType, nullableReader, (k, v) => nullableReader); } } else { var readers = _typeReaders.GetOrAdd(type, x => new ConcurrentDictionary <Type, TypeReader>()); readers[reader.GetType()] = reader; if (type.GetTypeInfo().IsValueType) { AddNullableTypeReader(type, reader); } } }
/// <summary> /// Adds a custom <see cref="TypeReader" /> to this <see cref="CommandService" /> for the supplied object /// type. /// If <paramref name="type" /> is a <see cref="ValueType" />, a nullable <see cref="TypeReader" /> for the /// value type will also be added. /// If a default <see cref="TypeReader" /> exists for <paramref name="type" />, a warning will be logged and /// the default <see cref="TypeReader" /> will be replaced. /// </summary> /// <param name="type">A <see cref="Type" /> instance for the type to be read.</param> /// <param name="reader">An instance of the <see cref="TypeReader" /> to be added.</param> public void AddTypeReader(Type type, TypeReader reader) { if (_defaultTypeReaders.ContainsKey(type)) { _ = _cmdLogger.WarningAsync($"The default TypeReader for {type.FullName} was replaced by {reader.GetType().FullName}." + "To suppress this message, use AddTypeReader<T>(reader, true)."); } AddTypeReader(type, reader, true); }