Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 加载从bin中的程序集继承特定接口的类型_C#_Asp.net Mvc_Reflection_.net 4.0 - Fatal编程技术网

C# 加载从bin中的程序集继承特定接口的类型

C# 加载从bin中的程序集继承特定接口的类型,c#,asp.net-mvc,reflection,.net-4.0,C#,Asp.net Mvc,Reflection,.net 4.0,我正在为ASP.NET MVC应用程序编写代码,该应用程序启动时将执行以下操作: 加载应用程序bin目录中的所有程序集 从每个程序集中获取从接口派生的所有类型(ITask) 对每种类型调用Execute()方法 这是我目前的想法。此方法将在ApplicationStarted()上的中调用。: private void ExecuteTasks() { List startupTasks=新建列表(); Assembly asm=this.ExecutingAssembly; //获取执行(

我正在为ASP.NET MVC应用程序编写代码,该应用程序启动时将执行以下操作:

  • 加载应用程序bin目录中的所有程序集
  • 从每个程序集中获取从接口派生的所有类型(
    ITask
  • 对每种类型调用
    Execute()
    方法
这是我目前的想法。此方法将在ApplicationStarted()上的
中调用。

private void ExecuteTasks()
{
List startupTasks=新建列表();
Assembly asm=this.ExecutingAssembly;
//获取执行(bin)文件夹的路径
字符串codeBase=this.ExecutingAssembly.codeBase;
UriBuilder uri=新的UriBuilder(代码库);
字符串路径=Uri.UnescapeDataString(Uri.path);
string bin=Path.GetDirectoryName(路径);
string[]assemblies=Directory.GetFiles(bin,*.dll”);
foreach(程序集中的字符串文件)
{
尝试
{
如果(File.Exists(File))
{
//加载程序集
asm=Assembly.LoadFrom(文件);
//从继承ITask的程序集中获取所有类型
var query=来自asm.GetTypes()中的t
t.IsClass在哪里&&
t、 GetInterface(typeof(ITask).FullName)!=null
选择t;
//将类型添加到启动任务列表中
foreach(查询中的类型)
{
添加((ITask)Activator.CreateInstance(类型));
}
}
}
捕获(例外情况除外)
{
例外情况。LogException(ex);
}
}
//执行每个启动任务
foreach(startupTasks中的ITask任务)
{
task.Execute();
}
}
我的问题:是否有更好的方法来执行这些步骤?获取bin目录的方法取自以下答案:。做一些简单的事情似乎需要做很多工作,但我想不出一个更简单的方法


另外,使用
System.Activator
创建实例,然后在每个实例上调用
Execute()
方法是否是执行该步骤的最有效方法?

您可能能够清理代码,但如果没有任何扩展库,代码不会变得更短


关于性能,我不太担心如何优化应用程序启动的任务,特别是,希望不会经常调用它,并且在它启动并运行后不会影响您的站点。

您如何管理任务的执行顺序?没有实现什么,因为顺序应该无关紧要。每个任务执行的内容应独立于所有其他任务。
    private void ExecuteTasks()
    {
        List<ITask> startupTasks = new List<ITask>();
        Assembly asm = this.ExecutingAssembly;

        // get path of executing (bin) folder 
        string codeBase = this.ExecutingAssembly.CodeBase;
        UriBuilder uri = new UriBuilder(codeBase);
        string path = Uri.UnescapeDataString(uri.Path);
        string bin = Path.GetDirectoryName(path);
        string[] assemblies = Directory.GetFiles(bin, "*.dll");

        foreach (String file in assemblies)
        {
            try
            {
                if (File.Exists(file))
                {
                    // load the assembly
                    asm = Assembly.LoadFrom(file);

                    // get all types from the assembly that inherit ITask
                    var query = from t in asm.GetTypes()
                                where t.IsClass &&
t.GetInterface(typeof(ITask).FullName) != null
                                select t;

                    // add types to list of startup tasks
                    foreach (Type type in query)
                    {
                        startupTasks.Add((ITask)Activator.CreateInstance(type));
                    }
                }
            }
            catch (Exception ex)
            {
                Exceptions.LogException(ex);
            }
        }

        // execute each startup task
        foreach (ITask task in startupTasks)
        {
            task.Execute();
        }
    }