Get the name of class inheriting from base class in C# - c#

I have the following base class
class BaseClass
{
//Want to get the name of class A only, even if B is inherited from A
}
class A : BaseClass
{
}
class B : A
{
}
Can someone help me with getting the name of class A only from the base class.
Thanks

public abstract class BaseClass
{
public void GetInheritorName()
{
var nameIs = this.InheritorTellMeYourName();
}
protected abstract string InheritorTellMeYourName();
}
public class A : BaseClass
{
protected sealed override string InheritorTellMeYourName()
{
return typeof(A).Name;
}
}
public class B : A
{
}
Like someone mentioned in the comments, you shouldn't usually need anything like that...

Related

Derived classes with derived properties

I have 2 classes that are both derived from a base class X. The base class contains a property of a class T. Both subclasses of X should contain a property that's derived from T.
What I would like to achieve is to declare the a property of T in the base class X and have it used in several methods, while using the same property of T in the derived classes of X but have it recognized as a derived class from T, so that I won't have to cast it every time I want to use it.
One of my solutions would be just remove the property from the base class and copy the methods for each derived class of X, but that would defeat the purpose of using inheritance.
Is anything like that achievable?
internal class TestObject
{
public string ID;
public XObject obj;
//....
}
internal class TestDocument : TestObject
{
public XDocument obj; //<--- want to be able to call obj in my methods
//as if it were an XDocument type but relate to the obj property in the base class
//....
}
internal class XObject
{
//....
}
internal class XDocument : XObject
{
//....
}
Generics should work for you:
class Base<T> where T: MyType
{
T MyProperty { get; set; }
public void DoSomething()
{
// do something with MyProperty
}
}
with MyType being the base-class of the property within Base.
Then in your derived class you can define the generic constraint, e.g.
class Derived : Base<DerivedType>
{
}
Now an instance of Derived has the property MyProperty of type DerivedType instead of MyType.
So in your case TestObject should be similar to this:
internal class TestObject<T> where T: XObject
{
public string ID;
public T obj;
//....
}
internal class TestDocument : TestObject<XDocument>
{
// obj is here of type XDocument
}
Make the type of the property a generic parameter of your base class:
class PropertyTypeBase { }
class PropertyTypeA : PropertyTypeBase { }
class Base<T> where T : PropertyTypeBase
{
public T Property { get; }
}
class Foo : Base<PropertyTypeA>
{
public Foo()
{
PropertyTypeBase x = Property;
PropertyTypeA a = Property;
}
}
The simplest way would be to make the base class generic, and constrain the generic parameter to be derived form a certain class:
class BaseProp { }
class DerivedPropA: BaseProp { }
class DerivedPropB : BaseProp { }
abstract class X<T>
where T: BaseProp
{
public T Value { get; set; }
public abstract void With(T value);
}
class A : X<DerivedPropA>
{
public override void With(DerivedPropA value)
{
this.Value = value;
}
}
class B : X<DerivedPropB>
{
public override void With(DerivedPropB value)
{
this.Value = value;
}
}
This is possible by using generics.
First, let me explain the example classes. Let's say these are your properties:
public class BaseHead {}
public class OrganicHead : BaseHead {}
public class CyborgHead : BaseHead {}
And you now want to implement these heads on your person classes:
public class BaseCreature {}
public class OrganicCreature : BaseCreature {}
public class CyborgCreature : BaseCreature {}
The solution:
public class BaseCreature<THead> where THead : BaseHead
{
public THead Head { get; set; }
public BaseCreature(THead head)
{
this.Head = head;
}
}
We make the BaseCreature generic
We limit the THead type to only allow types that either are BaseHead or are derived from BaseHead
However, we also want to ensure that the right creature (organic/cyborg) only uses the correct head (organic/cyborg). This can be done by deriving from a BaseCreature with a specific generic type:
public class OrganicCreature : BaseCreature<OrganicHead>
{
public OrganicCreature(OrganicHead head) : base(head)
{
}
}
CyborgCreature is analogous.
Suppose you wanted to make it possible that every creature can use every type of head. If that's what you want, then you need to keep the generic parameter generic:
public class OrganicCreature<THead> : BaseCreature<THead> where THead : BaseHead
{
public OrganicCreature(THead head) : base(head)
{
}
}
CyborgCreature is analogous.

