CreateModuleAsync in InteractionService cannot be used to create a buildable Module.
See original GitHub issueThe Problem:
CreateModuleAsync offers a BuilderAction, however, you cannot set the TypeInfo of the Module you want to build in the passed ModuleBuilder object. This results in a NullReferenceException
when the builder is being built.
interactionService.CreateModuleAsync("name", serviceProvider, builder =>
{
builder.TypeInfo = typeof(MyModule); // Not possible as the 'TypeInfo' property is marked internal.
});
The main problem is that the TypeInfo property is marked internal and there’s no way to provide a TypeInfo to the ModuleBuilder object externally.
Possible ways to fix this:
1. Add a Setter-Method to ModuleBuilder.
public ModuleBuilder WithTypeInfo(TypeInfo typeInfo)
{
TypeInfo = typeInfo;
return this;
}
Possible Problems:
The TypeInfo may be invalid. As validity checks are normally done in the ModuleClassBuilder type, a duplicate check or a reference to ModuleClassBuilder would have to be added.
2. Pass the TypeInfo from the CreateModuleAsync method and instantiate the ModuleBuilder via the ModuleClassBuilder.
In InteractionService:
public async Task<ModuleInfo> CreateModuleAsync(string name, TypeInfo typeInfo, IServiceProvider services,
Action<ModuleBuilder> buildFunc)
{
// ...
try
{
var moduleInfo = ModuleClassBuilder.CreateModuleAsync(name, typeInfo, buildFunc, this, services);
LoadModuleInternal(moduleInfo);
return moduleInfo;
}
// ...
}
In ModuleClassBuilder:
public static ModuleInfo CreateModuleAsync(string name, TypeInfo typeInfo, Action<ModuleBuilder> buildFunc,
InteractionService interactionService, IServiceProvider services)
{
if (!IsValidModuleDefinition(typeInfo))
throw new InvalidOperationException();
var builder = new ModuleBuilder(interactionService);
builder.TypeInfo = typeInfo; // Set TypeInfo here after it has been checked.
buildFunc(builder);
var moduleInfo = builder.Build(interactionService, services);
return moduleInfo;
}
Possible Problems:
This would most likely be a breaking change as the signature of CreateModuleAsync would need to be changed to accommodate the TypeInfo parameter. But seeing as the method can’t be used anyway, this might be the cleanest way to handle this.
Issue Analytics
- State:
- Created 2 years ago
- Comments:9
Top GitHub Comments
Probably going to introduce a new
Construct
method for performing this task.CreateModuleAsync
method isn’t meant to be used with reflection so setting TypeInfo on its own wouldnt accomplish much aside from solving the NRE. Instead, we can just add a null check and method validation before executing Module.OnModuleBuilding.Great issue btw.