Prevent inherited member from accessing protected member - c#

Is there a way in C# to prevent a derived class from inheriting a protected property?
class Base
{
protected int A { get; set; }
}
class DerivedA : Base
{
}
class DerivedB : DerivedA
{
}
What I want is for DerivedA to be able to access property A, however DerivedB should not. So is there a way to limit the inheritance of a property in the middle of the inheritance hierarcy?

Well, there is some valuable information in the comments, so I thought I'd better recap it so it will not get lost.
Kevin Gosse suggested to use the private protected access modifier.
The private protected keyword combination is a member access modifier. A private protected member is accessible by types derived from the containing class, but only within its containing assembly.
Please note that this access modifier is only available in c# 7.2 or higher.
While I agree with Kevin this might be a direct answer to your question, HimBromBeere suggested that this question is, in fact, an XYProblem - meaning you are asking how to implement a solution you are having problems with, instead of asking how to solve the underlying problem.
I also agree with him as well.
Fildor suggested using composition over inheritance - which is a very good point. using inheritance only for code reuse is a mistake. Remember that a derived class is a specific type of the base class type - for instance, a dog can derive from animal because a dog is a specific type of animal, but an airplane can't derive from a car just because they both have engines.
To get an answer to the actual underlying problem, I suggest you edit your question to include that problem, not only the current solution you are trying to implement, or perhaps ask a brand new question instead.

You should make it sealed:
class Base
{
protected int A { get; set; }
}
class DerivedA : Base
{
protected sealed override int A { get => base.A; set => base.A = value; }
}
class DerivedB : DerivedA
{
// Attempting to override A causes compiler error.
}
read more here: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/sealed

Why would you want a class to not inheriting a protected property of its base class?
You should redesign your class model:
class Base
{
}
class BaseA : Base
{
protected int A { get; set; }
}
class Derived : Base
{
}
class DerivedA : BaseA
{
}

Related

How not to allow exposing classes derived from a specific class?

Assume that a component (say CompA) exposes a public C# .NET class (say Base) in an SDK:
namespace CompA { public abstract class Base {} }
A different component (say ExtB) extends CompA by deriving from this base class:
namespace ExtB { class Derived : CompA.Base {} }
What is your opinion, what is the best approach to not to allow declaring class ExtB.Derived as public, please? CompA.Base is meant for extending CompA, but not for exposing new public API with it.
namespace ExtB { public class Derived : CompA.Base {} } // DISALLOW
What I can think for is to expect assemblies exposing public API to be marked with a .NET custom attribute and mark the base class with another .NET custom attribute. The product that loads these assemblies would then not allow such assembly to load that exposes/publishes such types that derive from types marked with the custom attribute.
AssemblyInfo.cs of CompA:
[assembly:SdkApiAssemblyAttribute()]
AssemblyInfo.cs of ExtB:
[assembly:SdkApiAssemblyAttribute()]
Base.cs:
namespace CompA
{
[Publishable(false)]
public abstract class Base {}
}
Thanks!
At first it sound you're doing something wrong. After confirming that your intention is just to prevent exposing the type accidentally, It sounds reasonable. It might be simple to write a unit test, but if Derived classes are not part of your code base you can do the following.
There is no compile time way to prevent this(AFAIK). Just check the type in Base class constructor and throw exception.
public abstract class Base
{
protected Base()
{
if (this.GetType().IsPublic)
throw new InvalidOperationException("Type is not meant to be exposed public");
}
}
public class Der : Base
{
public Der()
{
}
}
This will prevent the client from creating new instance of Der if it is declared as public.
Note: There are some scenarios that constructor will not run, You're out of luck then. Fortunately they are corner cases, not to be worried about much. For Example: FormatterServices.GetUninitializedObject won't run the constructor.
#SriramSakthivel, you are correct: I did something wrong! :)
Although not intentionally I did not say that CompA.Base was actually not a class, but an interface!
public interface Base
{
string ShouldNotBePublic();
}
The base class should expose the method with protected access modifier:
public abstract class Base
{
protected string ShouldNotBePublic();
}
This way the derived class will not acidentally expose the protected information. The component CompA is then able to access this method by implementing the template method patter:
namespace CompA
{
public abstract class Base : IBaseInternal
{
protected string ShouldNotBePublic();
string IBaseInternal.ShouldNotBePublic()
{
return ShouldNotBePublic();
}
}
internal interface IBaseInternal
{
string ShouldNotBePublic();
}
}
Sorry for the confusion.

modifying access permissions from an abstract class in the derived class [duplicate]

This question already has answers here:
Adding a set accessor to a property in a class that derives from an abstract class with only a get accessor
(3 answers)
Closed 9 years ago.
So I have an abstract class called AbstactSearchWithTwoLevelCache that was provided to me. All of its abstract properties only have read access (with a get;). I am not permitted to add a set; to the those abstract properties. Is there a way to change this in the derived class, SearchWithTwoLevelCache? In other words, is there a way to set these properties in the derived class?
If you mark the property with new, you define a new property, like this:
abstract class BaseClass
{
public int Property
{
get { ... }
}
}
class NewClass : BaseClass
{
public new int Property
{
get { return base.Property; }
set { ... }
}
}
EDIT:
The above works if the property in the base class is not abstract. When it is abstract, this will not work since you need to implement it. One option you do have is to create a class in between, like this:
abstract class BaseClass
{
public abstract int Property { get; }
}
class Between : BaseClass
{
public override int Property
{
get { ... }
}
}
class NewClass : Between
{
public new int Property
{
get { return base.Property; }
set { ... }
}
}
This however in no way is an elegant solution. Then, the real answer becomes that you cannot really do this (at least not without the above work around).
I can't imagine a way you can override them directly. Indirectly when you override the abstract class properties, the Getters can retrieve from a private field, and then you can create new properties that have Setters that set those fields.
Kind of a rig I know. Maybe there is a more elegant way around this.

