Silverlight 是否可以在不加载完整程序集的情况下读取程序集清单?
观察以下 Silverlight 代码:
foreach (AssemblyPart ap in Deployment.Current.Parts)
{
var sri = Application.GetResourceStream(new Uri(ap.Source, UriKind.Relative));
var assembly = new AssemblyPart().Load(sri.Stream);
// Do something with the assembly.
}
它迭代应用程序可用的所有程序集并对它们执行某些操作。这段代码的问题在于,即使只是读取程序集清单,代码也会将完整的程序集加载到进程空间中。
是否有可能以某种方式优化它?
我有类似的 .NET 代码,它使用 PostSharp.Core 库来读取程序集清单,而不加载完整的程序集(Mono.Cecil 也可以完成这项工作)。
预先感谢所有好心的撒玛利亚人。
Observe the following piece of Silverlight code:
foreach (AssemblyPart ap in Deployment.Current.Parts)
{
var sri = Application.GetResourceStream(new Uri(ap.Source, UriKind.Relative));
var assembly = new AssemblyPart().Load(sri.Stream);
// Do something with the assembly.
}
It iterates over all the assemblies available to the application and does something on them. The problem with this code is that even if that something is just reading the assembly manifest, the code loads the complete assemblies into the process space.
Is it possible to optimize it somehow?
I have similar .NET code, which uses PostSharp.Core library to just read the assembly manifests, without loading the complete assemblies (Mono.Cecil does the job as well).
Thanks in advance to all the good Samaritans out there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如您可以在任何 .NET 代码中执行的操作:
编辑添加
这里的技巧是将
mscorlib [2.0.5.0]
的引用替换为mscorlib [2.0] .0.0]
来实现您所需要的。但是,如果您创建一个单独的程序集来执行此操作,效果会更好,因为替换引用可能会搞乱代码的其他部分。
As you can do in any .NET code:
Edited to add
The trick here is to replace the reference to
mscorlib [2.0.5.0]
with themscorlib [2.0.0.0]
to achieve what you need.However, it'd be better if you create a separated assembly in order to do this, because replacing the reference might screw up other parts of your code.
silverlight 框架中没有内置方法可以执行此操作。您可以尝试查看类似 Cecil 或 Microsoft 的通用编译器基础结构 - 元数据 项目尝试在 SL 中运行这些项目。这样做肯定需要重新编译,而且我不确定是否有任何依赖关系。除此之外,您将不得不实现自己的代码来解析汇编字节。这样做不适合胆小的人,并且需要与我提到的两个项目大量重复源代码。
There is no built in way to do this in the silverlight framework. You could try looking at something like Cecil or Microsoft's Common Compiler Infrastructure - Metadata projects to try to run these in SL. Doing so would certainly require recompiling either and I'm unsure of any dependencies. Short of that, you'd be left with implementing your own code that would parse the assembly bytes. Doing this is not for the faint of heart, and would require significant duplication of source with the 2 projects I mention.