Exposing internal classes as properties?

  • Thread starter Thread starter Dave Veeneman
  • Start date Start date
D

Dave Veeneman

Is there a way to expose an internal class as a public property of its
parent object? Let's say I have a Customer object with Public accessibility.
It has an Addresses collection property that it populated with Address
objects. This property has Public accessibility, as well.

To keep the assembly interface clean, I want to hide the objects on which
the property is based (The Address and Addresses objects) from users outside
the assembly. Declaring them as Internal would do the trick, but the
compiler throws an "Inconsistent Accessibility" exception, since the
property is Public, and the objects on which it is based are Internal.

Is there any way around this issue? Is there some other way to expose the
objects only through the property based on them?

Thanks.
 
Is there a way to expose an internal class as a public property of its
parent object? Let's say I have a Customer object with Public accessibility.
It has an Addresses collection property that it populated with Address
objects. This property has Public accessibility, as well.

If the object is internal, making it available as a public property makes no
sense as the user can access the property but not the type returned! :)
Is there any way around this issue? Is there some other way to expose the
objects only through the property based on them?

Provide an IAddress interface that your internal class inherits and have the
property return an IAddress interface rather than the object type (example
follows):

n!

// Note this isn't a complete example...

public class Customer
{
private Address address = null;

public IAddress Address
{
get
{
return address;
}
}
}

public interface IAddress
{
// Interface definition
}

internal class Address : IAddress
{
// Implementation...
}
 
If your Addresses/Address object are going to be exposed via a property,
they have to be declared somewhere, why not create a public class or
interface and inherit from that, see example below:

//Create Abstract Bar class for FooBar to inherit
public abstract class Bar
{
private int barCount;

public int BarCount {get{return barCount;}set{barCount = value;}}
}

public class Foo
{
private Bar bar = new FooBar(10);

//Expose a Bar, from our private FooBar
public Bar FooBarObject {get{return bar;}}

private class FooBar: Bar
{
//Constructor only visible to Foo
internal FooBar(int cnt)
{
this.BarCount = cnt;
}
}
}
 
Thanks for the quick replies. What I take away from this is: If I am going
to declare a property ObjectProperty of type SomeObject, where SomeObject is
a class I have defined, then any user of that property is going to need to
see the SomeObject class. Otherwise, the user can't get to any of the
ObjectProperty 'sub-properties' contained in the SomeObject class.

"It seems so simple when uoy put it that way..." --Homer Simpson
 
Back
Top