How to Create a Hidden Property or Method That’s Still Public

Have you ever needed to hide a property or method on a class, but still needed to access it outside of the class?  Obviously declaring the property as “internal” will only allow the property to be accessible from within the same assembly, but what if you needed to access it outside of the assembly, but also didn’t want to make it public for others to see.  An example of this might be a business entity that you allow customers to retrieve and save data using a WCF service client.  The entity exposes properties that represent a business object that you persist to a database.  The entity is also used in other business processes which require information that you don’t want to expose as part of the entity.

The following method describes a technique to add a property or method to a class that isn’t directly accessible on the entity, but is available explicitly through an interface.

1. First let’s create an entity with a public property.

    public class MyEntity
    {
        /// <summary>
        /// My Public Property
        /// </summary>
        public string MyPublicProperty { get; set; }
    }

2.  Now, let’s create an interface with a property that we want to use on the entity.

public interface IMyHiddenPropertiesInterface
{
string MyInterfaceProperty { get; set; }
}

3.  Now, have the entity implement the interface, but explicitly implement the interface property. I’ve also overriden the “ToString” method so we can print out the property values later.

public class MyEntity : IMyHiddenPropertiesInterface
{
/// <summary>
/// My Public Property
/// </summary>
public string MyPublicProperty { get; set; }

/// <summary>
/// My Explicit Implementation of my interface property
/// </summary>
string IMyHiddenPropertiesInterface.MyInterfaceProperty { get; set; }

public override string ToString()
{
return MyPublicProperty + " " + ((IMyHiddenPropertiesInterface)this).MyInterfaceProperty;
}
}

4. Finally, here’s a console app that creates an entity and sets both of the properties.

class Program
{
static void Main(string[] args)
{
// Create a new entity and set the public property
MyEntity entity = new MyEntity() { MyPublicProperty = "Hello" };

// Set the hidden property
IMyHiddenPropertiesInterface entityInterface = (IMyHiddenPropertiesInterface)entity;
entityInterface.MyInterfaceProperty = "World!";

Console.WriteLine(entity.ToString());
Console.ReadLine();
}
}

Notice when you look at the meta data associated with entity that you don’t see the MyInterfaceProperty.  That’s because it’s only accessible through the interface.

Here’s what you see when you look at the interface data from intellisense.

MyInterfaceProperty is hidden when acting on the MyEntity variable, but it’s available explicitly through the interface.  This allows you to hide data on your entity that you don’t want customers to easily access and still make it publicly available outside of the assembly.  If your entity code is publicly available to your consumers and their code savy, then they can get to your property, but it’s not as easy as making it public.

Here’s the program output when executed:

Explicit interface implementation is ideal for classes that implement multiple interfaces with the same properties or method names, but since explicit implementation doesn’t make the properties available directly on the class, it’s also a tricky way of hiding something that you don’t want to make public.  Another option is to make your property internal and create a public utility class within the same assembly that can access the property.   This works just as well since it makes it much less obvious to consumers outside of your assembly that there’s hidden data on the entity.

Related Articles:

MSDN Explicit Interface Implementation

Advertisements

About Brian McKay
Software Architect

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: