接收用户输入后,PromptChoice更改选项

时间:2017-08-31 21:54:36

标签: c# botframework

我已经覆盖了一个抽象类型的PromptDialog.PromptChoice。我有两个不同的孩子,我想分享相同的逻辑,但我需要保证某些变量可用于我的部分TryParse逻辑。对于其中一个孩子,我在这里没有任何问题。然而,对于另一个孩子,我遇到了非常奇怪的行为。当我从我的对话框CustomPromptChoice.Choice(上下文,函数,选项)调用时,我看到选项是我期待的。向用户显示正确的选项列表,但在选择一个选项后,在TryParse逻辑中,我看到所有选项都已更改。显然,我的TryParse总会失败。有没有人见过这种行为?

这是基类:

[Serializable]
public abstract class ExtractionClass
{
    public List<ExtractionClass> children;
    public ExtractionClass parent;
    public string name;
    public string value;
    public override string ToString()
    {
        return name;
    }
    public ExtractionClass() { }
    public ExtractionClass(string name, string value, ExtractionClass parent)
    {
        this.name = name;
        this.value = value;
        this.parent = parent;
    }
    //upon initialization, guarantee that children is filled correctly
    public abstract void SetChildren();
    //use to check if child is a where match
    public abstract bool IsWhereMatch(ExtractionClass child, string query="");
    //use to check if child is a when match
    public abstract bool IsWhenMatch(ExtractionClass child, string query="");
    //use to check if child is a who match
    public abstract bool IsWhoMatch(ExtractionClass child, string query="");
    //use to check if child is a what match
    public abstract bool IsWhatMatch(ExtractionClass child, string query="");
}

以下是不起作用的子类的精简版本:

[Serializable]
public class ChildClass1:ExtractionClass
{
    public Dictionary<ChildClass1, int> childrenLookup;
    public Dictionary<string, ChildClass1> childrenNames;
    private dynamic content;
    public string spokenName;
    static HashSet<string> childrenToKeep = new HashSet<string>()
    static List<string> ignoreNames = new List<string> ();
    public static Dictionary<string, List<string>> nodeNameSynonyms = new Dictionary<string, List<string>>();
    public static HashSet<string> WhoNodes = new HashSet<string>();
    public ChildClass1(dynamic content)
    {
        this.children = new List<ExtractionClass>();
        this.childrenLookup = new Dictionary<ChildClass1, int>();
        this.childrenNames = new Dictionary<string, ChildClass1>();
        this.parent = null;
        this.name = String.Empty;
        this.value = String.Empty;
        this.spokenName = String.Empty;
        this.content = content;
    }
    public ChildClass1(string name, string value, List<string> synonyms) : this(null)
    {
        this.name = name;
        this.spokenName = name;
        double doubleValue;
        int integerValue;
        bool isInt = Int32.TryParse(value, out integerValue);
        if (isInt)
        {
            this.value = string.Format("{0}", integerValue);
        }
        else {
            bool isDouble = Double.TryParse(value, out doubleValue);
            if (isDouble)
            {
                this.value = string.Format("{0:N2}", doubleValue);
            }
            else
            {
                this.value = value;
            }
        }

    }
    public override string ToString()
    {
        return this.name;
    }

    public override void SetChildren()
    {
        //pretty long and complicated
    }

    public override bool IsWhereMatch(ExtractionClass child, string query = "")
    {
        return false;
    }

    public override bool IsWhenMatch(ExtractionClass child, string query = "")
    {
        return false;
    }

    public override bool IsWhoMatch(ExtractionClass child, string query = "")
    {
        return false;
    }

    public override bool IsWhatMatch(ExtractionClass child, string query = "")
    {
        return false;
    }
}

最后,这是一个有效的类的精简版本:

public class ChildClass2: ExtractionClass
{
    public Address address;
    public Rating rating;
    public string description, telephone, spokenName;


    public ChildClass2(string name, string value, ExtractionClass parent) : base(name, value, parent) { children = new List<ExtractionClass>(); }
    public override string ToString()
    {
        return name;
    }


    public override void SetChildren()
    {
        //a bit of a mess but works as expected
    }


}

对我来说没有意义的是,我在对话中打了这个电话:

ExtractionPromptChoice.Choice(context, this.CarouselEntityHandler, nodeOptions);

然后当我逐步完成堆栈时,我看到它返回给用户正确的选项。我的控件的最后一个实例中的nodeOptions设置正确,但是当我转到ExtractionPromptChoice TryParse逻辑时,选项是不同的。 nodeOptions最终保存ChildClass1类型的列表,但是为ExtractionClass定义了promptchoice。

编辑:事实证明我的代码有些东西导致远程服务器在我的MessagesController代码中返回400 Bad Request错误。

1 个答案:

答案 0 :(得分:0)

原来我在我的代码中的其他地方有错误的逻辑,它捕获错误而不传播它们。我收到了400 Bad Request响应,因为在ChildClass1中我有一个动态,它打破了序列化。这个错误并没有被抛到任何地方,我不确定为什么我看到了错误的PromptOption选择,但我不认为当我抓住并且不做时,行为是明确定义的重新抛出错误。

相关问题