Abstract fields aren't allowed. How do I force a derived class to instantiate/initialize a field?

I have an abstract base class for a generic 'Device' type and would like to require the derived specific device classes to initialize a list based on their respective differences.
When one sets the derived class to active it must go through the list to set the parameters to active as well. Each derived class will insert their own parameters into the list.
Here is the relevant code:
Base class:
abstract public class Device : Common, IDisposable
{
abstract public Boolean bActive
{
get;
set;
}
abstract List<Parameters> ActiveParameters;
...
}
I don't wish to initialize the List in the base class because I do not know which parameters are going to be inserted into the list from the derived class. This code produces an error saying that I cannot have an abstract field and I understand that but is there a way to require a derived class to initialize a field from the base class?
This code produces an error saying that I cannot have an abstract field and I understand that but is there a way to require a derived class to initialize a field from the base class?
If this is required to be implemented, it should be part of your API. To handle this, you can make this a protected property. This will force it to be implemented by all derived classes.
protected abstract List<Parameters> ActiveParameters { get; }
If this is about initializing the list, then you can force that by putting that list into the constructor (or all constructors, if you have more of them) of the base class.
Something like:
public abstract class Device : Common, IDisposable
{
protected Device(List<Parameters> activeParameters)
{
ActiveParameters = activeParameters;
}
protected List<Parameters> ActiveParameters { get; private set; }
}
The derived class will be then forced to do something like:
public class ConcreteDevice : Device
{
public ConcreteDevice()
: base(new List<Parameters>())
{}
}

can I have an abstract base class with the key attribute being generic

I want to create a re-usable library. I was going to use extension methods however I run into some issues in some cases for the client to have to specify in the calling method the types.
QUESTION - If I use an abstract base class as the basis, can I specify an attribute/property in the class to be generic (e.g. the key property might be an 'int' in one case, or a 'string' in another)?
Yes.
public abstract class MyBase<T>
{
public abstract T GetSomething();
}
public class MyConcreteType : MyBase<int>
{
public override int GetSomething()
{
return 3;
}
}
Or, what exactly do you mean ?
(Trying it out would have given you the answer faster then posting it on SO, I think ... )
Yes, you can do the following:
public abstract class Class<T>
{
T value;
T Prop { get; set;}
}

How to use a class as the base, but hide the class type publically?

I am currently just exposing the properties through a generic interface e.g.
public interface IBaseClass
{
int ID { get; set; }
}
internal class MyBaseClass : IBaseClass
{
public MyBaseClass() { }
public int ID { get; set; }
}
public class MyExposedClass : IBaseClass
{
private MyBaseClass _base = new MyBaseClass();
public int ID
{
get { return _base.ID; }
set { _base.ID = value; }
}
}
Then in my main application I can do:
IBaseClass c = new MyExposedClass();
c.ID = 12345;
But can't do:
MyBaseClass b = new MyBaseClass();
This is my desired behaviour.
However, I was just wondering if this is the correct approach? Or if there was a better way?
If you only want to prevent instantiation you could make MyBaseClass abstract (make it's constructor protected as well - it is a good design) and have MyExposedClass derive from it. If you want to completely hide the type your approach seems fine.
This look fine to me. Making small interfaces makes it easier to write decoupled code.
I don't know if this will help, but you can make your base class protected internal. This would mean that any internal class has access to it as if it were public, or any class (from within and without the assembly) can subclass the base class. It won't prevent people from implementing their own sub class though.
Alternatively, exposing through an Interface would be the best way I'd think.
For this you can opt for explicit implementation like this:
public interface IBaseClass
{
int ID { get; set; }
}
internal class MyBaseClass : IBaseClass
{
public MyBaseClass() { }
public int IBaseClass.ID { get; set; }
}
public class MyExposedClass : IBaseClass
{
private MyBaseClass _base = new MyBaseClass();
public int IBaseClass.ID
{
get { return _base.ID; }
set { _base.ID = value; }
}
}
You can refer to a similar post C# Interfaces. Implicit implementation versus Explicit implementation
Make your base class abstract.
You could expose the interface as public, implement an internal sealed implementation of that class, and use a factory approach to build instances of the desired interface. That way the client will never know when you change your implementation, or if you have multiple implementations of the same base interface plugged in the factory. You could also eliminate the set accessors in the interface and put them in the internal implementation to only expose the properties to the outside world. That way the exterior code has to make less assumptions over your implementation and you are better isolated. Please correct me if I'm having a poor/bad image of this approach.
Edit: The factory would be public and you'd need some sort of "transfer object" to pass data to the factory. That transfer object implementation would be public, together with it's interface.
Your example seems to include a poor example of taking advantage of inheritence. Since you included a single property it and couldnt come up with a better example i am guessing that its real. I would suggest in this case forget the base class and stick the property on the derived.

Categories

Resources