识别 Reflection.Emit 生成的程序集
有没有一种简单的方法来识别 Reflection.Emit 生成的程序集?处理加载到应用程序域中的所有程序集时,动态生成的程序集的 Assembly
实例的行为与标准程序集的行为不同。例如,访问CodeBase
属性会导致异常:
string codeBase;
try
{
codeBase = assembly.CodeBase;
}
catch(NotSupportedException)
{
// assemblies generated via Reflection.Emit throw an exception when CodeBase is accessed
codeBase = null;
}
是否有更好的方法来识别这种情况并避免try ... catch
块?
Is there a simple way to identify Reflection.Emit-generated assemblies? When processing all assemblies loaded into an application domain, Assembly
instances of dynamically generated assemblies don't behave the same as for standard assemblies. For example, accessing the CodeBase
property leads to an exception:
string codeBase;
try
{
codeBase = assembly.CodeBase;
}
catch(NotSupportedException)
{
// assemblies generated via Reflection.Emit throw an exception when CodeBase is accessed
codeBase = null;
}
Is there a better way to recognize this situation and avoid the try … catch
block?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Assembly.IsDynamic 没有回答您的问题吗?这可能是.Net 4.0中的新内容。
Doesn't Assembly.IsDynamic answer your question? It may be that it is new in .Net 4.0.
这应该有效:
This should work: