AndyH79
asked on
Custom Class Visibility
I have a couple different classes in a base class library that application objects will inherit or implement from.
I'm wondering if there is a way to limit the TouchRecord class so that it can only be implemented within a class that inherits from BaseClass.IDR.Meter. The calls from App.Facility methods should still be able to access the fields of the TouchRecord class within declared App.Meter object, they just should not be able to declare TouchRecord properties within the facility class. Is it possible to constrain classes in this way?
DLL Code
namespace BaseClass
{
public class Facility
{
}
}
namespace BaseClass.IDR
{
public class Meter
{
}
public class TouchRecord
{
public Nullable<DateTime> idrHarvestFirst { get; set; }
public Nullable<DateTime> idrHarvestLast { get; set; }
public Nullable<DateTime> idrConversionLast { get; set; }
public Nullable<DateTime> idrImportLast { get; set; }
public Nullable<DateTime> idrValidationLast { get; set; }
}
}
I'm wondering if there is a way to limit the TouchRecord class so that it can only be implemented within a class that inherits from BaseClass.IDR.Meter. The calls from App.Facility methods should still be able to access the fields of the TouchRecord class within declared App.Meter object, they just should not be able to declare TouchRecord properties within the facility class. Is it possible to constrain classes in this way?
Application Code
namespace App
{
public class Facility : BaseClass.Facility
{
public App.Meter meter;
public BassClass.IDR.TouchRecord touchRecord; //This should cause an error
public void newMeter(DateTime dateTime)
{
meter = new App.Meter();
meter.touchRecord = new BaseClass.IDR.TouchRecord(); //This should be ok
meter.touchRecord.idrHarvestFirst = dateTime; //This should be ok
BaseClass.IDR.TouchRecord facilityTouchRecord = new BaseClass.IDR.TouchRecord //This should cause an error
}
}
public class Meter : BaseClass.IDR.Meter
{
BassClass.IDR.TouchRecord touchRecord; //This is ok
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
While protected internal doesn't work, the article you linked to has a note that says this:
"The protected internal accessibility level means protected OR internal, not protected AND internal. In other words, a protected internal member can be accessed from any class in the same assembly, including derived classes. To limit accessibility to only derived classes in the same assembly, declare the class itself internal, and declare its members as protected."
When I list the class as internal and the members as protected, it does limit things as I would like, so I'm going to accept your response as a solution.