Add new overload for AddTypeReader (#1009)
This commit is contained in:
@@ -291,7 +291,7 @@ namespace Discord.Commands
|
||||
|
||||
//We dont have a cached type reader, create one
|
||||
reader = ReflectionUtils.CreateObject<TypeReader>(typeReaderType.GetTypeInfo(), service, services);
|
||||
service.AddTypeReader(paramType, reader);
|
||||
service.AddTypeReader(paramType, reader, false);
|
||||
|
||||
return reader;
|
||||
}
|
||||
|
||||
@@ -215,10 +215,11 @@ namespace Discord.Commands
|
||||
return true;
|
||||
}
|
||||
|
||||
//Type Readers
|
||||
//Type Readers
|
||||
/// <summary>
|
||||
/// Adds a custom <see cref="TypeReader"/> to this <see cref="CommandService"/> for the supplied object type.
|
||||
/// If <typeparamref name="T"/> is a <see cref="ValueType"/>, a <see cref="NullableTypeReader{T}"/> will also be added.
|
||||
/// If a default <see cref="TypeReader"/> exists for <typeparamref name="T"/>, a warning will be logged and the default <see cref="TypeReader"/> will be replaced.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The object type to be read by the <see cref="TypeReader"/>.</typeparam>
|
||||
/// <param name="reader">An instance of the <see cref="TypeReader"/> to be added.</param>
|
||||
@@ -226,17 +227,53 @@ namespace Discord.Commands
|
||||
=> AddTypeReader(typeof(T), 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 <see cref="NullableTypeReader{T}"/> for the value type will also be added.
|
||||
/// 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)
|
||||
{
|
||||
var readers = _typeReaders.GetOrAdd(type, x => new ConcurrentDictionary<Type, TypeReader>());
|
||||
readers[reader.GetType()] = reader;
|
||||
if (_defaultTypeReaders.ContainsKey(type))
|
||||
_cmdLogger.WarningAsync($"The default TypeReader for {type.FullName} was replaced by {reader.GetType().FullName}. To suppress this message, use the overload that has a Boolean to replace the default one and pass true.").GetAwaiter().GetResult();
|
||||
AddTypeReader(type, reader, true);
|
||||
}
|
||||
/// <summary>
|
||||
/// Adds a custom <see cref="TypeReader"/> to this <see cref="CommandService"/> for the supplied object type.
|
||||
/// If <typeparamref name="T"/> is a <see cref="ValueType"/>, a <see cref="NullableTypeReader{T}"/> will also be added.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The object type to be read by the <see cref="TypeReader"/>.</typeparam>
|
||||
/// <param name="reader">An instance of the <see cref="TypeReader"/> to be added.</param>
|
||||
/// <param name="replaceDefaultTypeReader">If <paramref name="reader"/> should replace the default <see cref="TypeReader"/> for <typeparamref name="T"/> if one exists.</param>
|
||||
public void AddTypeReader<T>(TypeReader reader, bool replaceDefaultTypeReader)
|
||||
=> AddTypeReader(typeof(T), reader, replaceDefaultTypeReader);
|
||||
/// <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="replaceDefaultTypeReader">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 replaceDefaultTypeReader)
|
||||
{
|
||||
if (replaceDefaultTypeReader && _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);
|
||||
if (type.GetTypeInfo().IsValueType)
|
||||
AddNullableTypeReader(type, reader);
|
||||
}
|
||||
}
|
||||
internal void AddNullableTypeReader(Type valueType, TypeReader valueTypeReader)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user