Reputation: 624
I am trying to write a class library as following:
namespace Test
{
class Field
{
}
public abstract class SomeClass
{
protected Field field; //Inconsistent accessibility: field type 'Field' is less accessible than field 'SomeClass.field'
}
public class SomeDerivedClass1:SomeClass
{
}
public class SomeDerivedClass2:SomeClass
{
}
}
Rules:
Field
a protected subclass of SomeClass
, because it is used by other classes;Field
visible outside of Test
namespace, because it's breaks encapsulation;SomeDerivedClass1
and SomeDerivedClass2
share a lot of code.Is any workaround for this problem without breaking any of these rules?
Upvotes: 1
Views: 58
Reputation: 71159
You can use the new private protected
modifier. This is the equivalent of internal
AND protected
.
Not to be confused with
protected internal
which isprotected
ORinternal
public abstract class SomeClass
{
private protected Field field;
}
It is still visible in the same assembly outside the namespace though.
Upvotes: 3