Protected generic class - is it supported?

I had a question on C# generics. I wish to store a generic type variable in my abstract class without declaring that type outside the class.
Below is the code sample. Please note that I do not wish to make the Param classes exposed outside the Calc class.
Thanks in advance.
- Dutta.
abstract class Base { }
abstract class Calc<T> where T : Base
{
protected Param Member; /* how can this be a made a generic declaration
* WITHOUT declaring this class like,
* class Calc<T, P>
* where T : Base
* where P : Param */
protected Calc(Param p)
{
this.Member = p;
}
protected abstract class Param { }
}
class MyBase : Base { }
class MyCalc : Calc<MyBase>
{
public MyCalc() : base(new MyParam()) { }
public void doSomething()
{
base.Member.A++; // fails on compilation
}
private class MyParam : Calc<MyBase>.Param
{
public int A;
public MyParam() { this.A = 0; }
}
}
You just need to cast it to the new type, because no matter what, the variable Member was declared as Param and it will always be accessed as Param:
((MyParam)base.Member).A++;
Secondly, you can fix up your MyParam class by changing from this:
MyParam : Calc<MyBase>.Param
To this:
MyParam : Param
Because Param is already Calc<MyBase> through generics and inheritance.
Thraka's answer is correct: if you don't want to use generics you need to cast. Just to add to it, in case what you're really trying to do looks something like this. Here's a set of classes that you can expose from your library, which will not be extensible by clients (unless they're running with full trust and can use reflection etc.!!) but which can be used in a type-safe way.
public abstract class SupportedPaymentMethod
{
protected internal SupportedPaymentMethod() { }
}
public sealed class Check : SupportedPaymentMethod
{
public int CheckNumber { get; private set; }
public Check(int checkNumber)
: base()
{
CheckNumber = checkNumber;
}
}
public sealed class CreditCard : SupportedPaymentMethod
{
public CreditCard()
: base()
{ }
}
public abstract class Payment<T>
where T : SupportedPaymentMethod
{
public T Method { get; private set; }
protected internal Payment(T method)
{
Method = method;
}
}
public sealed CheckPayment : Payment<Check>
{
public CheckPayment(Check check)
: base(check)
{ }
}
public sealed CreditCardPayment : Payment<CreditCard>
{
public CreditCardPayment(CreditCard creditCard)
: base(creditCard)
{ }
}
Clients (i.e. code outside of your class library's assembly) will be able to instantiate a CheckPayment or a CreditCardPayment, but they will not be able to create a new class deriving from Payment<T>. So, it will not be possible for clients to create a CheatingPaymentMethod : Payment<Cheating>, for example. :)
Calls like your intended call to base.Member.A++ will now work:
var checkPayment = new CheckPayment(new Check(123456));
var checkNumber = checkPayment.Method.CheckNumber; // Success! :)

c# templates how can i apply constraints to a class that derives from something

stoopid question time again.
I have this class that pulls in some code via a base class like so:
class TVIRoot : OURTreeNodeImpl { }
I now want to add some template functionality
class TVIRoot<TLabelHandler> : OURTreeNodeImpl { }
But I can't work out what sort of finger mangling I need to get it to compile when I need to supply some constraints.
class TVIRoot<TLabelHandler> where TLabelHandler : new(), OURTreeNodeImpl { } //no
class TVIRoot<TLabelHandler> where TLabelHandler : SomeClass : OURTreeNodeImpl { } //no
class TVIRoot<TLabelHandler> : OURTreeNodeImpl, where TLabelHandler : SomeClass { } //no
Can this be done?
Many thanks.
bg
class TVIRoot<TLabelHandler> : OURTreeNodeImpl where TLabelHandler : SomeClass { } //yes
The constraint comes after the base class inheritance, here is an example:
public interface IFood
{
}
public class Animal
{
}
public class Cat<T> : Animal where T : IFood
{
public void Eat(T food)
{
}
}
for more details check:
http://msdn.microsoft.com/en-US/library/d5x73970(v=vs.80).aspx

Restricting subclasses from inheriting certain methods of base class

using System;
public class Base
{
public Base()
{
}
public void M1()
{
}
public void M2()
{
}
public void M3()
{
}
}
public class Derived : Base
{
//this class should get only method 1
}
public class SecondDerived : Base
{
//this class should get only method 2 and method3
}
The requirement is : the base class contains the 3 methods M1, M2, M3.
The derived class should inherit only M1 and SecondDerived should inherit only M2 and M3.
How can this be done?
You cannot selectively inherit methods like this. A derived class automatically inherits all public methods of the base class. I suggest you to split the Base class into two classes:
public class Base1
{
public Base1()
{
}
public void M1()
{
}
}
public class Base2
{
public void M2()
{
}
public void M3()
{
}
}
public class First : Base1
public class Second : Base2
You cannot do it in this way. Inheritance implies an "IS A" relationship.
If SecondDerived would not have a M1() then it would not be compatible with a reference to a the class Base.
So maybe you shouldn't be using inheritance for whatever problem you're solving.
It is not possible to do what you want with inheritance.
It seems you have no intention of overriding, you simply want to "inherit" behavior from the base class selectively. You could do this using a "has a" relationship:
public class Base
{
internal Base() {} //mark constructor as internal so it can not be used outside your assembly if necessary
public Foo Mehtod1() {...}
public Foo Mehtod2() {...}
public Foo Mehtod3() {...}
}
Then simply do the following:
class A
{
private Base internalBase;
public A() { this.internalBase = new Base(); }
public Foo Method1() { return this.internalBase.Method1(); }
}
class B
{
private Base internalBase;
public A() { this.internalBase = new Base(); }
public Foo Method2() { return this.internalBase.Method2(); }
public Foo Method3() { return this.internalBase.Method3(); }
}
UPDATE: A possible alternative solution is to make your Base class methods virtual and override them all in your derived classes, throwing NotSupportedExceptions in those methods that you do not want the class to make available. I don't really like this solution but it has the advantage of not loosing the polyphormism inheritance gives you which might be useful if you have some core base functionality which all derived classes will share (in your example you seem to imply they wont).
It is possible by adding Obsolete attribute
public class A
{
public virtual void M1() { }
public void M2() { }
public void M3() { }
}
public class B : A
{
[Obsolete("You can not use this", true)]
public sealed override void M1()
{
}
}
public class C : B
{
public void Test()
{
// Will show error
base.M1();
}
}

Inheriting from a class that inherits from a abstract class

I trying to inherit a class Blah2, but after adding a method it says BlahA doesn't implement that method.
How can I add a method to my new class?
public class Blah2 : BlahA
{
}
public class Blah3 : Blah2
{
public List<int> MyNewMethod()
{
}
}
Note: BlahA is an abstract class.
Update
public abstract class BlahA : IBlah
{
}
Update II - the error
Error 3 'Blah.Components.BlahA' does not contain a definition for 'Blah3' and no extension method 'Blah3' accepting a first argument of type 'Blah.Components.BlahA' could be found (are you missing a using directive or an assembly reference?)
Well if it's implementing an interface as you posted in your comments, then the problem is that your BlahA class doesn't satisfy the requirements of the interface. There must be some method in the interface (I'm assuming its the MyNewMethod) that you're not implementing in your abstract BlahA class.
If my assumption is correct, add this to your base class:
public abstract List<int> MyNewMethod();
and in your sub class, add the word override to your method declaration.
Some code:
public interface MyInterface
{
void MyMethod();
}
public abstract class Base : MyInterface
{
public abstract void MyMethod();
}
public class SubA : Base
{
public override void MyMethod()
{
throw new NotImplementedException();
}
}
public class SubB : SubA
{
public void Foo() { }
}
Wrting this code and compiling works fine
public abstract class BlahA
{
}
public class Blah2 : BlahA
{
}
public class Blah3 : Blah2
{
public List<int> MyList()
{
return new List<int>();
}
}
We will need a bit more of the code that isnt working
EDIT:
from comments you need to implement the method from interface in abstract class.
public interface IBlah
{
int GetVal();
}
public abstract class BlahA : IBlah
{
public int GetVal()
{
return 1;
}
}
public class Blah2 : BlahA
{
}
public class Blah3 : Blah2
{
public List<int> MyList()
{
int i = GetVal();
return new List<int>();
}
}

Categories

Resources