-1

C# avoid user to override internal protected methods. I know this question has been asked and I found Eric Lippert's example about it. But I didn't consider that as a good idea. Here is my example:

// A.dll
public abstract class A {
    public abstract void Method1(); 
    internal protected abstract void Method2(); 
}

// B.dll, ref A.dll
public class B : A {
    public override void Method1(); 
    internal protected override void Method2(); 
}

public class B1 {
    public void MethodC(){
        var b = new B();
        // can access b.Method1
        // can access b.Method2
    }
}

// C.dll, ref A.dll and B.dll
public class C {
    public void MethodC(){
        var b = new B();
        // can access b.Method1
        // can not access b.Method2
    }
}

In this case, Method2 can be accessed by B1, not by C. But since internal protected can not be override, that will be not convenient to do this. How do you guys think about it?

---- add comment ---------

My purpose is clear. I want to let Method2 can be accessed by B1, and I don't want Method2 access by C. The problem is that C# does not allow to overwrite "internal protected".

---- add comment again ---------

Well, I know what's the problem now. This question is easy but Eric Lippert's example make it complex.

Community
  • 1
  • 1
Renkuei
  • 276
  • 3
  • 8
  • 2
    What is your question exactly? What problem are you trying to solve? Are you trying to override Method2? Because you should be able to do that. Protected internal means you can access the method from the assembly or any derived classes. – Mike Sackton Aug 25 '16 at 02:11
  • 1
    Very unclear what concrete question you have. Linked question explains why it is and only way to solve it. If you simply want to chat whether you like that design or not - SO is wrong place, if you have concrete problem where such confusing access modifiers combination is needed - ask your concrete problem... Also clarifying how IOC tag is related to the question would be nice addition while you [edit] the post to clarify it. – Alexei Levenkov Aug 25 '16 at 03:04
  • My purpose is clear. I want to let Method2 can be accessed by B1, and I don't want Method2 access by C. The problem is that C# does not allow overwrite internal protected. – Renkuei Aug 25 '16 at 03:58

1 Answers1

0

The internal modifier means that members of the same assembly are able to access it. If class C is in a different assembly, it will not be able to see the internal methods of the assemblies it is referencing. If you need an external assembly to be able to access the internal members of an assembly, you can add the InternalsVisisbleToAttribute to the assembly information for assembly B granting access to assembly C.

Alternatively, you can have a type in your external assembly inherit from your Type B, and it would gain access through virtue of being within the protected scope. You could then call the methods through your local type as expected.

Jonathon Chase
  • 9,396
  • 21
  • 39
  • InternalsVisibleToAttribute is interesting, but I did not want to limit the particular assembly. In my case, I want that class B1 have right to access Method2, but it is out of protected scope. C# does not allow to overwrite "internal protected". – Renkuei Aug 25 '16 at 07:26
  • Well, I know what's the problem now. This question is easy but Eric Lippert's example make it complex. Thank you for your help. – Renkuei Sep 05 '16 at 08:21