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:
- I do not want to make
Fielda protected subclass ofSomeClass, because it is used by other classes; - I do not want to make
Fieldvisible outside ofTestnamespace, because it's breaks encapsulation; - I need inheritance, because
SomeDerivedClass1andSomeDerivedClass2share a lot of code.
Is any workaround for this problem without breaking any of these rules?
CodePudding user response:
You can use the new private protected modifier. This is the equivalent of internal AND protected.
Not to be confused with
protected internalwhich isprotectedORinternal
public abstract class SomeClass
{
private protected Field field;
}
It is still visible in the same assembly outside the namespace though.
