Reputation: 210
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>
)?
Upvotes: 1
Views: 310
Reputation: 14787
Instead of delegating the responsibility of calling the next dialog to the LoginDialog
, wouldn't be better to make the LoginDialog
to return if it's valid or not, and have the caller decide who to call next?
Regarding your issue, first, check if the Type.GetType
is returning what you expected and then yes, try casting to see if the issue is there or if there is something else going on.
Upvotes: 2