Castle Windsor:如何指定运行时值作为参数(例如从静态函数调用返回的值)

2023-12-08

我想执行这个CODEcastle xml 配置文件中的等效项。

// Foo(字符串名称)

IFoo f = new Foo(StaticBarClass.Name);




XML

现在对于 XML,除了参数部分内的内容之外,我什么都知道(例如,废话)。

参数部分会是什么样子?

<component id="blah"
           service="blah"
           type="blah">
  <parameters>
    <name>StaticBarClas.Name_THAT_I_NEED_HELP_WITH</name>
  </parameters>

您可以使用的一种方法是用您自己的变体替换配置参数检查器,该变体可以引入一些额外的行为 - 这是一个快速原型:

public class ExtendedConfigurationParametersInspector : IContributeComponentModelConstruction
{
  #region IContributeComponentModelConstruction Members

  public virtual void ProcessModel(IKernel kernel, ComponentModel model)
  {
    if (model.Configuration == null) return;

    IConfiguration parameters = model.Configuration.Children["parameters"];

    if (parameters == null) return;

    foreach (IConfiguration parameter in parameters.Children)
    {
      String name = parameter.Name;
      String value = parameter.Value;

      if (value == null && parameter.Children.Count != 0)
      {
        IConfiguration parameterValue = parameter.Children[0];
        model.Parameters.Add(name, parameterValue);
      }
      else
      {
        if (parameter.Attributes["type"] == "static")
        {
          int lastIndex = parameter.Value.LastIndexOf(".");
          string typeName = parameter.Value.Substring(0, lastIndex);
          string field = parameter.Value.Substring(lastIndex + 1);
          Type ownerType = Type.GetType(typeName);
          FieldInfo valueField = ownerType.GetField(field);
          value = (string) valueField.GetValue(null);
        }

        model.Parameters.Add(name, value);
      }
    }

    foreach (ParameterModel parameter in model.Parameters)
    {
      if (parameter.Value == null || !ReferenceExpressionUtil.IsReference(parameter.Value))
      {
        continue;
      }

      String newKey = ReferenceExpressionUtil.ExtractComponentKey(parameter.Value);

      model.Dependencies.Add(new DependencyModel(DependencyType.ServiceOverride, newKey, null, false));
    }
  }

  #endregion
}

public class ExtendedComponentBuilder : DefaultComponentModelBuilder
{
  public ExtendedComponentBuilder(IKernel kernel) : base(kernel)
  {
  }

  protected override void InitializeContributors()
  {
    AddContributor(new GenericInspector());
    AddContributor(new ConfigurationModelInspector());
    AddContributor(new ExtendedConfigurationParametersInspector());
    AddContributor(new LifestyleModelInspector());
    AddContributor(new ConstructorDependenciesModelInspector());
    AddContributor(new PropertiesDependenciesModelInspector());
    AddContributor(new LifecycleModelInspector());
    AddContributor(new InterceptorInspector());
    AddContributor(new ComponentActivatorInspector());
    AddContributor(new ComponentProxyInspector());
  }
}

public class ExtendedWindsorContainer : WindsorContainer
{
  public ExtendedWindsorContainer(IConfigurationInterpreter interpreter)
    : base(CreateKernel(), new Castle.Windsor.Installer.DefaultComponentInstaller())
  {
    if (interpreter == null) throw new ArgumentNullException("interpreter");

    interpreter.ProcessResource(interpreter.Source, Kernel.ConfigurationStore);

    RunInstaller();
  }

  private static IKernel CreateKernel()
  {
    DefaultKernel kernel = new DefaultKernel();
    kernel.ComponentModelBuilder = new ExtendedComponentBuilder(kernel);
    return kernel;
  }
}

然后,您可以像这样连接容器中的属性,其中为参数指定“静态”类型将导致该值被参数值引用的静态字段替换。

<castle>
  <components>
    <component id="test"
           type="SomeNamespace.TestComponent,Example">
      <parameters>
        <value type="static">SomeNamespace.SomeClass.TheStaticFieldValue</value>
      </parameters>
    </component>
  </components>
</castle>

