Minor fixes around OnModuleBuilding (#1116)

* Don't attempt instantiation of an abstract module
* Attempt associating a TypeReader in case one is registered late (ie. OnModuleBuilding)
This commit is contained in:
Joe4evr
2018-08-01 16:10:21 +02:00
committed by Christopher F
parent afc3a9d063
commit 5dad0fa1a1
2 changed files with 14 additions and 12 deletions

View File

@@ -120,7 +120,7 @@ namespace Discord.Commands.Builders
if (Name == null) if (Name == null)
Name = _aliases[0]; Name = _aliases[0];
if (TypeInfo != null) if (TypeInfo != null && !TypeInfo.IsAbstract)
{ {
var moduleInstance = ReflectionUtils.CreateObject<IModuleBase>(TypeInfo, service, services); var moduleInstance = ReflectionUtils.CreateObject<IModuleBase>(TypeInfo, service, services);
moduleInstance.OnModuleBuilding(service, this); moduleInstance.OnModuleBuilding(service, this);

View File

@@ -45,14 +45,7 @@ namespace Discord.Commands.Builders
internal void SetType(Type type) internal void SetType(Type type)
{ {
var readers = Command.Module.Service.GetTypeReaders(type); TypeReader = GetReader(type);
if (readers != null)
TypeReader = readers.FirstOrDefault().Value;
else
TypeReader = Command.Module.Service.GetDefaultTypeReader(type);
if (TypeReader == null)
throw new InvalidOperationException($"{type} does not have a TypeReader registered for it. Parameter: {Name} in {Command.PrimaryAlias}");
if (type.GetTypeInfo().IsValueType) if (type.GetTypeInfo().IsValueType)
DefaultValue = Activator.CreateInstance(type); DefaultValue = Activator.CreateInstance(type);
@@ -61,6 +54,15 @@ namespace Discord.Commands.Builders
ParameterType = type; ParameterType = type;
} }
private TypeReader GetReader(Type type)
{
var readers = Command.Module.Service.GetTypeReaders(type);
if (readers != null)
return readers.FirstOrDefault().Value;
else
return Command.Module.Service.GetDefaultTypeReader(type);
}
public ParameterBuilder WithSummary(string summary) public ParameterBuilder WithSummary(string summary)
{ {
Summary = summary; Summary = summary;
@@ -100,7 +102,7 @@ namespace Discord.Commands.Builders
internal ParameterInfo Build(CommandInfo info) internal ParameterInfo Build(CommandInfo info)
{ {
if (TypeReader == null) if ((TypeReader ?? (TypeReader = GetReader(ParameterType))) == null)
throw new InvalidOperationException($"No type reader found for type {ParameterType.Name}, one must be specified"); throw new InvalidOperationException($"No type reader found for type {ParameterType.Name}, one must be specified");
return new ParameterInfo(this, info, Command.Module.Service); return new ParameterInfo(this, info, Command.Module.Service);