加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – .NET告诉我TypeLoadException:违反了类型参数’T’的约束

发布时间:2020-12-15 18:35:58 所属栏目:百科 来源:网络整理
导读:我正在尝试初始化DataEditor Student的对象,其中我的DataEditor T class实现接口IDataEditor T其中T:IEditableObject. DataEditorStudent editor = GetEditorFor(student); 在运行时,我遇到了一个TypeLoadException: ‘Namespace.IDataEditor`1 [T]’上的G
我正在尝试初始化DataEditor< Student>的对象,其中我的DataEditor< T> class实现接口IDataEditor< T>其中T:IEditableObject.
DataEditor<Student> editor = GetEditorFor(student);

在运行时,我遇到了一个TypeLoadException:
‘Namespace.IDataEditor`1 [T]’上的GenericArguments [0],’Namespace.Data.Student’违反了类型参数’T’的约束.异常发生在上面的行上,甚至在它进入GetEditorFor方法之前.

对T的唯一约束是IEditableObject,我的Student类清楚地实现了它(我仔细检查了界面拼写,命名空间等),而且编译器也没有给我任何错误,所以我不知道为什么会出现这个错误在运行时.

如果我删除IEditableObject约束,代码运行没有此异常,但我的逻辑依赖于类是IEditableObject,所以它不是一个选项.

知道为什么会这样,以及如何解决它?

这些页面似乎是相关的,但我仍然不知道解决方案

> https://connect.microsoft.com/VisualStudio/feedback/details/270717/reflection-emit-chokes-on-method-type-parameters
> http://bytes.com/topic/c-sharp/answers/478595-reflection-generics-could-anyone-confirm-deny-bug

这是.NET中的错误吗?有没有人找到解决方法?

编辑:声明请求

public class DataEditor<T> : ViewModel,IDataEditor<T> where T :  IEditableObject

public interface IDataEditor<T> : IDataEditor 
    where T :  IEditableObject

解决方法

由于同样的错误,我的项目无法构建.即使我没有使用任何这种通用类型的类.它的组装刚刚引用.

当我删除它的访问者文件时,问题解决了.如果您不需要访问者,删除它可能是一个解决方案.

这个答案可能对某人有用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读