查找所有程序集依赖项,反射器风格
我正在使用 Reflection.Emit 即时生成程序集,然后保存它。 它包含一个 Type 和一个静态 Main() 方法。
.NET 足以自动引用所需的程序集。 但是,在 Main() 中,从另一个程序集调用了方法,并且不会以标准方式引用它。
当执行程序集时,运行时寻找这个程序集却找不到,这是一个问题。
Reflector 可以检测到这一点,并在“取决于”列表下显示这个额外的程序集。 如何使用 Reflection API 检索这些隐式依赖项?
谢谢
I am generating an Assembly on the fly using Reflection.Emit and then saving it.
It contains one Type and a static Main() method in it.
.NET is kind enough to automatically reference a required assembly.
However, in Main(), there is a call to a method from another assembly and it doesn't get referenced in the standard way.
When the assembly is executed, the runtime looks for this assembly and cannot find it, which is a problem.
Reflector can detect this and shows this extra assembly under the "depends" list.
How can I retrieve these implicit dependencies using the Reflection API?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
感谢各位的回复,我已经成功解决了问题。
发生的情况如下:
AssemblyBuilder builder = ... // 生成程序集
builder.GetReferencedAssemblies(); => 即使我已经调用了 Save(),它也不会返回对方法主体中使用的程序集的引用 - 它似乎只返回已加载到内存中的程序集。
Assembly.ReflectionOnlyLoadFrom(文件名).GetReferencedAssemblies() => 工作正常
Thanks for the responses guys, I have managed to resolve the problem.
Here's what happens:
AssemblyBuilder builder = ... // generate assembly
builder.GetReferencedAssemblies(); => It will NOT return a reference to the assembly used in the method body even though I have called Save() already - it seems to return only the assemblies loaded in memory already.
Assembly.ReflectionOnlyLoadFrom(filename).GetReferencedAssemblies() => works fine
您尝试过Assembly.GetReferencedAssemblies吗? 它返回引用的程序集的 AssemblyName。
Have you tried Assembly.GetReferencedAssemblies? It returns the AssemblyName of the referenced assemblies.
嗯...显然,System.Type 的
Assembly
属性返回定义类型的程序集。如果您完全无法控制/了解 Main() 方法中的 IL,则必须解析刚刚生成的 IL 并检查提到的所有类型是否都存在。
更现实的是,手动确保引用 call 和 callvirt 发出中涉及的所有类型。
Hm... The
Assembly
property of System.Type returns the assembly that defines the type, obviously.If you have absolutely no control/knowledge about the IL in that Main() method, you'd have to parse the IL you just generated and check if all types mentioned are present.
Much more realistic, is to manually ensure all types involved in the emission of call and callvirt are referenced.