繁体   English   中英

如何使用反射从子类调用父类中的方法?

[英]How to invoke method in parent class from child class using reflection?

我创建子对象(客户,产品等),并在父类(AggregateRoot)中调用方法ApplyChange,从该方法中我想在子类中为通过的事件调用方法Apply。 是否可以使用反射或我应该更改某些内容?

public abstract class AggregateRoot
{
    public void ApplyChange(IEvent @event)
    {
        Apply(@event); // how to call this method?
    }
}


public class Customer : AggregateRoot
{    
    private void Apply(CustomerCreatedEvent e)
    {
        Console.WriteLine("CustomerCreatedEvent");
    }
}

public class Product : AggregateRoot
{
    private void Apply(ProductCreatedEvent e)
    {
        Console.WriteLine("ProductCreatedEvent");
    }
}

public interface IEvent
{
}

public class CustomerCreatedEvent : IEvent
{
}

public class ProductCreatedEvent : IEvent
{
}

class Program
{
    static void Main(string[] args)
    {
        Customer customer = new Customer();
        customer.ApplyChange(new CustomerCreatedEvent());

        Product product = new Product();
        product.ApplyChange(new ProductCreatedEvent());
    }
}

是否可以使用反射或我应该更改某些内容?

我现在专注于不反射,因为IMO反射应该是这里的最后手段。

选项1:抽象方法

您可以使Apply方法成为一个抽象方法,然后可以从AggregateRoot调用它。

例如

using System;


public abstract class AggregateRoot
{
    public void ApplyChange(IEvent @event)
    {
        Apply(@event); // how to call this method?
    }

    protected abstract void Apply(IEvent e);
}

public class Customer : AggregateRoot
{
    protected override void Apply(IEvent e)
    {
        if (e is CustomerCreatedEvent)
        {
            Console.WriteLine("CustomerCreatedEvent");
        }
    }
}

public class Product : AggregateRoot
{
    protected override void Apply(IEvent e)
    {
        if (e is ProductCreatedEvent)
        {
            Console.WriteLine("ProductCreatedEvent");
        }
    }
}

public interface IEvent
{
}

public class CustomerCreatedEvent : IEvent
{
}

public class ProductCreatedEvent : IEvent
{
}

但请注意,它具有以下缺点:

  • 方法需要非私有
  • 的参数类型应与Apply相同。 IEvent参数)-因此,我在Apply方法中添加了类型检查。

选项2:抽象方法和通用AggregateRoot

另一个选择是使AggregateRoot对IEvent类型通用,例如这样的东西。

using System;


public abstract class AggregateRoot<TEvent>
where TEvent : IEvent
{
    public void ApplyChange(TEvent @event)
    {
        Apply(@event); // how to call this method?
    }

    protected abstract void Apply(TEvent e);
}

public class Customer : AggregateRoot<CustomerCreatedEvent>
{
    protected override void Apply(CustomerCreatedEvent e)
    {
        Console.WriteLine("CustomerCreatedEvent");

    }
}

public class Product : AggregateRoot<ProductCreatedEvent>
{
    protected override void Apply(ProductCreatedEvent e)
    {
        Console.WriteLine("ProductCreatedEvent");
    }

}

public interface IEvent
{
}

public class CustomerCreatedEvent : IEvent
{
}

public class ProductCreatedEvent : IEvent
{
}

注意在这种情况下,我也更改了ApplyChange。

如果这些方法不能解决您的问题,请详细说明您要归档的内容,否则将是XY问题

暂无
暂无

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

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