在类的所有实例中引发事件

时间:2010-08-03 16:48:23

标签: c# events inheritance

我有一个名为生物的抽象类,它有一个 Born 事件。我还有一些继承 Organism 类的类...所以我的问题是如何在所有类的所有实例中引发 Born 事件,继承了有机体上课?

编辑:抱歉,我打算写的事件是Tick not Born ... Born是一个实例级事件......

4 个答案:

答案 0 :(得分:2)

public class Organism : IDisposable
{
    public static List<Organism> All = new List<Organism>();

    private bool disposed = false;

    public Organism()
    {
        Organism.All.Add(this);
    }

    public void BeBorn()
    {
        throw new NotImplementedException();
    }

    public void Dispose()
    {
        Dispose(true);

        GC.SuppressFinalize(this);
    }

    private void Dispose(bool disposing)
    {
        if (!this.disposed)
        {
            if (disposing)
            {
                Organism.All.Remove(this);
            }

            disposed = true;
        }
    }

<击>

<击>
    ~Organism()
    {
        Dispose(false);
    }

<击>

}

答案 1 :(得分:0)

无法获取某些类的所有实例的列表,因此您必须自己跟踪新实例:

List<Organism> organisms = new List<Organism>();

Organism bob = new Amoeba();
organisms.Add(bob);

稍后您可以迭代您的实例列表,并让它们一个接一个地引发事件:

foreach (Organism organism in organisms)
{
    organism.Awake();
}

答案 2 :(得分:0)

这不太好,但这可能会奏效:

public abstract class Organism : IDisposable
{
    private static readonly List<Organism> LiveOrganisms = 
        new List<Organism>();

    private event EventHandler onBorn;

    public void InvokeBorn(EventArgs e)
    {

        foreach (var liveOrganism in LiveOrganisms
            .Where(liveOrganism => liveOrganism.onBorn != null))
        {
            liveOrganism.onBorn(this, e);
        }
    }

    public event EventHandler Born
{
    add
    {
        onBorn += value;
    }
    remove
    {
        onBorn -= value;
    }
}

    protected Organism()
    {
        LiveOrganisms.Add(this);
    }

    public void Dispose()
    {
        LiveOrganisms.Remove(this);
    }
}

答案 3 :(得分:0)

为什么每个具体的生物都不会在它出生时调用它的父母的Born事件。类似的东西:

public abstract class Organism
{
    public event EventHandler<EventArgs> Born;
    public string Name { get; set; }

    public Organism()
    {
    }

    public void Birth()
    {
        this.raiseBorn(new EventArgs());
    }

    protected void raiseBorn(EventArgs args)
    {
        EventHandler<EventArgs> handler = this.Born;
        if (handler != null)
            handler(this, args);
    }
}

// Concrete organisms ========--------

public class Dog : Organism
{
    public Dog()
    {
        this.Name = "Dog";
    }

    public override string ToString()
    {
        return this.Name;
    }
}

public class Cat : Organism
{
    public Cat() 
    {
        this.Name = "Cat";
    }

    public override string ToString()
    {
        return this.Name;
    }
}

// Creature Factory ========--------

public class CreatureCreator : List<Organism>
{
    public event EventHandler<BornArgs> CreaturBorn;
    private void raiseCreatureBorn(BornArgs args)
    {
        EventHandler<BornArgs> handler = this.CreaturBorn;
        if (handler != null)
            handler(this.CreaturBorn, args);
    }

    public void CreateCreature<T>() where T : Organism, new()
    {
        Organism o = new T();
        o.Born += o_Born;
        this.Add(o);

        o.Birth();
    }

    private void o_Born(object sender, EventArgs e)
    {
        this.raiseCreatureBorn(new BornArgs((Organism)sender));
    }
}

public class BornArgs : EventArgs
{
    public Organism Creature { get; set; }

    public BornArgs(Organism o)
    {
        this.Creature = o;
    }
}

// Usage ========--------

static void Main(string[] args)
{
    CreatureCreator deity = new CreatureCreator();
    deity.CreaturBorn += deity_CreaturBorn;

    deity.CreateCreature<Dog>();
    deity.CreateCreature<Cat>();

    Console.ReadKey();
}

static void deity_CreaturBorn(object sender, BornArgs e)
{
    Console.WriteLine(e.Creature.ToString() + " was born");
}

输出:

Dog was born
Cat was born