Jump to content
  • Advertisement
Sign in to follow this  
Proudest

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

This topic is 4437 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!