如何按名称获取 C#.Net 程序集?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1913057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get C#.Net Assembly by name?
提问by Jronny
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()
,我们可以直接获取特定程序集,而不是循环遍历。
采纳答案by cyberzed
Have you tried looking at Assembly.Load(...)?
您是否尝试过查看Assembly.Load(...)?
回答by Maximilian Mayerl
回答by EMP
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). 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.
如果您只想获取程序集,那么您应该调用System.Reflection.Assembly.Load()
(正如已经指出的那样)。这是因为 .NET 会自动检查程序集是否已加载到当前 AppDomain 中,如果已加载,则不会再次加载它。
If you're just trying to checkwhether 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.
您可能想要循环的另一个原因是您只知道某些程序集信息(例如,您不确定版本)。也就是说,您知道的足够多,可以“看到就认出来”,但还不足以加载它。不过,这是一个相当模糊且不太可能的情况。
回答by Mathieu Frenette
For those who just need to access the assembly's metadata (version, etc.) check out Assembly.ReflectionOnlyLoad(name), which is able to load only the metadata, possibly saving on memory and IO.
对于那些只需要访问程序集元数据(版本等)的人,请查看 Assembly.ReflectionOnlyLoad(name),它只能加载元数据,可能会节省内存和 IO。
回答by Fabio
I resolved with LINQ
我用 LINQ 解决了
Assembly GetAssemblyByName(string name)
{
return AppDomain.CurrentDomain.GetAssemblies().
SingleOrDefault(assembly => assembly.GetName().Name == name);
}
回答by Marcell Toth
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 SomeAssemblyis loaded into the current AppDomain the method will return it, otherwise it will return null
.
如果SomeAssembly加载到当前 AppDomain 中,该方法将返回它,否则将返回null
.
回答by Dave Cousineau
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;