Silverlight 是否可以在不加载完整程序集的情况下读取程序集清单?

发布于 2024-08-17 23:03:24 字数 492 浏览 2 评论 0原文

观察以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青芜 2024-08-24 23:03:24

正如您可以在任何 .NET 代码中执行的操作:

foreach (AssemblyPart ap in Deployment.Current.Parts)
{
    byte[] buffer = new byte[1024];
    List<byte> assemblyBytes = new List<byte>();

    using (var sri = Application.GetResourceStream(new Uri(ap.Source, UriKind.Relative)).Stream)
    {
        int read = -1;
        do
        {
            read = sri.Read(buffer, 0, buffer.Length);
            if (read != -1)
            {
                Array.Resize(ref buffer, read);
                assemblyBytes.AddRange(buffer);
            }
        }
        while (read != -1);
    }

    var assembly = Assembly.ReflectionOnlyLoad(assemblyBytes.ToArray());
    // Do something with the assembly.     
}

编辑添加

这里的技巧是将 mscorlib [2.0.5.0] 的引用替换为 mscorlib [2.0] .0.0] 来实现您所需要的。

但是,如果您创建一个单独的程序集来执行此操作,效果会更好,因为替换引用可能会搞乱代码的其他部分。

As you can do in any .NET code:

foreach (AssemblyPart ap in Deployment.Current.Parts)
{
    byte[] buffer = new byte[1024];
    List<byte> assemblyBytes = new List<byte>();

    using (var sri = Application.GetResourceStream(new Uri(ap.Source, UriKind.Relative)).Stream)
    {
        int read = -1;
        do
        {
            read = sri.Read(buffer, 0, buffer.Length);
            if (read != -1)
            {
                Array.Resize(ref buffer, read);
                assemblyBytes.AddRange(buffer);
            }
        }
        while (read != -1);
    }

    var assembly = Assembly.ReflectionOnlyLoad(assemblyBytes.ToArray());
    // Do something with the assembly.     
}

Edited to add

The trick here is to replace the reference to mscorlib [2.0.5.0] with the mscorlib [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.

颜漓半夏 2024-08-24 23:03:24

silverlight 框架中没有内置方法可以执行此操作。您可以尝试查看类似 CecilMicrosoft 的通用编译器基础结构 - 元数据 项目尝试在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文