3

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>

しかし、これは目的に反します。

4

1 に答える 1

4

何が起こっているか (私が見ることができることから) は、すべての値型にデフォルトのパラメーターのないコンストラクターがあるにもかかわらず、 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」などのタイムスパンの独自の省略形を作成できます。構成を読みやすく、維持しやすくし、現在発生している問題を回避できます。

于 2008-11-06T10:00:20.213 に答える