1

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>)?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You are right on both. First, regarding the issue, I made a bubu on GetType. Now I use Func<IDialog<object>> and I call it like this: new LoginDialog(() => new TheDialog()) Second, you are absolutely right and I never thought of this. I will return from login dialog the result and decide next dialog outside.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.