NHibernate例外:方法添加应为“公共/受保护的虚拟"或“受保护的内部虚拟" [英] NHibernate exception: method Add should be 'public/protected virtual' or 'protected internal virtual'
本文介绍了NHibernate例外:方法添加应为“公共/受保护的虚拟"或“受保护的内部虚拟"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以此类为例:
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?
这很令人困惑!
推荐答案
方法也必须是虚拟的,因为它们可以访问字段.考虑这种情况:
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)
// ...
}
}
这篇关于NHibernate例外:方法添加应为“公共/受保护的虚拟"或“受保护的内部虚拟"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文