不幸的是,由于组件模型中的参数是不可变的,您通常无法通过更简单的方法(例如模型创建内核事件)来做到这一点。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Castle Windsor:如何指定运行时值作为参数(例如从静态函数调用返回的值) 的相关文章

  • 初始化参数时会发生什么? C++

    void foo int i int k 7 cout lt lt k int main foo 1 2 k将输出2 我的问题是 foo 按什么顺序初始化参数并获取参数 foo 得到 2 的过程是什么 谢谢 void foo int i i
  • PowerShell 参数 - “术语‘param’未被识别为 cmdlet 的名称”

    考虑 notepad Starts Notepad Get Process notepad Finds the processes named notepad param Parameter Mandatory true string Pr
  • Maven:无法在 OS X 上找到 java.lang 问题

    当我尝试时遇到以下问题mvn clean install显然它无法找到运行时 jar 但我需要做什么 错误日志 ERROR COMPILATION ERROR INFO ERROR Failure executing javac but c
  • C++ 仪器(诊断)库

    我正在考虑向我的应用程序添加代码 以收集诊断信息以供以后检查 是否有为此目的创建的 C 库 我想做的与分析类似 但又不一样 因为收集的数据将更多地用于调试而不是分析 EDIT 平台 Linux要收集的诊断信息 由应用程序逻辑 各种断言和统计
  • 运行时嵌套循环的数量

    我正在尝试输出一组整数从 1 到 max 的所有可能的唯一整数组合 因此 对于 3 个整数且最多 4 个整数 我会得到 123 124 134 234 我正在使用嵌套的 for 循环来执行此操作 但我希望允许用户在运行时输入整数的数量 现在
  • 具有重复符号的 C++ 插件库上的段错误

    我有一个跨平台 C 应用程序 它分为多个共享库 并从插件共享库加载附加功能 插件库应该是自包含的并自行运行 无需了解或依赖于调用应用程序 其中一个插件包含从主应用程序复制的代码 因此包含与引擎中的符号名称重复的符号名称 是的 我知道这通常是
  • In 和 Out 属性在 .NET 中如何工作?

    我一直在尝试跨序列化数组AppDomain边界 使用以下代码 public int Read byte buffer int offset int count return base Read buffer offset count 作为猜
  • 为什么我不能在接收数组参数的函数中使用 SetLength?

    我正在尝试使用以下函数来设置动态数组 即 var 参数 的长度 当我尝试编译代码时只有一个错误 dcc64 错误 lolcode dpr 138 E2008 不兼容类型 function execute var command array
  • 为什么没有参数的函数(与实际函数定义相比)可以编译?

    我刚刚看到某人的 C 代码 我很困惑为什么要编译它 有两点我不明白 The 函数原型与实际函数定义相比没有参数 中的参数函数定义没有类型 include
  • C++ 在运行时分配 const 值?

    我有一个在运行时永远不会改变的恒定值 但在运行时之前不可能知道 有没有一种方法可以在不定义常量的情况下声明常量 无论是否作为类的成员 并在确定后 且仅一次 分配一个计算值 或者我是否必须诉诸非常量声明并使用编码 S Ps ALL CAPS变
  • 如何将可变数量的参数传递给 SQL Server 存储过程?

    我将 SQL Server 2005 用于我的小型 Web 应用程序 我想将参数传递给 SP 但有一个条件 可以不时更改的参数数量 想一想 这次我传递姓名和地址 下次我传递姓名 地址 该参数范围可以是 1 30 您使用默认参数声明该过程 并
  • 在 C 中使用带有任意数量参数的函数的参数

    我刚刚读过 C 无效参数 https stackoverflow com questions 693788 c void arguments关于C中这些函数定义之间的差异 int f void and int f 我想知道第二种形式意味着返
  • $_GET 作为 PHP 函数中的参数

    我有同样的问题 但是 我根据使用标头的 if 语句将用户重定向到通过函数构造的动态页面 为了使该函数正常工作 需要在标头的 GET 部分中传递参数 根据提供的答案 这是一种不好的做法 我应该用什么方式来做呢 function page ti
  • 如何在Python中另一个类的函数中获取调用者类名?

    我的目标是模拟应用程序的序列图 为此我需要有关运行时调用者和被调用者类名的信息 我可以成功检索调用者函数 但无法获取调用者类名 Scenario caller py import inspect class A def Apple self
  • 将参数传递给 GWT bootstrap .nocache.js 脚本

    有没有办法将参数传递给 GWT 生成的 nocache js 脚本文件并在 onModuleLoad 函数中对其进行评估 就像这样 主机页 URL 应与内部工作的 GWT 内容完全分离 因此将 appId 参数作为主机页的查询参数传递并使用
  • 无法在 onclick 函数中传递多个参数

    我正在尝试创建一个上传机制 其中我可以使用 HTML 中的文件 ID 将文件上传到 google 驱动器中 我不想将文件夹的 ID 放在上传函数中 因为这是需要的 我试图通过声明另一个参数 即函数 upload e id 来传递函数 upl
  • Rails 3:使用 AJAX 请求更新 URL 参数

    我有一个过滤器和一个产品列表 id 名称 创建日期 我可以按 ID 名称或创建日期进行过滤 通过 AJAX 请求 我更新了内容 div 但显然 URL 没有改变 如何将参数附加到 URL 例如 localhost 3000 dashboar
  • 在ASP CLASSIC中使用SQL参数,对象定义不正确错误

    我试图使用参数保护我的 INSERT 语句免受 SQL 注入 但由于某种原因我收到错误 Parameter object is improperly defined Inconsistent or incomplete informatio
  • Oracle存储过程使用数组作为表插入的参数

    我一直在寻找一个明显的例子 但没有运气 抱歉 如果已经回答了 我正在尝试做一些非常简单的事情 一个存储过程 它将获取输入并将它们插入到表中 我希望它获取多行数组并一次全部插入 我认为这很简单 但我还没有找到一个可以展示我的例子 在很多例子中
  • Ruby 可选参数和多个参数

    我试图将方法的第一个参数设置为可选 后跟任意数量的参数 例如 def dothis value 0 args 我遇到的问题是 这似乎实际上不可能 当我打电话时dothis hey how are you good 我希望它将值设置为默认值

随机推荐