问题描述
你好
我有一个自定义工具(单个文件生成器),该工具需要加载项目的引用程序集以进行反射.如果我只是加载它,则程序集将被锁定,因此构建引用的项目将失败.据我了解,这是不可能建立的 默认应用程序域(甚至对于仅反射上下文)的卷影复制,因此唯一的解决方案似乎是创建一个新域,将程序集加载到该域中,然后在GenerateCode方法期间将其卸载.
I have a custom tool (single file generator), which needs to load project's referenced assemblies for reflection. If I just load it, the assemblies get locked, so building the referenced project then fails. As I have found out, it is impossible to set up shadow copying for the default app domain (even for the reflection only context), so the only solution seems to create a new domain, load the assemblies there and unload it during the GenerateCode method.
现在,如果我将自定义工具作为独立的应用程序运行,这对我有用,但是从Visual Studio中运行时它将失败.在自定义工具中使用应用程序域是否有任何限制?还是您会使用其他解决方案来检查引用的内容 组装?
Now this works for me if I run the custom tool as a standalone application, but it fails when run from the Visual Studio. Is there any restriction on working with the app domains in a custom tool? Or would you use any other solution to inspect the referenced assemblies?
感谢任何提示,
Jan
推荐答案
byte[] readAllBytes = File.ReadAllBytes("path");
Assembly assembly = Assembly.Load(readAllBytes);
但是,将程序集重复加载到默认的AppDomain中会泄漏内存,您可能仍想使用单独的AppDomain.或者,您可以使用反射以外的其他方式.有多种框架,其中之一是 FxCop中内置的自省引擎.这是一个示例:
However, loading assemblies repeatedly into the default AppDomain will leak memory, you may still want to use a separate AppDomain. Alternatively, you can use something other than Reflection. There are multiple frameworks out there, one of them is the Introspection engine built into FxCop. Here is an example:
http://www.olegsych.com/2007/12/how-to-use-t4-to-decorator-classs/
这篇关于自定义工具中的AppDomains的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!