views:

451

answers:

3

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 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.

Thank you

A: 

This sounds like you should reconsider your choice of design. Internal is used to avoid what you are trying to do, so consider using some kind of public access to the properties instead.

It's possible to use the InternalsVisibleTo Attribute to make a specific assembly able to reach internal properties but from my point of view that's a poor design.

Erik Hellström
+1  A: 

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.

Jon Skeet
A: 

Yes I agree that this is weird design. I will go with protected modifier since InternalVisibleTo attribute doesn't works for me.

Aleksandar