简体   繁体   English

在c#中继承泛型

[英]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 它抱怨说, T无法导出,当我宣布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. 你正在输入HappyDecorator到T,但在该类中没有T的实例。

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();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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