Let's say, for example, that we have a LoginDialog
(reusable dialog) that is called at different times to access specific areas/dialogs of your bot.
Being a reusable dialog, you don't want to write explicit call of child dialog.
public class LoginDialog : IDialog<object> {
public async Task StartAsync(IDialogContext context) {
var message = "Insert password";
PromptDialog.Text(context, AfterPassword,
message,
null,
1);
}
public async Task AfterPassword(IDialogContext context, IAwaitable<string> result) {
var password = await result;
var valid = await Mocks.ValidatePasswordMockAsync(password);
if (valid) {
context.Call(new TheDialog(), ResumeAfter);
} else context.Call(new TransferDialog(), ResumeAfter);
}
private async Task ResumeAfter(IDialogContext context, IAwaitable<object> result) {
context.Done<object>(null);
}}
Maybe you have more than one dialogs that need login validation.
What you want to do is to use reflection in order to reuse that dialog?
I tried something like this:
public class LoginDialog : IDialog<object> {
private string classToCall;
public LoginDialog(string classToCall) {
this.classToCall = classToCall;
}
public async Task StartAsync(IDialogContext context) {
var message = "Insert password";
PromptDialog.Text(context, AfterPassword, message, null, 1);
}
public async Task AfterPassword(IDialogContext context, IAwaitable<string> result) {
var password = await result;
var valid = await Mocks.ValidatePasswordMockAsync(password);
if (valid) {
var type = Type.GetType(classToCall);
context.Call(Activator.CreateInstance(type), ResumeAfter);
}
else
context.Call(new TransferDialog(), ResumeAfter);
}
private async Task ResumeAfter(IDialogContext context, IAwaitable<object> result) {
context.Done<object>(null);
}
}
I get an error on this line:
context.Call(Activator.CreateInstance(type), ResumeAfter);
Type arguments cannot be inferred from the usage. Try specifying the type arguments explicitly.
What do you recommend? A forced cast (as IDialog<object>
)?