从ASP.NET中的Bin加载程序集 [英] Load an Assembly from Bin in ASP.NET

查看:83
本文介绍了从ASP.NET中的Bin加载程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我知道位于bin目录中的库,我有一个文件名,例如 Foo.dll。我想为其创建一个Assembly对象。我正在尝试从不是页面的类中实例化此对象,因此我没有Request对象来获取路径。如何获取需要使用Assembly.Load()的路径?

I have a file name, like "Foo.dll," for a library that I know is in the bin directory. I want to create an Assembly object for it. I'm trying to instantiate this object from a class that's not a page, so I don't have the Request object to get the path. How do I get the path I need to use Assembly.Load()?

推荐答案

Assembly.Load不需要文件路径,而是需要一个AssemblyName。如果您知道程序集位于标准搜索路径(即bin目录)中,则无需知道程序集的磁盘路径...只需要知道其程序集名称即可。对于程序集,假设您不需要特定的版本,区域性等,则程序集名称应仅为 Foo:

Assembly.Load should not require a file path, rather it requires an AssemblyName. If you know that your assembly is in the standard search path (i.e. the bin directory), you should not need to know the disk path of the assembly...you only need to know its assembly name. In the case of your assembly, assuming you don't need a specific version, culture, etc., the assembly name should just be "Foo":

Assembly fooAssembly = Assembly.Load("Foo");

如果确实需要加载特定版本,则可以执行以下操作:

If you do need to load a specific version, you would do the following:

Assembly fooAssembly = Assembly.Load("Foo, Version=1.1.2, Culture=neutral");

通常,您要使用Assembly.Load,而不是Assembly.LoadFrom或Assembly.LoadFile。 LoadFrom和LoadFile在标准融合过程之外工作,并且可能导致程序集被多次加载,从不安全的位置等加载。Assembly.Load执行标准加载,搜索标准程序集位置(例如bin,GAC)等等,并应用所有标准安全检查。

Generally, you want to use Assembly.Load, rather than Assembly.LoadFrom or Assembly.LoadFile. LoadFrom and LoadFile work outside of the standard fusion process, and can lead to assemblies being loaded more than once, loaded from insecure locations, etc. Assembly.Load performs a "standard" load, searching the standard assembly locations such as bin, the GAC, etc., and applies all the standard security checks.

这篇关于从ASP.NET中的Bin加载程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