如何判断.NET程序集是否动态?

时间:2022-03-28 07:09:05

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?

在迭代一组程序集时,例如AppDomain.CurrentDomain.GetAssemblies(),如果您尝试访问CodeBase等属性,动态程序集将抛出NotSuportedException。如何在不触发和捕获NotSupportedException的情况下判断程序集是否是动态的?

3 个解决方案

#1


41  

To check if the assembly is dynamic:

要检查装配是否是动态的:

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

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

我花了一段时间才弄明白,所以在这里被问及回答。

Update:

更新:

In .NET 4.0, there is now a property:

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

if (assembly.IsDynamic)

#2


19  

In .NET 4 you can also check the Assembly.IsDynamic property.

在.NET 4中,您还可以检查Assembly.IsDynamic属性。

#3


0  

Prior to .NET Framework 4, the simplest solution seems to be to check if the Assembly is of type System.Reflection.Emit.AssemblyBuilder. This is the solution we use on our team.

在.NET Framework 4之前,最简单的解决方案似乎是检查程序集是否为System.Reflection.Emit.AssemblyBuilder类型。这是我们在团队中使用的解决方案。

If you take a look at the AssemblyBuilder's CodeBase property implementation, it simply throws an exception, regardless of anything else. AssemblyBuilder is also a sealed class, so it's impossible for a derived class to change this behavior. So, if you have an AssemblyBuilder object, you can be certain that you can never call CodeBase or GetManifestResourceStream or a bunch of other methods.

如果你看看AssemblyBuilder的CodeBase属性实现,它只会引发异常,无论其他什么。 AssemblyBuilder也是一个密封类,因此派生类不可能改变这种行为。因此,如果你有一个AssemblyBuilder对象,你可以确定你永远不能调用CodeBase或GetManifestResourceStream或其他一些方法。

public override string CodeBase
{
    get
    {
        throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicAssembly"));
    }
}

And in .NET Framework 4, checking the Assembly.IsDynamic property should be preferable because it's more legible and perhaps more future-proof, in case some new class comes along that overrides IsDynamic. Since AssemblyBuilder.IsDynamic always returns true, this is more evidence that an AssemblyBuilder object is always equivalent to a "dynamic dll".

在.NET Framework 4中,检查Assembly.IsDynamic属性应该是更可取的,因为它更易读,并且可能更具未来性,以防某些新类出现覆盖IsDynamic。由于AssemblyBuilder.IsDynamic始终返回true,因此更多证据表明AssemblyBuilder对象始终等效于“动态dll”。

Here's the .NET 4 AssemblyBuilder's IsDynamic:

这是.NET 4 AssemblyBuilder的IsDynamic:

public override bool IsDynamic
{ 
    get {
        return true; 
    } 
}

#1


41  

To check if the assembly is dynamic:

要检查装配是否是动态的:

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

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

我花了一段时间才弄明白,所以在这里被问及回答。

Update:

更新:

In .NET 4.0, there is now a property:

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

if (assembly.IsDynamic)

#2


19  

In .NET 4 you can also check the Assembly.IsDynamic property.

在.NET 4中,您还可以检查Assembly.IsDynamic属性。

#3


0  

Prior to .NET Framework 4, the simplest solution seems to be to check if the Assembly is of type System.Reflection.Emit.AssemblyBuilder. This is the solution we use on our team.

在.NET Framework 4之前,最简单的解决方案似乎是检查程序集是否为System.Reflection.Emit.AssemblyBuilder类型。这是我们在团队中使用的解决方案。

If you take a look at the AssemblyBuilder's CodeBase property implementation, it simply throws an exception, regardless of anything else. AssemblyBuilder is also a sealed class, so it's impossible for a derived class to change this behavior. So, if you have an AssemblyBuilder object, you can be certain that you can never call CodeBase or GetManifestResourceStream or a bunch of other methods.

如果你看看AssemblyBuilder的CodeBase属性实现,它只会引发异常,无论其他什么。 AssemblyBuilder也是一个密封类,因此派生类不可能改变这种行为。因此,如果你有一个AssemblyBuilder对象,你可以确定你永远不能调用CodeBase或GetManifestResourceStream或其他一些方法。

public override string CodeBase
{
    get
    {
        throw new NotSupportedException(Environment.GetResourceString("NotSupported_DynamicAssembly"));
    }
}

And in .NET Framework 4, checking the Assembly.IsDynamic property should be preferable because it's more legible and perhaps more future-proof, in case some new class comes along that overrides IsDynamic. Since AssemblyBuilder.IsDynamic always returns true, this is more evidence that an AssemblyBuilder object is always equivalent to a "dynamic dll".

在.NET Framework 4中,检查Assembly.IsDynamic属性应该是更可取的,因为它更易读,并且可能更具未来性,以防某些新类出现覆盖IsDynamic。由于AssemblyBuilder.IsDynamic始终返回true,因此更多证据表明AssemblyBuilder对象始终等效于“动态dll”。

Here's the .NET 4 AssemblyBuilder's IsDynamic:

这是.NET 4 AssemblyBuilder的IsDynamic:

public override bool IsDynamic
{ 
    get {
        return true; 
    } 
}