0

我有 2 个视图模型:

1)

public class TaskTrayViewModel<T> : ViewModelBase where T : IBlotterRow, new()
{
}

2)

public class BlotterCriteriaViewModel : ViewModelBase , IDataErrorInfo 
{ 
}

我正在尝试TaskTrayViewModel<T>像这样访问 BlotterCriteriaViewModel 中的公共属性

public class BlotterCriteriaViewModel : ViewModelBase , IDataErrorInfo 
{
TaskTrayViewModel<IBlotterRow> _all;
    TaskTrayViewModel<IBlotterRow> All
    {
        get { return _all; }
        set { value = _all; }
    }
}

执行上述操作时,出现以下错误:“DMS.Common.Interfaces.Blotter.IBlotterRow”必须是具有公共无参数构造函数的非抽象类型,才能将其用作泛型类型或方法中的参数“T” DMS.GUI.ViewModels.TaskTrayViewModel'。

请建议?如何纠正它?

4

3 回答 3

0

我不明白,你到底想做什么,但错误说(根据你的约束)要使用TaskTrayViewModel<T>泛型类型,你需要非抽象类型和公共无参数构造函数作为类型参数:

public class MyBlotterRow : IBlotterRow { ... }

然后你可以把你的财产声明写成TaskTrayViewModel<MyBlotterRow> _all;

于 2012-11-19T15:30:39.763 回答
0

去掉new()泛型约束,因为你不能用new创建接口,实际上你不能创建任何接口。请参阅有关约束的内容。您可能想使用class

于 2012-11-19T12:49:21.867 回答
0

你不能有这样定义的属性:

TaskTrayViewModel<IBlotterRow> _all;
TaskTrayViewModel<IBlotterRow> All
{
    get { return _all; }
    set { value = _all; }
}

因为您没有指定 IBlotterRow 的具体实现。如果您有TaskTrayViewModel<T>该类的具体实现,例如

public class MyImpl: TaskTrayViewModel<MyClass>

那么您可能会将其作为另一个类的属性。否则,您必须定义一个非泛型基类来TaskTrayViewModel<T>包含您需要的属性,或者使BlotterCriteriaViewModel泛型并使用其类型参数定义属性:

public class BlotterCriteriaViewModel<T> : ViewModelBase , IDataErrorInfo 
{
    TaskTrayViewModel<T> _all;
    TaskTrayViewModel<T> All
    {
       get { return _all; }
       set { value = _all; }
    }
}
于 2012-11-19T15:26:11.290 回答