访问内部属性进行组装范围 [英] Accessing internal property out of the assembly scope

查看:131
本文介绍了访问内部属性进行组装范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有内部属性类:

 内部虚拟StateEnum EnrolmentState 
{
获得{。 .getter逻辑}
集合{..setter逻辑}
}

不过我希望能够访问到该属性之外组装的,所以我创建方法只是简单地返回该属性:

 公共StateEnum GetCurrentState()
{
返回EnrolmentState;
}



但是,当我把从类此方法本届大会之外我得到一个异常



(System.TypeLoadException:方法上键入'get_EnrolmentState'EnrolmentAopProxy从程序集44fe776f-458e-4c5d-aa35-08c55501dd43,版本= 1.0.0.0,文化=中立,公钥=空'是压倒一切的,是不是从组装可见的方法。)



因此,它是可以访问到内部成员大会以外的任何方式,还是应该考虑不同的方法。



只是提一提,这个类用作O / R映射实体(NPersist),它是从O / R映射overrided注入持久性代码。


解决方案

为什么物业内部首先?如果你想拥有它的公共访问,将其公开。我假设你已经在这个一些控制,否则您将无法添加一个公共的方法来访问它摆在首位。



如果您只想选择其他组件才能够访问它, InternalsVisibleTo 是你的朋友(双关语并非意) - 但正如埃里克说,你应该仔细考虑的设计在这一点

至于为什么你得到那个特定的错误 - 它看起来像你的AOP代理仍在试图重写内部属性,而不是使用您的公共方法。很难知道你是否可以改变,如果没有更多地了解您的特定设置 - 但使公共财产很可能是一个简单的修复


I have class with internal property:

internal virtual StateEnum EnrolmentState  
{
    get { ..getter logic }
    set { ..setter logic }
}

However I want to be able to access to this property outside of the assembly so I created method that simply returns this property:

public StateEnum GetCurrentState()
{
    return EnrolmentState;
}

But when I call this method from class outside of this assembly I get an exception

(System.TypeLoadException: Method 'get_EnrolmentState' on type 'EnrolmentAopProxy' from assembly '44fe776f-458e-4c5d-aa35-08c55501dd43, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is overriding a method that is not visible from that assembly.)

So it is possible to access to internal member outside of the assembly in any way, or I should consider a different approach.

Just to mention that this class is used as an O/R mapper entity (NPersist) and it is overrided from the O/R mapper to inject persistence code.

解决方案

Why is the property internal in the first place? If you want to have public access to it, make it public. I assume you have some control over this, as otherwise you wouldn't be able to add a public method to access it in the first place.

If you only want selected other assemblies to be able to access it, InternalsVisibleTo is your friend (pun not intended) - but as Erik says, you should think about the design carefully at that point.

As to why you're getting that particular error - it looks like your AOP proxy is still trying to override the internal property, rather than using your public method. It's hard to know whether or not you can change that without knowing more about your particular setup - but making the property public is likely to be a simpler fix.

这篇关于访问内部属性进行组装范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