简体   繁体   中英

Inheriting a generic in c#

I've inherited a large codebase and I'm trying to implement some new functionality into the framework. Basically, in order to do it the "right" way, I would have to modify the entire structure of the framework. since I'm not the guy who designed the framework, nor am I a mind reader, doing so probably isn't going to happen (although I would really love to redesign it all from scratch myself).

So in order to do what I want, I'm trying to implement a decorator pattern, of sorts. This answer from maliger suggests that what I'm doing below is perfectly valid. However, mono doesn't seem to like it; it complains that T cannot be derived from when I declare HappyDecorator

Please forgive the overly simplistic example, but it gets the point across.

public class HappyObject
{
    public virtual void print()
    {
        Console.WriteLine ("I'm happy");
    }
}

public class VeryHappyObject : HappyObject
{
    public override void print()
    {
        Console.WriteLine ("I'm very happy");
    }

    public void LeapForJoy()
    {
        Console.WriteLine("Leaping For Joy!");
    }
}

public class SuperHappyObject : VeryHappyObject
{    
    public override void print()
    {
        Console.WriteLine ("I'm super happy!");
    }

    public void DieOfLaughter()
    {
        Console.WriteLine("Me Dead!");
    }
}

public class HappyDecorator<T> : T where T : HappyObject
{
    public string SpecialFactor { get; set; }

    public void printMe()
    {
        Console.WriteLine (SpecialFactor);
        print();
    }
}
class MainClass
{
    public static void Main (string[] args)
    {
        HappyDecorator<HappyObject> obj = new HappyDecorator<HappyObject> ();
        obj.SpecialFactor = Console.ReadLine();
        obj.printMe();
    }
}

You're typing HappyDecorator to T, but there's no instance of T to use inside that class.

public class HappyDecorator<T> where T : HappyObject
{
    private readonly T _instance;

    public HappyDecorator(T instance)
    {
        _instance = instance;
    }

    public string SpecialFactor { get; set; }

    public void printMe()
    {
        Console.WriteLine(SpecialFactor);
        _instance.print();
    }
}

Another alternative is to structure it like this with a generic method instead of a generic class. It's not really a decorator then though:

public class HappyDecorator
{
    public string SpecialFactor { get; set; }

    public void printMe<T>(T instance) where T : HappyObject
    {
        Console.WriteLine(SpecialFactor);
        instance.print();
    }
}

And call like:

        HappyDecorator obj = new HappyDecorator();
        obj.SpecialFactor = Console.ReadLine();
        obj.printMe(new HappyObject()); 

I think this is what you are trying to do:

    public interface IhappyObject
    {
        void Print();
    }

    public class HappyObject : IhappyObject
    {
        private IhappyObject obj;

        public HappyObject(IhappyObject obj)
        {
            this.obj = obj;
        }

        public void Print()
        {
            obj.Print();
        }
    }

    public class VeryHappyObject : IhappyObject
    {
        public void Print()
        {
            Console.WriteLine("I'm very happy");
        }
    }

    public class SuperHappyObject : IhappyObject
    {
        public void Print()
        {
            Console.WriteLine("I'm super happy!");
        }
    }

    static void Main(string[] args)
    {
        HappyObject obj = new HappyObject(new SuperHappyObject());
        obj.Print();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM