Adding base-class (inherited) functionality to classes that you don’t control

I have a set of classes from a 3rd party library. These classes use an inheritance structure to share logic. I would like to add a layer of abstraction in the middle of their inheritance tree to add functionality to all of the children (concrete) implementations.

Here is a simplified example of the classes in the 3rd party lib:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public interface IAnimal
{
bool IsMammal { get; }
}
public abstract class Animal : IAnimal
{
public abstract bool IsMammal { get; }
public string Name { get; set; }
}
public class Cat : Animal
{
public override bool IsMammal { get { return true; } }
public void Pur() {}
}
public class Dog : Animal
{
public override bool IsMammal { get { return true; } }
public void Fetch() {}
}
public class Snake : Animal
{
public override bool IsMammal { get { return false; } }
public void ShedSkin() {}
}
</code>
<code>public interface IAnimal { bool IsMammal { get; } } public abstract class Animal : IAnimal { public abstract bool IsMammal { get; } public string Name { get; set; } } public class Cat : Animal { public override bool IsMammal { get { return true; } } public void Pur() {} } public class Dog : Animal { public override bool IsMammal { get { return true; } } public void Fetch() {} } public class Snake : Animal { public override bool IsMammal { get { return false; } } public void ShedSkin() {} } </code>
public interface IAnimal
{
    bool IsMammal { get; }
}

public abstract class Animal : IAnimal
{
    public abstract bool IsMammal { get; }
    public string Name { get; set; }
}

public class Cat : Animal
{
    public override bool IsMammal { get { return true; } }
    public void Pur() {}
}

public class Dog : Animal
{
    public override bool IsMammal { get { return true; } }
    public void Fetch() {}
}

public class Snake : Animal
{
    public override bool IsMammal { get { return false; } }
    public void ShedSkin() {}
}

I would like to add the concept of an AnimalWithSuperPower. These types of animals should have 1 additional Property; SuperPower. I would like to be able to have classes like CatWithSuperPower which derive from Cat, AnimalWithSuperPower, & Animal so that I can access all the functionality of those.

Here is the definition of SuperPower:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public enum SuperPower { Invisibility, SuperStrength, XRayVision }
</code>
<code>public enum SuperPower { Invisibility, SuperStrength, XRayVision } </code>
public enum SuperPower { Invisibility, SuperStrength, XRayVision }

My first idea was to use multiple inheritance. But unfortunately, C# doesn’t support multiple base classes.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>private abstract class AnimalWithSuperPower : Animal
{
public SuperPower SuperPower { get; set; }
}
// doesn't compile because you can't extend 2 classes
private class DogWithSuperPower : AnimalWithSuperPower, Dog {}
</code>
<code>private abstract class AnimalWithSuperPower : Animal { public SuperPower SuperPower { get; set; } } // doesn't compile because you can't extend 2 classes private class DogWithSuperPower : AnimalWithSuperPower, Dog {} </code>
private abstract class AnimalWithSuperPower : Animal
{
    public SuperPower SuperPower { get; set; }
}

// doesn't compile because you can't extend 2 classes
private class DogWithSuperPower : AnimalWithSuperPower, Dog {}

My next attempt uses a combination of inheritance, composition, and generics to try to deliver the functionality of the base classes.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>private abstract class AnimalWithSuperPower<TAnimalType> : Animal where TAnimalType : IAnimal
{
public SuperPower SuperPower { get; set; }
protected readonly TAnimalType Animal;
protected AnimalWithSuperPower()
{
Animal = (TAnimalType) Activator.CreateInstance(typeof(TAnimalType));
}
}
private class SuperCat : AnimalWithSuperPower<Cat>
{
public override bool IsMammal { get { return Animal.IsMammal; } }
}
private class SuperCatWithPur : AnimalWithSuperPower<Cat>
{
public override bool IsMammal { get { return Animal.IsMammal; } }
public void Pur() // needing duplicate pass-through methods/properties like this is painful :(
{
Animal.Pur();
}
}
private static void ExampleUsage()
{
var invisibleCat = new SuperCat { SuperPower = SuperPower.Invisibility };
invisibleCat.Pur(); // doesn't compile - can't access Pur() method because doesn't extend Cat
var xrayCat = new SuperCatWithPur { SuperPower = SuperPower.XRayVision };
xrayCat.Pur(); // only works because I exposed the method with the EXACT same signature
}
</code>
<code>private abstract class AnimalWithSuperPower<TAnimalType> : Animal where TAnimalType : IAnimal { public SuperPower SuperPower { get; set; } protected readonly TAnimalType Animal; protected AnimalWithSuperPower() { Animal = (TAnimalType) Activator.CreateInstance(typeof(TAnimalType)); } } private class SuperCat : AnimalWithSuperPower<Cat> { public override bool IsMammal { get { return Animal.IsMammal; } } } private class SuperCatWithPur : AnimalWithSuperPower<Cat> { public override bool IsMammal { get { return Animal.IsMammal; } } public void Pur() // needing duplicate pass-through methods/properties like this is painful :( { Animal.Pur(); } } private static void ExampleUsage() { var invisibleCat = new SuperCat { SuperPower = SuperPower.Invisibility }; invisibleCat.Pur(); // doesn't compile - can't access Pur() method because doesn't extend Cat var xrayCat = new SuperCatWithPur { SuperPower = SuperPower.XRayVision }; xrayCat.Pur(); // only works because I exposed the method with the EXACT same signature } </code>
private abstract class AnimalWithSuperPower<TAnimalType> : Animal where TAnimalType : IAnimal
{
    public SuperPower SuperPower { get; set; }

    protected readonly TAnimalType Animal;

    protected AnimalWithSuperPower()
    {
        Animal = (TAnimalType) Activator.CreateInstance(typeof(TAnimalType));
    }
}

private class SuperCat : AnimalWithSuperPower<Cat>
{
    public override bool IsMammal { get { return Animal.IsMammal; } }
}

private class SuperCatWithPur : AnimalWithSuperPower<Cat>
{
    public override bool IsMammal { get { return Animal.IsMammal; } }

    public void Pur() // needing duplicate pass-through methods/properties like this is painful :(
    {
        Animal.Pur();
    }
}

private static void ExampleUsage()
{
    var invisibleCat = new SuperCat { SuperPower = SuperPower.Invisibility };
    invisibleCat.Pur(); // doesn't compile - can't access Pur() method because doesn't extend Cat

    var xrayCat = new SuperCatWithPur { SuperPower = SuperPower.XRayVision };
    xrayCat.Pur(); // only works because I exposed the method with the EXACT same signature
}

This solution is not very good (IMO) because of these reasons:

  • SuperCat and SuperCatWithPur aren’t actually instances of Cat
  • any method you wish to use from Cat needs to be mirrored in the container class
  • feels kind of messy: AnimalWithSuperPower is an Animal but it also takes an Animal type parameter

I also tried doing it with an extension method but it wasn’t any better than the above two attempts:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>private abstract class AnimalWithSuperPower : Animal
{
public SuperPower SuperPower { get; set; }
}
private static AnimalWithSuperPower WithSuperPower(this Animal animal, SuperPower superPower)
{
var superAnimal = (AnimalWithSuperPower) animal;
superAnimal.SuperPower = superPower;
return superAnimal;
}
private static void ExampleUsage()
{
var dog = new Dog { Name = "Max" };
var superDog = dog.WithSuperPower(SuperPower.SuperStrength);
superDog.Fetch(); // doesn't compile - superDog isn't an instance of Dog
}
</code>
<code>private abstract class AnimalWithSuperPower : Animal { public SuperPower SuperPower { get; set; } } private static AnimalWithSuperPower WithSuperPower(this Animal animal, SuperPower superPower) { var superAnimal = (AnimalWithSuperPower) animal; superAnimal.SuperPower = superPower; return superAnimal; } private static void ExampleUsage() { var dog = new Dog { Name = "Max" }; var superDog = dog.WithSuperPower(SuperPower.SuperStrength); superDog.Fetch(); // doesn't compile - superDog isn't an instance of Dog } </code>
private abstract class AnimalWithSuperPower : Animal
{
    public SuperPower SuperPower { get; set; }
}

private static AnimalWithSuperPower WithSuperPower(this Animal animal, SuperPower superPower)
{
    var superAnimal = (AnimalWithSuperPower) animal;
    superAnimal.SuperPower = superPower;
    return superAnimal;
}

private static void ExampleUsage()
{
    var dog = new Dog { Name = "Max" };
    var superDog = dog.WithSuperPower(SuperPower.SuperStrength);
    superDog.Fetch(); // doesn't compile - superDog isn't an instance of Dog
}

If I had control of the 3rd party classes, I could likely do this cleanly by introducing a new class in the middle of the inheritance tree, but I can’t

My Question:

How can I model AnimalWithSuperPower so that:

  • instances are considered of types Cat (or appropriate sub-class), Animal, & AnimalWithSuperPower
  • all the methods and properties are available without extra pass-through calls

1

Why don’t you just use interfaces?

If you’re concerned about sharing functionality, you can use extension methods to serve as your pseudo-base class. It’s not exactly ideal, but it should get what you’re looking for done.

Something along the lines of:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public interface IAnimalWithSuperPower {
public SuperPower power { get; set; }
}
public class SuperCat : Cat, IAnimalWithSuperPower {
public SuperPower power { get; set; }
public SuperCat() {
SuperPower = SuperPower.SuperStrength;
}
}
public static void UseSuperPower(this IAnimalWithSuperPower animal) {
animal.power.doSomething();
}
</code>
<code>public interface IAnimalWithSuperPower { public SuperPower power { get; set; } } public class SuperCat : Cat, IAnimalWithSuperPower { public SuperPower power { get; set; } public SuperCat() { SuperPower = SuperPower.SuperStrength; } } public static void UseSuperPower(this IAnimalWithSuperPower animal) { animal.power.doSomething(); } </code>
public interface IAnimalWithSuperPower {
    public SuperPower power { get; set; }
}

public class SuperCat : Cat, IAnimalWithSuperPower {
    public SuperPower power { get; set; }
    public SuperCat() {
        SuperPower = SuperPower.SuperStrength;
    }
}

public static void UseSuperPower(this IAnimalWithSuperPower animal) {
    animal.power.doSomething();
}

4

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật