为什么NHibernate可以为具有私有setter属性的类创建代理,但不能为具有内部setter属性的类创建代理?或者我错过了一些应该完全明显的东西?
public class PrivateSetter {
// no proxy error
public virtual string PrivateSetterProperty { get; private set; }
}
public class InternalSetter {
// proxy error
public virtual string InternalSetterProperty { get; internal set; }
}
答案 0 :(得分:3)
您需要将setter标记为protected internal
,以便代理类可以访问它:
public class InternalSetter
{
public virtual string InternalSetterProperty { get; protected internal set; }
}
答案 1 :(得分:2)
这是一个纯粹的.NET语言问题。亲自尝试一下:
public class A
{
public virtual string PrivateSetter { get; private set; }
public virtual string InternalSetter { get; internal set; }
}
在另一个集会中:
public class B : A
{
// works fine, private isn't derived at all
// you can omit the setter, make it public, internal to this
// assembly etc.
public override string PrivateSetter { get; set; }
// compilation time error: setter can't be overridden,
// there is no access to it.
public override string InternalSetter { get; internal set; }
}
顺便说一句,我只是在分析私有设置器和代理的问题,所以我不确定代理在这种情况下是否真的有效。