开发者

NHibernate proxy generation for internal setters vs private setters

开发者 https://www.devze.com 2022-12-17 17:29 出处:网络
Why can NHibernate create a proxy for classes with properties that have private setters but not for classes with p开发者_开发知识库roperties that have internal setters?Or am I missing something that s

Why can NHibernate create a proxy for classes with properties that have private setters but not for classes with p开发者_开发知识库roperties that have internal setters? Or am I missing something that should be completely obvious?

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; }
}


You need to mark the setter as protected internal so that the proxy class can access it:

public class InternalSetter 
{
    public virtual string InternalSetterProperty { get; protected internal set; }
}


This is a pure .NET language problem. Try it yourself:

public class A
{
    public virtual string PrivateSetter { get; private set; }
    public virtual string InternalSetter { get; internal set; }
}

in another assembly:

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; }
}

By the way, I'm just analyzing an issue with private setters and proxies, so I'm not sure if the proxies really work in this case.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号