在字典中持有 类
Holding classes in Dictionary
我有一个关于在 dictionary.So 中举办 class 的问题 我正在做一个关于 university.There 的项目 不止一个教员 names.When 用户输入一个教员名称,我正在使用 context.call
将用户引导至适当的教师 class
所以在这里,如果用户输入 show me computer engineering,用户将转到 ShowComp class。
但使用 if-else 使代码真正 unreadable.I 认为我可以将这些关键字放入字典
但是这次 context.Call 给出了一个关于值 type.What 的错误,我应该把字典值 type.I 搞不懂 out.Can 有人帮我吗?
这样做:
static Dictionary<String, Type> FACULTY_CLASS_MAP;
/**
* faculty class mapping.
*/
FACULTY_CLASS_MAP= new Dictionary<String, Type>
{
{ "Text", typeof(FacultyClass) }
}
由于对话框继承自 IDialog<object>
,您可以将其放入字典中:
private readonly Dictionary<string, IDialog<object>> options
= new Dictionary<string, IDialog<object>>
{ { "computer", new ShowComp() }, { "law", new ShowLaw() } };
public async Task GetFacilities(IDialogContext context, LuisResult result)
{
var entity = result.Entities.FirstOrDefault(e => options.ContainsKey(e.Entity));
if (entity != null)
{
IDialog<object> dialog = null;
if (options.TryGetValue(entity.Entity, out dialog))
{
context.Call(dialog, this.AfterResume);
}
}
}
我有一个关于在 dictionary.So 中举办 class 的问题 我正在做一个关于 university.There 的项目 不止一个教员 names.When 用户输入一个教员名称,我正在使用 context.call
将用户引导至适当的教师 class所以在这里,如果用户输入 show me computer engineering,用户将转到 ShowComp class。
但使用 if-else 使代码真正 unreadable.I 认为我可以将这些关键字放入字典
但是这次 context.Call 给出了一个关于值 type.What 的错误,我应该把字典值 type.I 搞不懂 out.Can 有人帮我吗?
这样做:
static Dictionary<String, Type> FACULTY_CLASS_MAP;
/**
* faculty class mapping.
*/
FACULTY_CLASS_MAP= new Dictionary<String, Type>
{
{ "Text", typeof(FacultyClass) }
}
由于对话框继承自 IDialog<object>
,您可以将其放入字典中:
private readonly Dictionary<string, IDialog<object>> options
= new Dictionary<string, IDialog<object>>
{ { "computer", new ShowComp() }, { "law", new ShowLaw() } };
public async Task GetFacilities(IDialogContext context, LuisResult result)
{
var entity = result.Entities.FirstOrDefault(e => options.ContainsKey(e.Entity));
if (entity != null)
{
IDialog<object> dialog = null;
if (options.TryGetValue(entity.Entity, out dialog))
{
context.Call(dialog, this.AfterResume);
}
}
}