Sign in to follow this  
Proudest

[.net] [C#] Accessing private class through public propery?

Recommended Posts

I have an assembly named "Engine.dll" which contains a "Root" class and "TextureManager" class. Root contains a TextureManager instance which is accessed externally through a public property.
namespace Engine
{
	public class Root
	{
		TextureManager textureManager;
		
		public Root
		{
			textureManager = new TextureManager();
		}
		
		public TextureManager TextureManager
		{
			get { return textureManager; }
		}
	}
	
	public class TextureManager
	{
		internal TextureManager() //internal constructor
		{
			//
		}
		
		public void LoadTexture()
		{
			//
		}
	}
}

namespace Program
{
	using Engine;
	
	static void Main()
	{
		Program program = new Program();
	}
	
	public Program()
	{
		Root root = new Root(); //creates TextureManager instance
		root.TextureManager.LoadTexture();
	}
}


With the internal constructor, I successfully prevent the end user from creating his own TextureManager instance. He has to use the one I provide. Is it possible to hide the TextureManager class from the outside while still allowing access through the public property in Root?
namespace Program
{
	using Engine;
	
	static void Main()
	{
		Program program = new Program();
	}
	
	public Program()
	{
		//I still want this to be possible (visible through root)
		Root root = new Root(); //creates TextureManager instance
		root.TextureManager.LoadTexture();
		
		//But not this (invisible without root)
		TextureManager myTextureManager;
	}
}


I want the class to appear private outside of the Engine but still accessible through the root class from the outside. If normal classes don't allow it, is there a way to allow this with interfaces?

Share this post


Link to post
Share on other sites
I think what you want isn't possible, but I'm not sure.

What would be the difference between a 'quasi-private' class like you are describing and a public class with an internal constructor? Either way, they can only use the instance that you provide for them. I think the way you're currently doing it is good.

Share this post


Link to post
Share on other sites
No, you can't do that. How would it make sense to have access to a class you don't have access to, if you know what I mean?

Share this post


Link to post
Share on other sites
You could make a public ITextureManager interface, and then make the TextureManager class private. You could then have a property that returns the texture manager as an ITextureManager object. So, you would have given access to the object without giving access to the class, but since you can make the constructor internal anyway, I don't see what the point would be in this case.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this