【问题标题】:Holding classes in Dictionary在字典中上课
【发布时间】:2018-10-20 14:41:33
【问题描述】:

我有一个关于在字典中开课的问题。所以我正在做一个关于大学的项目。有多个教职员工姓名。当用户输入教职员工姓名时,我正在使用context.call

所以在这里,如果用户输入 show me computer engineering,用户会被定向到 ShowComp 类。

但是使用 if-else 让代码真的不可读。我以为我可以将这些关键字放入字典中

但这一次 context.Call 给出了一个关于值类型的错误。我应该把字典值类型放在什么地方。我想不通。谁能帮帮我吗?

【问题讨论】:

    标签: botframework bots chatbot azure-language-understanding


    【解决方案1】:

    这样做:

            static Dictionary<String, Type> FACULTY_CLASS_MAP;
    
    
            /**
             * faculty class mapping.
            */
            FACULTY_CLASS_MAP= new Dictionary<String, Type>
            {
                { "Text", typeof(FacultyClass) }
            }
    

    【讨论】:

    • 不使用object map,如果使用class,应该做IDialog option =(IDialog)Activator.CreateInstance(options[option_from_query], new object[] { }), Context.Call(选项,之后...)。或者 Context.Call(options[option_from_query], after...)。是编程错误
    【解决方案2】:

    由于对话框继承自IDialog&lt;object&gt;,您可以将其放入字典中:

    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);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多