/// <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 <see cref="NullableTypeReader{T}"/> 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)) { Log($"The default TypeReader for {type.FullName} was replaced by {reader.GetType().FullName}." + $"To suppress this message, use AddTypeReader<T>(reader, true).", LoggingTarget.Runtime, LogLevel.Error); } AddTypeReader(type, reader, true); }
/// <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 <see cref="NullableTypeReader{T}"/> 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">If <paramref name="reader"/> should replace the default <see cref="TypeReader"/> for <paramref name="type"/> if one exists.</param> public void AddTypeReader(Type type, TypeReader reader, bool replaceDefault) { if (replaceDefault && _defaultTypeReaders.ContainsKey(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); } } }