如何判断一个.NET程序集是动态的?如何判断、程序、动态、NET

2023-09-03 11:50:25 作者:故人何时归

在通过一系列的组件,如迭代AppDomain.CurrentDomain.GetAssemblies(),动态组件会如果您尝试访问诸如codeBase的物业抛出NotSuportedException。你如何判断一个程序集是动态的,而不会触发和捕捉NotSupportedException异常?

When iterating through a set of assemblies, e.g. AppDomain.CurrentDomain.GetAssemblies(), dynamic assemblies will throw a NotSuportedException if you try to access properties like CodeBase. How can you tell that an assembly is dynamic without triggering and catching the NotSupportedException?

推荐答案

要检查组件是动态的:

if (assembly.ManifestModule is System.Reflection.Emit.ModuleBuilder)

这花了我一段时间才能弄清楚,所以这里的提问和回答。

This took me a while to figure out, so here it is asked and answered.

更新:

在.NET 4.0中,现在有一个特性:

In .NET 4.0, there is now a property:

if (assembly.IsDynamic)