Internal class visible from another namespace in Unity project - c#

I am organizing my unity project into namespaces, and want to hide the classes that are used only inside the namespaces.
I define the namespace MyProject.Accounts and place an internal class there:
namespace ProjectName.Accounts
{
internal struct PictureData
{
internal int notSeenMember;
}
}
Then i add the using ProjectName.Accounts to a file in ProjectName.LoginScreen namespace, and i am able to see the class through the Intellisence, and instanciate it, but not to use the notSeenMember member:
The MS says that :
Internal types or members are accessible only within files in the same assembly.
Is being able to instanciate internal classes from other namespaces an expected behavior?
EDIT: you can also access the internal members

Related

Can I break encapsulation/data hiding with namespaces (.Net)

If I am making a .Net dll, is it possible to break encapsulation in any program that uses it, by creating a class with the same namespace?
For example, consider a DLL with the following code in it:
using System;
namespace MyDLL
{
internal class MyClass
{
internal void Stuff()
{
...
}
}
}
If I was to then create a project, and reference the DLL, would I be able to do something like the following?
using System;
using MyDLL;
namespace MyDLL
{
public class TheirClass
{
MyClass exposedClass = new MyClass();
exposedClass.Stuff();
}
}
I'm currently working on a project that will have a few abstract classes I want a user to be able to inherit, but I only want to certain features to be exposed.
Thanks in advance.
No, using the same namespace have no impact on data encapsulation nor any impact on visibility (private/protected/internal). Namespaces are syntactic sugar in C#, actual class names consist of namespace, name and assembly identity.
So in your particular case adding class with full name MyDLL.TheirClass{TheirAssembly} will not make MyDLL.MyClass{MyDLL} visible from TheirClass (since your class is internal and new class is from other assembly not marked as InternalsVisibleTo in your assembly).

Accessibility in .Net Class Library

