温莎城堡是否允许解析值类型?

发布于 2024-07-08 14:54:05 字数 954 浏览 5 评论 0原文

我正在尝试将参数传递到需要 System.TimeSpan 的组件中。 我只能让“长滴答”ctor 来解决。

这是配置文件的片段:

<component id="timeInForce" type="System.TimeSpan, mscorlib">
  <parameters>
    <hours>0</hours>
    <minutes>15</minutes>
    <seconds>0</seconds>
  </parameters>
</component>

<component id="FooSettings" type="Foo.FooSettings, Foo">
    <parameters>
        <tif>${timeInForce}</tif>
    </parameters>
</component>

这是例外:

Castle.MicroKernel.Handlers.HandlerException : Cant create component 'timeInForce'
as it has dependencies to be satisfied. 
timeInForce is waiting for the following dependencies: 

Keys (components with specific keys)
    - ticks which was not registered.

为组件参数传递刻度值可以工作,如下所示:

<parameters><tif>0</tif></parameters>

但这达不到目的。

I'm trying to pass a parameter into a component that requires a System.TimeSpan. I'm only able to get the 'long ticks' ctor to resolve.

Here's a snippet of the config file:

<component id="timeInForce" type="System.TimeSpan, mscorlib">
  <parameters>
    <hours>0</hours>
    <minutes>15</minutes>
    <seconds>0</seconds>
  </parameters>
</component>

<component id="FooSettings" type="Foo.FooSettings, Foo">
    <parameters>
        <tif>${timeInForce}</tif>
    </parameters>
</component>

This is the exception:

Castle.MicroKernel.Handlers.HandlerException : Cant create component 'timeInForce'
as it has dependencies to be satisfied. 
timeInForce is waiting for the following dependencies: 

Keys (components with specific keys)
    - ticks which was not registered.

Passing a tick value for the component parameter works, as in:

<parameters><tif>0</tif></parameters>

but this defeats the purpose.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

倾城°AllureLove 2024-07-15 14:54:05

发生的情况(据我所知)是,即使所有值类型都有默认的无参数构造函数,ticks 属性也被错误地识别为强制参数(因为它属于参数数量最少的构造函数)。

然而,即使您提供额外的参数(即刻度),仍然会选择与大多数参数匹配的构造函数候选者,因此您可以通过在参数列表中包含刻度来解决此问题:

<component id="timeInForce"" type="System.TimeSpan, mscorlib">
<parameters>
  <ticks>0</ticks>
  <hours>0</hours>
  <minutes>15</minutes>
  <seconds>0</seconds>
</parameters>
</component>

这是一个快速测试来验证它是否有效(通过对于城堡树干):

string xml = @"<?xml version=""1.0"" encoding=""utf-8"" ?> 
<castle>
<components>
<component id=""timeInForce"" type=""System.TimeSpan, mscorlib"">
<parameters>
  <ticks>0</ticks>
  <hours>0</hours>
  <minutes>15</minutes>
  <seconds>0</seconds>
</parameters>
</component>
</components>
</castle>";

WindsorContainer container = new WindsorContainer(
  new XmlInterpreter(new StaticContentResource(xml)));

TimeSpan span = container.Resolve<TimeSpan>("timeInForce");

Assert.AreEqual(new TimeSpan(0, 15, 0), span);

但是,我建议您使用的方法是实现您自己的类型转换器,如 城堡文档

这样,您就可以开发自己的时间跨度速记形式,即“15m”或“2h15m”或任何您喜欢的形式 - 使您的配置更易于阅读和维护,并解决您当前遇到的问题。

What's happening (from what I can see) is that the ticks property is being incorrectly identified as a compulsory parameter (because it belongs to the constructor with the least number of arguments) even though all value types have a default parameter-less constructor.

However the constructor candidate matching the most parameters will still be selected even if you supply additional parameters (i.e. ticks) so you can work around this by just including ticks in the list of parameters:

<component id="timeInForce"" type="System.TimeSpan, mscorlib">
<parameters>
  <ticks>0</ticks>
  <hours>0</hours>
  <minutes>15</minutes>
  <seconds>0</seconds>
</parameters>
</component>

Here is a quick test to verify it works (which passes for against the castle trunk):

string xml = @"<?xml version=""1.0"" encoding=""utf-8"" ?> 
<castle>
<components>
<component id=""timeInForce"" type=""System.TimeSpan, mscorlib"">
<parameters>
  <ticks>0</ticks>
  <hours>0</hours>
  <minutes>15</minutes>
  <seconds>0</seconds>
</parameters>
</component>
</components>
</castle>";

WindsorContainer container = new WindsorContainer(
  new XmlInterpreter(new StaticContentResource(xml)));

TimeSpan span = container.Resolve<TimeSpan>("timeInForce");

Assert.AreEqual(new TimeSpan(0, 15, 0), span);

However, what I would suggest rather then the approach your using is to implement your own type converter, as discussed in the castle documentation.

That way you could develop your own shorthand form for a timespan i.e. "15m" or "2h15m" or whatever takes your fancy - making your config a little easier to read and maintain and working round the issues you're currently experiencing.

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