简体   繁体   English

如何按名称获取 C#.Net 程序集?

[英]How to get C#.Net Assembly by name?

Is there something like:有没有类似的东西:

AppDomain.CurrentDomain.GetAssemblyByName("TheAssemblyName")

so instead of looping through AppDomain.CurrentDomain.GetAssemblies() , we could just get the specific assembly directly.因此,我们可以直接获取特定的程序集,而不是通过AppDomain.CurrentDomain.GetAssemblies()循环。

您是否尝试过查看Assembly.Load(...)

I resolved with LINQ我用 LINQ 解决了

Assembly GetAssemblyByName(string name)
{
    return AppDomain.CurrentDomain.GetAssemblies().
           SingleOrDefault(assembly => assembly.GetName().Name == name);
}

It depends on what you're trying to accomplish.这取决于您要实现的目标。

If you just want to get the assembly, then you should call System.Reflection.Assembly.Load() (as already pointed out).如果您只想获取程序集,那么您应该调用System.Reflection.Assembly.Load() (正如已经指出的那样)。 That's because .NET automatically checks if the assembly has already been loaded into the current AppDomain and doesn't load it again if it has been.这是因为 .NET 会自动检查程序集是否已加载到当前 AppDomain 中,如果已加载,则不会再次加载它。

If you're just trying to check whether the assembly has been loaded or not (for some diagnostics reason, perhaps) then you do have to loop over all the loaded assemblies.如果您只是想检查程序集是否已加载(可能出于某种诊断原因),那么您必须遍历所有已加载的程序集。

Another reason you might want to loop is if you know only some of the assembly information (eg. you're not sure of the version).您可能想要循环的另一个原因是您只知道某些程序集信息(例如,您不确定版本)。 That is, you know enough to "recognise it when you see it", but not enough to load it.也就是说,您知道的足够多,可以“看到就认出来”,但还不足以加载它。 That is a fairly obscure and unlikely scenario, though.不过,这是一个相当模糊且不太可能的情况。

If this is an assembly you have referenced, I like to write a class like the following:如果这是您引用的程序集,我喜欢编写如下所示的类:

namespace MyLibrary {
   public static class MyLibraryAssembly {
      public static readonly Assembly Value = typeof(MyLibraryAssembly).Assembly;
   }
}

and then whenever you need a reference to that assembly:然后每当您需要对该程序集的引用时:

var assembly = MyLibraryAssembly.Value;

对于那些只需要访问程序集元数据(版本等)的人,请查看 Assembly.ReflectionOnlyLoad(name),它只能加载元数据,可能会节省内存和 IO。

You can write an extension method that does what you need.您可以编写一个扩展方法来满足您的需求。

This method will only enumerate loaded assemblies , if you possibly need to load it, use Assembly.Load from the accepted answer.此方法只会枚举已加载的程序集,如果您可能需要加载它,请使用已接受答案中的Assembly.Load

public static class AppDomainExtensions
{
    public static Assembly GetAssemblyByName(this AppDomain domain, string assemblyName)
    {
        return domain.GetAssemblies().FirstOrDefault(a => a.GetName().Name == assemblyName);
    }
}

Then you call this method on an AppDomain like this:然后你像这样在 AppDomain 上调用这个方法:

Assembly a = AppDomain.CurrentDomain.GetAssemblyByName("SomeAssembly")

If SomeAssembly is loaded into the current AppDomain the method will return it, otherwise it will return null .如果SomeAssembly加载到当前 AppDomain 中,该方法将返回它,否则将返回null

看看 System.Reflection.Assembly 类,特别是 Load 方法: MSDN

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

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