【问题标题】:Caliburn.Micro 1.5.2 to 2.0.0 breaking changes for BootstrapperBaseCaliburn.Micro 1.5.2 到 2.0.0 对 BootstrapperBase 的重大更改
【发布时间】:2014-08-08 11:47:03
【问题描述】:

我正在尝试将一些项目引入 Caliburn Micro 2.0.0 (CB.M),但我发现我的代码与引导相关的静默失败。

这是因为我需要在引导程序中获取我的应用程序的可执行程序集。

更一般地说,我注意到当例如尝试按照here 的描述编写 MEFBoottrapper,调用AssemblySource.Instance 将在 CB.M 的 1.x 和 2.0 版本之间产生不同的结果

使用 CB.M 1.5.2,AssemblySource.Instance 将包含正在执行的程序集。

使用 CB.M 2.0.0,AssemblySource.Instance 将包含定义自定义引导程序的程序集(它在不同于启动项目的项目中定义),因此是一个 dll。

从 1.5.2 迁移到 2.0.0 here on the CB.M homepage 的迁移指南提到了一些更明显的重大变化,但与上述无关。

如果有人对 CB.M 有深入的了解并可以对此发表评论,或者指出更详尽的重大变更概述,那将非常有用。

【问题讨论】:

  • 不知道为什么这会被否决。
  • 我也不是,我可以理解初始版本的质量不达标,但特别是近距离投票的原因很奇怪。这个问题永远不会被误认为与“专业的服务器或网络相关的基础设施管理”有关。但是,嘿,我学到了一些东西,我相信有一天答案可能对某人有用:)

标签: c# mvvm caliburn.micro


【解决方案1】:

仔细对比1.x和2.0的源码后发现方法:

protected virtual IEnumerable<Assembly> SelectAssemblies()

BootstrapperBase 发生了一些变化。

// Decompiled with JetBrains decompiler
// Type: Caliburn.Micro.BootstrapperBase
// Assembly: Caliburn.Micro, Version=1.5.2.0, Culture=neutral, PublicKeyToken=8e5891231f2ed21f
// MVID: DC6F950D-BBB2-4CAB-9754-D5C81FE2659F
// Assembly location: ..\bin\Debug\Caliburn.Micro.dll
if (Execute.InDesignMode)
  {
    AppDomain currentDomain = AppDomain.CurrentDomain;
    Assembly assembly = Enumerable.LastOrDefault<Assembly>((IEnumerable<Assembly>) (currentDomain.GetType().GetMethod("GetAssemblies").Invoke((object) currentDomain, (object[]) null) as Assembly[] ?? new Assembly[0]), new Func<Assembly, bool>(BootstrapperBase.ContainsApplicationClass));
    if (assembly == (Assembly) null)
      return (IEnumerable<Assembly>) new Assembly[0];
    return (IEnumerable<Assembly>) new Assembly[1]
    {
      assembly
    };
  }
  else
  {
    Assembly entryAssembly = Assembly.GetEntryAssembly();
    if (entryAssembly == (Assembly) null)
      return (IEnumerable<Assembly>) new Assembly[0];
    return (IEnumerable<Assembly>) new Assembly[1]
    {
      entryAssembly
    };
  }

而对于 2.0 (Bootstrapper.cs):

    /// <summary>
    /// Inherit from this class in order to customize the configuration of the framework.
    /// </summary>
    public abstract class BootstrapperBase {

    ... left out for brevity

    /// <summary>
    /// Override to tell the framework where to find assemblies to inspect for views, etc.
    /// </summary>
    /// <returns>A list of assemblies to inspect.</returns>
    protected virtual IEnumerable<Assembly> SelectAssemblies() {
        return new[] { GetType().Assembly };
    }

我遇到了这个问题,因为我依靠 SelectAssemblies() 的能力将可执行程序集返回给我以供下游使用。

我可以通过这样覆盖来解决问题(出于我的目的,我需要 exe 程序集):

protected override IEnumerable<System.Reflection.Assembly> SelectAssemblies()
{
    return new[] { Assembly.GetEntryAssembly() };
}

欢迎了解 CB.M 团队为何更改此方法的人士发表评论。

【讨论】:

  • 更改可能是因为将片段移动到 PCL 中。
  • 意味着,在这种情况下,可执行文件的概念并不总是有意义(在 WP 上,...)?所以只应该返回包含引导程序的实际程序集,更有意义吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-17
  • 1970-01-01
  • 2012-01-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多