0
internal class MyClassA
{
    [...properties...]
    public List<string> DoSomething(){}
}

// Different project/assembly, same solution:
public class MyBase
{
    protected MyClassA _myClassA = new MyClassA(); // Here is the problem

}

// What I want to do:
public class SomeOtherClass : MyBase
{
    private static void Foo(){
        var list = _myClassA.DoSomething();
    }
}

SomeOtherClass inherits from MyBase.

MyBase shall make an instance of MyClassA to make it accessible by SomeOtherClass (protected).

But since MyClassA is internal, this is not possible.

How can I make it possible, that SomeOtherClass can use MyClassA.DoSomething() while using the instance from MyBase?

PS: I am perfectly aware of the possibility to "just change visibility from MyClassA to public", that is not an option though.

Arno Nym
  • 49
  • 1
  • 7
  • 2
    https://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396 – Aleksey L. Aug 23 '17 at 05:49
  • I am not able to modifiy class MyClassA in any way. If that would be possible, I would just have changed to public – Arno Nym Aug 23 '17 at 05:58
  • 1
    you don't need to modify MyClassA , the attribute is applied at the assembly level – Aleksey L. Aug 23 '17 at 06:01
  • Just include the MyBase class into the assembly that contains MyClassA. – Alin Aug 23 '17 at 06:22
  • The linked duplicate is incorrect, this is, contrary to the description in the title, not about inheritance, but simply about internal classes being, well ... internal, and how to handle this. @AlekseyL.'s comment above is the right one. To make internal classes in one assembly available in another you must add `InternalsVisibleToAttribute` to the assembly with the classes. – Lasse V. Karlsen Aug 23 '17 at 06:41
  • @Lasse: thank you, you are correct. I was misled by the OP's inclusion of inheritance in the question, even though it has absolutely nothing to do with what they are actually trying to do. Of course, the question is still a well-documented duplicate. I've updated the duplicate list accordingly. – Peter Duniho Aug 25 '17 at 04:22

1 Answers1

-1

You can change visibilty of MyClassA to protected:

protected class MyClassA
{
    [...properties...]
    public List<string> DoSomething(){}
}

if you are "not able to modifiy class MyClassA in any way" then you can't.

Encapsulation is a basic pricipal of OOP, & C# is primarily an OO language.

It's not going to let you violate the encapsulation of a class you don't have access to.

Ashley Pillay
  • 868
  • 4
  • 9
  • Voting down an answer with no incorrect statments without ant comment??? All the comments above, as well as the referenced duplicate answers target the first part on the question "making internal classes visible" while ignoring the context of wanting to do this only for "inheriting classes". 'InternalsVisibleTo' has to be applied preemtively, and cannot apply to all inherited classes. If the OP says he can't modify the class why would he be able to modify the project which is far more invasive than modifying a single class? – Ashley Pillay Aug 28 '17 at 06:41