Good Evening to Everyone,
I have 2 abstract classes in C# :
public abstract class A
{
}
public abstract class B
{
}
I want in my third Class to decide on run time through the config file to either extend Class A or Class B.
For Example :
public Class C : B or A
Both of the abstract classes provide one abstract method with the same Signature .
No, you can't change/chose base class at run-time as base class is defined at compile time.
It sounds like implementing common interface in both classes and using just interface everywhere else would be good solution in your case.
interface IListener
{
void Listen();
...
}
class CA : A, IListener{ .... }
class CB : B, IListener { .... }
// to use:
IListner listener = useSSL ? new CA(...) : new CB(...);
If I am understanding correctly from your question and the comments (check what #RB said), the appropriate behavior would be to define:
public interface IC
{
}
public class A : IC
{
}
public class B : IC
{
}
Write your code to consume the Interface, and contrive a way to inject the correct concrete class into the consumer.
Related
Looking for help to put such structure together.
Have base generic class:
public class A<T>
where T: class
{
public T info { get; set; }
}
Works good for one step inheritance like:
class B : A<BInfoClass>{}
But need same for higher hierarchy members like, but without making all classes generics:
class C : B<CInfoClass>{}
Need possibility to have specific "info" type for each B, C, D etc. ("info" classes derive from one base):
I'd like to suggest to add one more level of hierarchy just to make generic class to be pure generic.
You can put all common code to the AContainer also.
public class AContainer<T> where T : class, InfoA {
public T info { get; set; }
}
public class A : AContainer<InfoA> {}
public class BContainer<T>: AContainer<T> {}
public class B : BContainer<InfoB> {}
public class CContainer<T>: BContainer<T> {}
public class C : CContainer<InfoC> {}
A, B and C classes are empty classes for the without making all classes generics condition (to be used in Unity)
AContainer BContainer and CContainer are added to make generic possible here.
However it does not look like a place to use generics
I've read a lot of questions of people asking how to have implicit ctor inheritance, so to not have to copy them around.
This question is totally the opposite: why C# is not forcing me to do it anymore? I want the derived classes to be forced to implement the base constructors...but it's not happening. What am I doing wrong?
Base class
public abstract class LogicalDevice
{
private LogicalDevice()
{
}
protected LogicalDevice(string id)
{
}
}
Intermediate derived class
public abstract class Device : LogicalDevice
{
public Device(string ID)
: base("ID")
{
}
public Device(ConfigurationData configuration)
: base(configuration["ID"])
{
}
}
Final derived class
internal class CoffeMachineDevice : Device
{
public CoffeMachineDevice (ConfigurationData configuration)
: base(configuration)
{
}
}
Why the class CoffeMachineDevice compiles?
C# requires that every constructor in derived class must call a constructor in the base class. But there is no requirement that is must call each constructor of the base class.
That's why CoffeMachineDevice compiles.
I need some sort of way to mark base interfaces and identify if a class implemented the base interface or its derived interface. c# doesn't allow having 'abstract interface'. Is there any way to do this in c#?
public interface IBaseFoo
{
void BaseMethod();
}
public interface IFoo : IBaseFoo
{
void FooMethod();
}
public class Base
{
}
public class A : Base, IFoo
{
}
public class B : Base, IBaseFoo
{
}
Now in the following method I need to check if the typeCls is implemented the IFoo or IBaseFoo without explicitly specifying types. I need sort of a way to mark the base interface and identify it in the method. (ie: if c# allowed having abstract interface, I could have check if IsAbstract property of interfaces of typeClas)
public bool IsBaseFooImplemented<T>(T typeCls) where T : Base
{
// Here I need to check if the typeCls is implemented the IFoo or IBaseFoo
}
Because IFoo : IBaseFoo, every class implementing IFoo also implements IBaseFoo. But not the other way around, so you can simply check whether typeCls is IFoo.
Do note that changing behavior based on implemented interfaces generally is a design smell that bypasses the use for interfaces in the first place.
//somewhere define
static List<IBaseFoo> list = new List<IBaseFoo>();
public class A : Base, IFoo
{
public A()
{
YourClass.list.add(this);
}
}
public class B : Base, IBaseFoo
{
public B()
{
YourClass.list.add(this);
}
}
//then you can check if a class is IFoo or not.
public bool IsBaseFooImplemented<T>(T typeCls) where T : Base
{
foreach(var c in list )
{
if(typeof(c) == typeCls) return true;
}
return false;
}
I have not tested the code but it should work.
I am trying to find a way to derive a class from a generic base class. Say:
sealed public class Final : Base<Something>
{
}
public class Base<T> : T
where T : Anything // <-- Generics do not allow this
{
}
In C# this does not seem to be possible.
Is there any other solution to achieve something similar to this?
I found this StackOverflow question, but it doesn't seem to solve the issue, or at least I do not understand how it should.
EDIT:
The result I'd like to get is to be able to do something like that:
Anything[] anything;
//Assign some Instances to anything
foreach(Final final in anything){
//do something with final
}
The result I'd like to get is to be able to do something like that:
Anything[] anything;
//Assign some Instances to anything
foreach(Final final in anything){
//do something with final
}
Your foreach loop suggests this: class Anything : Final { … }.
This obviously turns around the inheritance hierarchy as you planned and named it. (You cannot have cycles in your inheritance relationships).
public class Base<T> : T where T : Anything { …
Let me elaborate on this part for a bit. I'll reduce your example even further to just class Base<T> : T.
This is not possible, for good reason. Imagine this:
class Base<T> : T
{
public override string Frobble()
{
Fiddle();
return "*" + base.Frobble() + "*";
}
}
class A
{
public sealed string Frobble() { … }
}
class B
{
}
class C
{
public virtual string Frobble() { … }
}
abstract class D
{
public abstract void Fiddle();
public virtual string Frobble() { … }
}
class E
{
public void Fiddle() { … }
public virtual string Frobble() { … }
}
You get all kinds of absurd situations if class Base<T> : T were allowed.
Base<A> would be absurd because Frobble cannot be overridden in a derived class.
Base<B> would be absurd because you cannot override a method that
doesn't exist in the base class.
Base<C> doesn't work because there is no Fiddle method to call.
Base<D> would not work because you cannot call an abstract method.
Only Base<E> would work.
How would the compiler ever know how to correctly compile Base<T> and analyse code that depends on it?
The point is that you cannot derive from a class that is not known at compile-time. T is a parameter, i.e. a variable, a placeholder. So class Base<T> : T is basically like saying, "Base<T> inherits from some (unknown) class". Class inheritance is a type relationship that requires both involved types to be known at compile-time. (Actually, that's not a super-precise statement because you can inherit from a generic type such as class SpecialList<T> : List<T>. But at the very least, the derived class has to know what members (methods, properties, etc.) are available in the base class.)
Is this what you want?
sealed public class Final : Base<int>{
}
public class Base<T> {
}
You could only do this if Final would be a generic class as well, like so:
public sealed class Final<T> : Base<T>
Then you can put a type restraint on T as either a class, to allow only reference types as T, or an instance of Base<T>, to allow only types that derive from Base<T>:
public class Base<T> where T : Base<T>
I don't know the context of this question, but I ran into same question with a project where I had to make it possible to extend the base class which is already derived by many others. Like:
abstract class Base {}
class FinalA : Base {}
class FinalB : Base {}
// Now create extended base class and expect final classes to be extended as well:
class BetterBase : Base {}
The solution was to create common ancestor and connect through properties:
abstract class Foundation {}
abstract class Base : Foundation
{
Foundation Final { get; }
}
class FinalA : Foundation {}
class FinalB : Foundation {}
class FinalC : Foundation
{
Foundation Base { get; }
}
// Here's the desired extension:
class BetterBase : Base {}
Now BetterBase has connection to final class and if needed, the final classes could have connection with (Better)Base also, as shown in FinalC class.
I want to force any class not to be able to create a new instance if it inherits a specific base class, so how this base class should look like?
The following code is in java. just to give you an Example
Base class has an exception on the constructor.
public class BaseClass
{
public BaseClass()
{
throw new AssertionError();
}
}
The child class extending the base class but if you create an object of it it will give u an exception.
public class MainClass extends BaseClass
{
public MainClass()
{
}
public static void main(String[] args) {
MainClass c = new MainClass();
}
}
You want to seal your base class.
public sealed class BaseClass
{
public BaseClass(){};
}
public class SubClass : BaseClass
{
public SubClass(){};
}
This will throw a compiler error because you cannot inherit from a sealed base.
You can't specify that in the baseclass, any deriving class is self responseable, if it wants to present the ability to be derived from, than you can't do anything about it.
you can declare the base class as const - that way other classes cant extend it.
You can't do this. Please specify why you want to do this.