我正在尝试初始化DataEditor<Student>我的DataEditor<T>类实现的对象interface IDataEditor<T> where T : IEditableObject.
DataEditor<Student> editor = GetEditorFor(student);
Run Code Online (Sandbox Code Playgroud)
在运行时,我得到一个TypeLoadException,说: 'Namespace.IDataEditor`1 [T]'上的GenericArguments [0],'Namespace.Data.Student'违反了类型参数'T'的约束.异常发生在上面的行上,甚至在它进入GetEditorFor方法之前.
对T的唯一约束是IEditableObject,我的Student类清楚地实现它(我仔细检查了界面拼写,命名空间等),并且编译器也没有给我任何错误,所以我不知道为什么在运行时发生这个错误.
如果我删除IEditableObject约束,代码运行没有此异常,但我的逻辑依赖于类是一个IEditableObject,所以它不是一个选项.
知道为什么会这样,以及如何解决它?
这些页面似乎是相关的,但我仍然不知道解决方案
这是.NET中的错误吗?有没有人找到解决方法?
编辑:声明请求
public class DataEditor<T> : ViewModel, IDataEditor<T> where T : IEditableObject
public interface IDataEditor<T> : IDataEditor
where T : IEditableObject
Run Code Online (Sandbox Code Playgroud)
小智 -1
发生的情况是Student没有实现IEditableObject,该GetEditorFor方法可能有一个where子句 where Tmust be IEditableObject,而T您传递的 isStudent和 not IDataEditor。
这就是您收到此错误的原因,您违反了发送T不是 IEditableObject 的方法签名。
您应该IEditableObject在Student类中实现,或者删除该where T : IEditableObject子句。
| 归档时间: |
|
| 查看次数: |
5007 次 |
| 最近记录: |