c# - 有没有办法按顺序执行一个实现类

标签 c# .net assemblies structuremap

public interface IInterface<T>
{
 void Handle(T message);
}

public class A: IInterface<T>
{
  void Handle(T Message) {}
}

public class B: IInterface<T>
{
  void Handle(T Message) {}
}

客户:

        public void Execute<T>(T message)
        {  
            var commandHandlers = DependencyResolver.Current.GetServices(typeof(IInterface<T>)).Cast<IInterface<T>>();

// The above line will give a list of implementation class of IInterface.

            foreach (var commandHandler in commandHandlers)
            {
                commandHandler.Handle(message);
            }
        }

有没有办法先执行 B 类实现,然后再执行 A 类实现?

最佳答案

您可以添加订单属性来控制它:

public interface IInterface<T>
{
 void Handle(T message);
 int Order {get;}
}

public class A: IInterface<T>
{
  void Handle(T Message) {}
  public int Order => 1;
}

public class B: IInterface<T>
{
  void Handle(T Message) {}
  public int Order => 0;
}

foreach (var commandHandler in commandHandlers.OrderBy(o=>o.Order))
{
    commandHandler.Handle(message);
}

关于c# - 有没有办法按顺序执行一个实现类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51146366/

相关文章:

.NET 程序集差异/比较工具 - 有什么可用的?

c# - 客户端。我如何接收大消息?

c# - 如何检查应用程序是否处于调试或发布状态

c# - 如何固定通用 Span<T> 实例以使用 Parallel.For 对其进行处理?

c# - Linq Where 列表包含另一个列表中的项目

assemblies - 将汇编和调试符号(pdb 文件)添加到 GAC - Wix 安装程序

visual-studio-2008 - VS2008中的自动汇编版本号管理

c# - Global.asax 中的自动事件连接

c# - ASP.NET Web 应用程序的 SQL 性能不佳

c# - 分配通用异常 <TException> 一条消息