I am working on a class Library and I'm having troubles with accessibility. My class library contains several internal classes which shouldn't be accessed from other applications. Instead I want to create a Singleton Main Class that contains Instances of all the internal classes, so other applications can access the Main class and then use the internal classes from that instance. The picture below explains the hierarchy.
I've tried making the Main Class public and the Internal Classes internal, however this gives me the error Error "Inconsistent accessibility". My Main Class looks like this:
public class Main
{
private static Main Instance;
public static Main GetInstance()
{
if (Instance == null)
Instance = new Main();
return Instance;
}
public Debugging Debugger = new Debugging();
}
And one of my Internal Classes (Debugging) looks like this:
internal class Debugging
{
Content....
}
So I'm hoping that someone can help me to figure out how to make the Internal Classes only accessible through my singleton Main Class.
I'm not positive from your question what your intent is so I'll break it into two options:
You want `Debugging` accessible to external assemblies, but only via `Main`
There are a few ways how to do this, but the simplest right now for you would be to keep Debugging public, but define only internal constructors. This will allow its usage but external assemblies won't be able to instantiate them, thus forcing them to access the instance created on Main
public class Debugging
{
internal Debugging() { }
}
You don't want `Debugging` accessible to external assemblies, and but still accessible within your class assembly via `Main`
Simply update the accessibility modifier for Main.Debugger to be internal
internal Debugging Debugger = new Debugging();
You have to set all of you class public if you have some public access to them (via the Main class).
Otherwise you can set the internal class as internal and provide a set of properties in the Main class (that wrap internal classes' fields/methods) in order to access their field/methods. You can set the constructors of internal class as internal in order to avoid those class instantiations.
In general: Every field/method exposed from a public class should have a public return type.
Some MSDN reference
If you need to access the Debugger class outside the Library you need to change the access modeifier to public. As suggested make the constructor as internal so no one outside the assembly can create an instance of those classes.

Why VS IDE gives error when we create private class

If we create private class under any namespace then we got compilation error but if we create private class as a nested class in another class then it compile fine. Another guy explained why we got an error for declaring private under any namespace ? He said:
Allowing classes to be private to a namespace would achieve no
meaningful level of protection.Any assembly in the world could simply
reference your dll, and start writing code in your namespace which
accesses your supposedly private classes.I think that's possibly the
answer you'd get from Microsoft.
I just really do not understand this sentence "start writing code in your namespace which accesses your supposedly private classes"
How can any one access my private class? Any can tell me this "start writing code in your namespace which accesses your supposedly private classes"
Suppose you have one DLL where you wrote:
// hypotetical code !
namespace My.Namespace
{
private class MyClass
{
}
}
By this you want to declare that this class can only be used withing the namespace My.Namespace. You want to make this class internal to that namespace and protect if form usage outside.
Now anyone could create a different DLL reference the above DLL and declare the same namespace My.Namespace and actually be able to use MyClass anyway. Because its in the same namespace:
// hypotetical code !
namespace My.Namespace
{
public class AnotherClass
{
private hisClass = new MyClass();
}
}
What use is the private modifier in this case?
Imagine the following scenario:
You write an assembly A.dll with a namespace Mou.MyStuff. In that namespace, there is a private class SomeClass. To whom should that class be visible, based on its private visibility? Only to other types in the same namespace?
Then, someone else can write his or her own assembly B.dll. In that assembly, they can define a class SomeOtherClass in namespace Mou.MyStuff. Nothing prevents them from using the very same namespace as you did, hence there would be absolutely no obstacle to accessing your "private" type.
If what you are looking for is visibility only within your assembly, use the internal visibility.

Hiding namespaces containing only internal types in a class library?

I have a class library that has a couple of namespaces containing only internal types.
However, when using the class library in an application project, the namespaces shows up in intellisense, but of course they are empty. Is there any way for me to hide the namespaces completely when using intellisense in other projects?
I've tried to apply EditorBrowsableAttribute to all the internal classes as well, but what I'd like to do would be to apply that to the namespace, which is of course impossible.
Or is, if I care enough about this, the only option I have to just move the types into a namespace that contains public types?
It depends on how you're referencing your class library:
If you have the class library project contained within your solution and use a project reference, you'll always see that empty namespace via Intellisense.
If you're referencing a compiled dll of your class library, you won't see the namespace popping up in intellisense, provided it contains only internal members.
Try this:
namespace ClassLibrary1
{
namespace Internal
{
internal class InternalClass
{
public int internalStuff { get; set; }
}
}
namespace Public
{
public class PublicClass
{
public int publicStuff { get; set; }
}
}
}
If you reference this via a project reference, you'll see the empty namespace. If you reference a dll of it, you won't.
I've come up against this before and found no solution, only a workaround which may or may not work in your project. Instead of defining a namespace you could use an nested static class?

Class-member access-modifiers

I want a method of the class: "One" ("AccessibleWithinSameNamespace") to be accessible by the class: "Two", without having "Two" extending "One".
Both classes are in the same namespace, so I'm thinking that maybe there's an access-modifier that emulates the "protected" modifyer, but for namespaces.
Some code:
namespace Test
{
class One
{
public void AccessibleToAll()
{
}
protected void AccessibleWithinSameNamespace()
{
// I am not public
// I can be accessed from other classes
// within the same namespace of my class
}
}
}
namespace Test
{
class Two
{
public Two()
{
One one = new One();
// I am able to access this method because my class
// is in the same namespace as the class: "One"
one.AccessibleWithinSameNamespace();
}
}
}
You can use the internal modifier if both classes are in the same assembly.
With your example:
namespace Test
{
class One
{
public void AccessibleToAll()
{
}
internal void AccessibleWithinSameNamespace()
{
// I am not public
// I can be accessed from other classes
// within the same namespace of my class
}
}
}
namespace Test
{
class Two
{
public Two()
{
One one = new One();
// I am able to access this method because my class
// is in the same namespace as the class: "One"
one.AccessibleWithinSameNamespace();
}
}
}
C# and .NET have no concept of "within the same namespace". Internal is the closest equivalent.
The problem isn't the access modifier for the namespace its the access modifier for the function. "Protected" means that it can be accessed by child classes, not by other classes even if they're in the same namespace.
You have several solutions to choose from...
1. you can make the access modifier for the function "internal" then all classes/functions inside the same assembly (or marked with some cool assembly flags so that they pretend to be in the same assembly) can access it
2. Make "TWO" a sub class of "One" then two can call into it (but not via an instance of "One" as shown in the same code
Since namespaces are arbitrary - they can't really be a security boundary. Anyone can create an assembly that reuses your namespace Test.
The best you can do is limit by assembly using the already mentioned internal (and InternalsVisibleTo if needed).
Edit: InternalsVisibleTo allows other assemblies to access internal methods and classes as if they were in the same assembly. You can strong name the other assemblies, which provides security. This is commonly used for test assemblies to test internal members of the main assembly, without bloating the main assembly by including the test code.
Note that, the VB.NET compiler does not respect InternalsVisibleTo - so a VB assembly cannot call into an InternalsVisibleTo attributed assembly. However, C# can call into a VB.NET assembly that has the appropiate InternalsVisibleTo attribute.

Categories