アンケートのフレームワークを作成しています。
アンケートにはいくつかの質問があります。Question
私の状況では、あなたが望む答えをサポートする、というクラスを探しています。
つまり、いくつかの質問は1つの答えだけを必要とし、他の2つは、文字列、int、double、または開発者が作成した新しい構造体を必要とします(たとえば、開発者が分数構造体を答えとして使用する数学の問題を作成していると想像してください)。
言い換えれば、私はあらゆるデータ型または回答の量をサポートする必要があります。
そこで、応答のQuestion
を含む抽象クラスを作成することを考えていました。Dictionary
public abstract class Question
{
protected Question(string questionText)
{
this.QuestionText = questionText;
this.Responses = new Dictionary<string, object>();
}
public string QuestionText
{
get;
set;
}
public IDictionary<string, object> Responses { get; protected set; }
}
たとえば、新しいを作成するQuestion
と、これがデモになります。
public sealed class Question1 : Question
{
public Question1(string questionText)
: base(questionText)
{
}
public int? Response1
{
get
{
int? value = null;
if (this.Responses.ContainsKey("Response1"))
value = this.Responses["Response1"] as int?;
return value;
}
set
{
this.Responses["Response1"] = value;
}
}
}
このアイデアについてどう思いますか?私の最初の疑問:私が別の独立したクラスではなく、クラスに応答を含めたのは正しいですか。