我在使用泛型时遇到问题。我正在创建一个名为IProblem
的接口(interface),其中每个问题都有结果(答案)和结果(如果正确的话)
public interface IProblem<T>
{
ushort ResultCount { get; }
T[] Results { get; }
bool IsCorrect();
}
public abstract class ProblemBase<T> : IProblem<T>
{
private T[] _results;
private ushort? _resultCount;
public ushort ResultCount
{
get
{
if (_resultCount == null) throw new ArgumentNullException("_resultCount");
return (ushort)_resultCount;
}
protected set
{
if (_resultCount != value)
_resultCount = value;
}
}
public T[] Results
{
get
{
if (_results == null)
_results = new T[ResultCount];
return _results;
}
}
public abstract bool IsCorrect();
}
这是我创建算术问题(称为
ProblemA
)的示例。 T
是decimal
,因为数组数据类型应为十进制(其他问题可能是string
或int
)public class ProblemA: ProblemBase<decimal>
{
private decimal _number1;
private decimal _number2;
private Operators _operator;
public decimal Number1
{
get { return _number1; }
set { _number1 = value; }
}
public decimal Number2
{
get { return _number2; }
set { _number2 = value; }
}
public Operators Operator
{
get { return _operator; }
set { _operator = value; }
}
public decimal Result
{
get { return Results[0]; }
set { Results[0] = value; }
}
public ProblemA()
{
this.ResultCount = 1;
}
public override bool IsCorrect()
{
bool result;
switch (_operator)
{
case Operators.Addition:
result = this.Result == (this.Number1 + this.Number2);
break;
case Operators.Subtract:
result = this.Result == (this.Number1 - this.Number2);
break;
case Operators.Multiplication:
result = this.Result == (this.Number1 * this.Number2);
break;
case Operators.Division:
result = this.Result == (this.Number1 / this.Number2);
break;
default:
throw new ArgumentException("_operator");
}
return result;
}
}
我正在使用MVVM,所以我想为每个包含
ProblemBase<T>
作为属性的问题创建一个ViewModel,但是它是如何泛型的,我想如果将IProblemViewModel
放置为泛型将是一个问题。public interface IProblemViewModel : IViewModel
{
ProblemBase<T> Problem { get; set; }
}
我之所以这样说,是因为以后有一个使用
ObservableCollection<IProblemViewModel>
的计划,所以我不确定如果我写IProblemViewModel
或IProblemViewModel<T>
是否没有问题。提前致谢。
最佳答案
也许我还不太了解,但这是你的追求吗?
ObservableCollection<IProblemViewModel<object>> collection = new ObservableCollection<IProblemViewModel<object>>
{
new ProblemViewModel<DerivedResult>(),
new ProblemViewModel<OtherResult>()
};
这可以通过将通用参数声明为协变来实现。
您也可以将集合更改为
ObservableCollection<IProblem<BaseType>>
并让它接受特定的结果链。在此示例中,DerivedResult和OtherResult必须随后从BaseType继承以适合集合。
最大的警告是,原始类型无论如何都不适合该层次结构。您将不得不将它们包装在
IProblem<IntResult>
中,依此类推。当然,您可以实现一个简单的载体,例如Boxer,它将对任何值类型进行装箱而不是为每种类型实现一个值。
最后一个警告:协变类型不可能具有'set'属性,因此
IProblemViewModel
仅支持get
。一个完整的可编译示例:
class Program
{
public interface IProblem<out T>
{
ushort ResultCount { get; }
T[] Results { get; }
bool IsCorrect();
}
public class ProblemBase<T> : IProblem<T>
{
private T[] _results;
private ushort? _resultCount;
public ushort ResultCount
{
get
{
if (_resultCount == null) throw new ArgumentNullException("_resultCount");
return (ushort)_resultCount;
}
protected set
{
if (_resultCount != value)
_resultCount = value;
}
}
public T[] Results
{
get
{
if (_results == null)
_results = new T[ResultCount];
return _results;
}
}
public bool IsCorrect()
{
return true;
}
}
public interface IProblemViewModel<out T>
{
IProblem<T> Problem { get; }
}
public class BaseResult
{
}
public class DerivedResult : BaseResult
{
}
public class OtherResult : BaseResult
{
}
public class ProblemViewModel<T> : IProblemViewModel<T>
{
public IProblem<T> Problem
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
}
static void Main(string[] args)
{
ObservableCollection<IProblemViewModel<object>> collection = new ObservableCollection<IProblemViewModel<object>>
{
new ProblemViewModel<DerivedResult>(),
new ProblemViewModel<OtherResult>()
//, new ProblemViewModel<int>() // This is not possible, does not compile.
};
}
}
关于c# - 如何解决通用类<T>的情况?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8843832/