在我的后端,我有一个界面,它定义了两种类型:
public class FilterRange
{
public double? Min { get; set; }
public double? Max { get; set; }
}
public class FilterRange<T> where T : struct
{
public T? Min { get; set; }
public T? Max { get; set; }
}
第一堂课是遗产班。第二个是我刚刚定义的新类,因此我可以使用除double之外的其他类型(在这个实例中为int)。
在前端,我有遗留类的视图模型:
public class FilterRangeBaseViewModel<T> : ViewModelBase<T>, IDataErrorInfo
where T: FilterRange, new()
{
public FilterRangeBaseViewModel(T model)
: this(model, "F2")
{
}
public FilterRangeBaseViewModel(T model, string formatString)
: base(model)
{
this.FormatString = formatString;
CreateErrorDictionary();
SetTextValues();
}
// More code below
}
public class FilterRangeViewModel : FilterRangeBaseViewModel<FilterRange>
{
public FilterRangeViewModel(FilterRange model)
: base(model)
{
}
public FilterRangeViewModel(FilterRange model, string formatString)
: base(model, formatString)
{
}
}
我努力定义FilterRangeViewModelBase
,以便我可以使用新的通用FilterRange
课程。
我想像
public class FilterRangeBaseViewModel<T> : ViewModelBase<T>, IDataErrorInfo
where T: FilterRange<U> where U : struct, new()
可能有用,但它没有。
我的视图模型的正确定义是什么?
答案 0 :(得分:5)
您需要在类类型参数中定义T
和U
。
public class FilterRangeBaseViewModel<T, U> : ViewModelBase<T>, IDataErrorInfo
where T: FilterRange<U>, new() where U : struct
根据评论编辑。