NHibernate异常:方法Add应该是'public / protected virtual'或'protected internal virtual'(N

编程入门 行业动态 更新时间:2024-10-20 20:51:26
NHibernate异常:方法Add应该是'public / protected virtual'或'protected internal virtual'(NHibernate exception: method Add should be 'public/protected virtual' or 'protected internal virtual')

以此课程为例:

public class Category : PersistentObject<int> { public virtual string Title { get; set; } public virtual string Alias { get; set; } public virtual Category ParentCategory { get; set; } public virtual ISet<Category> ChildCategories { get; set; } public /*virtual*/ void Add(Category child) { if (child != null) { child.ParentCategory = this; ChildCategories.Add(child); } } }

当运行没有添加方法的虚拟关键字的应用程序时,出现此错误:

method Add should be 'public/protected virtual' or 'protected internal virtual'

我明白为什么属性需要声明为虚拟的,因为它需要被延迟加载功能覆盖。

但我不明白为什么方法需要被声明为虚拟的...他们需要被推翻为什么?

这很混乱!

Take this class as example:

public class Category : PersistentObject<int> { public virtual string Title { get; set; } public virtual string Alias { get; set; } public virtual Category ParentCategory { get; set; } public virtual ISet<Category> ChildCategories { get; set; } public /*virtual*/ void Add(Category child) { if (child != null) { child.ParentCategory = this; ChildCategories.Add(child); } } }

When running the application without the virtual keyword of add method, I getting this error:

method Add should be 'public/protected virtual' or 'protected internal virtual'

I understand why properties need to declare as virtual, because thay need to be overridden by the lazy loading feature.

But I don't understand why Methods need to be declare as virtual... they need to be overridden for what reason?

This very confusing!

最满意答案

方法也需要是虚拟的,因为它们可以访问字段。 考虑这种情况:

class Entity { //... private int a; public virtual int A { get { return a; } } public virtual void Foo() { // lazy loading is implemented here by the proxy // to make the value of a available if (a > 7) // ... } }

Methods as well need to be virtual because they could access fields. Consider this situation:

class Entity { //... private int a; public virtual int A { get { return a; } } public virtual void Foo() { // lazy loading is implemented here by the proxy // to make the value of a available if (a > 7) // ... } }

更多推荐

本文发布于:2023-04-28 04:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1330300.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应该是   异常   方法   NHibernate   Add

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!