简体   繁体   English

如何在不知道C#.net名称的情况下从文件夹加载所有dll?

[英]How can I load all dlls from a folder without knowing their names in c# .net?

Previously I was using this call to load all cs file that extends from Rule class 以前我使用此调用来加载从Rule类扩展的所有cs文件

var repository = new RuleRepository();
repository.Load(x => x.From(typeof(Rule1).Assembly));

By calling Load method as shown above all the class files of the same type as Rule1.cs (meaning all files extending from Rule class) get loaded into repository memory. 通过如上所示调用Load方法,将所有与Rule1.cs类型相同的类文件(意味着从Rule类扩展的所有文件)加载到存储库内存中。 Currently I have decided to convert all these .cs files (ie Rule1.cs) into dlls and scan the folder containing these dlls. 目前,我已决定将所有这些.cs文件(即Rule1.cs)转换为dll,并扫描包含这些dll的文件夹。 How can I achieve this behavior? 我该如何实现这种行为? Currently I am doing something like this 目前我正在做这样的事情

Assembly assembly1 = Assembly.LoadFile(Server.MapPath("Rule1.dll"));
List<Assembly> asmblyList = new List<Assembly>();
asmblyList.Add(assembly1);
repository.Load(x => x.From(asmblyList));

I want to scan all the assemblies of type Rule1.dll from the folder. 我想从该文件夹中扫描Rule1.dll类型的所有程序集。 How can I possibly do it? 我该怎么办? Any help would be great. 任何帮助都会很棒。

As in the comments mentioned, get a list of files and load them is not a problem, but there is only one way to drop a loaded assembly out and this is to unload the whole AppDomain. 就像在评论中提到的那样,获取文件列表并加载它们不是问题,但是只有一种方法可以删除已加载的程序集,这就是卸载整个AppDomain。 Have a look at this example: 看一下这个例子:

static void Main(string[] args)
{
    var path = AssemblyDirectory + @"\external\";
    var files = Directory.GetFiles(path); //get all files

    var ad = AppDomain.CreateDomain("ProbingDomain"); //create another AppDomain
    var tunnel = (AppDomainTunnel)
        ad.CreateInstanceAndUnwrap(typeof (AppDomainTunnel).Assembly.FullName,
        typeof (AppDomainTunnel).FullName); //create tunnel

    var valid = tunnel.GetValidFiles(files); //pass file paths, get valid ones back
    foreach (var file in valid)
    {
        var asm = Assembly.LoadFile(file); //load valid assembly into the main AppDomain
        //do something
    }

    AppDomain.Unload(ad); //unload probing AppDomain
}

private class AppDomainTunnel : MarshalByRefObject 
{   
    public string[] GetValidFiles(string[] files) //this will run in the probing AppDomain
    {
        var valid = new List<string>();
        foreach (var file in files)
        {
            try
            {   //try to load and search for valid types
                var asm = Assembly.LoadFile(file);
                if (asm.GetTypes().Any(x => x.IsSubclassOf(typeof (Rule1))))
                    valid.Add(file); //valid assembly found
            }
            catch (Exception)
            {
                //ignore unloadable files (non .Net, etc.)
            }
        }
        return valid.ToArray();
    }
}
//found here: http://stackoverflow.com/a/283917/4035472
public static string AssemblyDirectory
{
    get
    {
        string codeBase = Assembly.GetExecutingAssembly().CodeBase;
        UriBuilder uri = new UriBuilder(codeBase);
        string path = Uri.UnescapeDataString(uri.Path);
        return Path.GetDirectoryName(path);
    }
}

暂无
暂无

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

相关问题 如何在不知道任何使用C#的机器名称的情况下找到LAN上所有共享的名称? - How can I find the names of all shares on a LAN without knowing any machine names using C#? 在不知道所有属性名称的情况下,如何在C#中反序列化JSON数据(使用DataContractJsonSerializer)? - How can you deserialize JSON data in C# (using DataContractJsonSerializer) without knowing all property names? 如何将所有引用的 DLL 移动到 c# 中的单独文件夹中? - How to move all referenced DLLs into seperate folder in c#? 如何使用C#在不知道其名称的情况下从XML读取子节点? - How to read child nodes from XML without knowing their names using C#? 如何在不知道 URL 的情况下获得在 C# 中加载网页的时间? - How to get the time to load a webpage in C# without knowing the URL? C#加载文件夹名称 - C# load folder names 我如何调试在外部C#应用程序中切换的C ++ DLL(无法从调试器启动) - How can i debug C++ DLLs SWIGed in external C# app (that can not be started from debugger) 如何在不使用C#知道密码的情况下检查项目的TFS 2010权限? - How can I check for the TFS 2010 permissions of a project without knowing password using C#? 如何将 C# DLL 包装到 Ubuntu 上的 NuGet package? - How can I wrap C# DLLs into a NuGet package on Ubuntu? 如何在LabVIEW 2012中从C#.DLL获取返回值? - How can I get return values from C# .DLLs in LabVIEW 2012?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM