TJHeuvel
TJHeuvel

Reputation: 12608

Members which are only public from another assembly

In C# we can define any member as Internal, which makes it only visible inside the current assembly:

internal int Age; 

I was wondering if its possible to reverse this effect, marking it private/protected to this assembly, but public to a specified other assembly.

This sounds completely insane, but we have a valid reason to do this. We're using the Unity3D game engine, where all of our game-logic is in the 'runtime' assembly. We can also define custom editors for these classes, which allow us to create custom UI controls in the IDE. Those editors live in a special 'editor' assembly.

This editor assembly frequently needs more information about a specific type in the runtime assembly then we'd like to expose to our own assembly. Our current solution is to get the specific private/protected member via Reflection, but i'd like to know if there is a better solution.

Upvotes: 1

Views: 81

Answers (2)

Haney
Haney

Reputation: 34762

It does sound like a design issue. The data that must be known to both assemblies should be isolated to its own assembly and referenced by each of them. Failing that, you could look at creating a wrapper/adapter that publicly exposes the hidden data in the core assembly, in a read-only way that is available to your other assemblies.

Upvotes: 0

C.Evenhuis
C.Evenhuis

Reputation: 26446

You can't change the visibility of these members.

One option is to create an interface that exposes the desired members, and then explicitly implement it:

class MyClass : IEditable
{ 
    internal int Age { get; private set; }
    int IEditable.Age { get; set; }
}

You'd have to cast MyClass to IEditable to access the properties in this case. It would also help identify when changing the runtime assembly would break the editor.

Upvotes: 3

Related Questions