1

I asked this question to multiple people and until now I do not have an answer.

ASP.NET C#

Project tree (files and folders):

> (ROOT) 
>
> Class MyAccess (namespace myproject.core)
> 
> (Directory John)
------> Class MyJohn          (namespace myproject.core.John)
------> Class JohnSecret      (namespace myproject.core.John)
------> Class OtherJohnSecret (namespace myproject.core.John)
> 
> (Directory Paul)
------> Class MyPaul          (namespace myproject.core.Paul)
------> Class PaulSecret      (namespace myproject.core.Paul)

How can I use (public, private, protected, internal) ????? to have have this behavior:

  • class MyJohn sees and can create objects from all the classes inside John folder (all his secrets)
  • class MyPaul have the same behavior but inside Paul folder
  • All this secrets CAN NOT be used OUTSIDE this folders.

Examples:

  • MyPaul can use all his secrets, and he can communicate with the classes MyAccess and with MyJohn.
  • MyPaul, MyJohn, MyAccess will be public or internal
  • MyAccess can not use PaulSecret.

Solutions that I do not like:

  • BAD Solution 1

Make John secrets as protected and heritage one MyJohn

Example:

> protected JohnSecret
> internal MyJohn:JohnSecret

bad because if i've 100 class secrets I'll need to have something like:

> internal MyJohn:JohnSecret1, JohnSecret2,....,JohnSecret100
  • BAD Solution 2

Have classes inside classes:

> internal class MyJohn {
> 
>        internal string DoSomething(){}
>        
>        private class JohnSecret{}
>        private class JohnSecret2{}
>        private class JohnSecret3{}
>
> }

It's not good because, once again, if I've 100 secrets I'll have a HUGE file, I can't split that code in different source code files.


Can anyone give a good solution?

I appreciate :)

Thanks a lot.

3
  • Why do you need so many secret classes? Commented Aug 24, 2009 at 20:46
  • If you have such complex situations with access modifiers, I guess there must be something other wrong in your situation. Commented Aug 24, 2009 at 20:49
  • This question came when we've code that will be developed by a team (multiple people coding -> CVS control) and sometimes with the Visual Studio's IntelliSense we can access one class (in some other code directory) that should not be used directly. This is why I asked how can we avoid this. Commented Aug 26, 2009 at 13:51

2 Answers 2

3

You should go with the second solution you propose. For the visibility requirements that you have it is most appropriate for JohnSecret, OtherJohnSecret, et. al. to be defined inside the MyJohn class.

If your desire is to split the class file in multiple files, you can use partial classes to achieve that and still maintain the visibility requirements that you have.

For example:

> (Directory John)
> File "MyJohn.cs"
public partial class MyJohn
{
}

> File "JohnSecret.cs"
public partial class MyJohn
{
    private class JohnSecret
    {
    }
}

> File "OtherJohnSecret.cs"
public partial class MyJohn
{
    private class OtherJohnSecret
    {
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

C# has the following access.

  • public - everyone can see it
  • private - only things inside the class can see it
  • protected - only things inside the class, or inside derived classes can see it
  • internal - only things inside the assembly (dll or exe) can see it
  • protected internal - only things inside the class or derived classes can see it, and only if they are in the same assembly

You have only 2 options:

  • As Neil mentioned, you can use internal, and split your code up into 2 assemblies - one for John and one for Paul.
  • As you have already mentioned, you can use private, and nest all the JohnSecret classes inside the outer John class. paracycle's tip of using partial classes makes this reasonably nice.

Note: to make multiple assemblies you must make multiple projects inside visual studio.


From a pragmatic point of view, does it really matter if the directories are fenced off from each other like that? There are no security benefits in using access modifiers as anyone can always use reflection to easily call your private/internal methods anyway.

The main benefit it gives you is keeping clutter out of your way when coding, but you've already put them in different namespaces, which should help with clutter anyway.

2 Comments

I tested the partial classes the way "paracycle" said and it's working fine. About the reflection aspect you talked about: I used the tool "Red Gate's .NET Reflector" and yes, I can see all my private classes and core. I am also able to Disassemble. and I can see the code .... :S This showed me that there isn't a way to protect our code? How can I sell a program and do not allow a smartguy to clone my code?
Actually, protected internal is protected or internal, so it is visible to anything in the same assembly, the class itself and its derived classes, regardless of which assembly they are in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.