Spring.NET problem with tag in config

2019-07-29 11:49发布

According to the help file that comes with the Spring.NET framework, you can inject a dependancy defined in the local file by using an 'idref' tag along with a 'local' attribute.

I have been trying to do this with no success and was hoping someone had the experience to help me out.

Below I have a snippet from the config where I am passing it as a constructor argument, but I have tried setting it as a property as well. Both methods seem to yield the same error.

<object id="theTargetObject" type="TestClassLibrary.TargetObject, TestClassLibrary"/>

<object id="theClientObject" type="TestClassLibrary.ClientObject, TestClassLibrary">
    <constructor-arg name="myClass">
        <idref local="theTargetObject"/>
    </constructor-arg>
</object>

Error creating context 'spring.root': Error creating object with name 'theClientObject' defined in 'file [C:\Test\TestApp\bin\Debug\my.config.xml]' : Unsatisfied dependency expressed through constructor argument with index 0 of type [TestClassLibrary.TargetObject] : Could not convert constructor argument value [theTargetObject] to required type [TestClassLibrary.TargetObject] : Cannot convert property value of type [System.String] to required type [TestClassLibrary.TargetObject] for property ''.

2条回答
ゆ 、 Hurt°
3楼-- · 2019-07-29 12:25

I guess gef was on the right way but accidentially mixed it up when pasting the snippet.You are looking for the <ref> element:

<object id="theTargetObject" type="TestClassLibrary.TargetObject, TestClassLibrary"/>
<object id="theClientObject" type="TestClassLibrary.ClientObject, TestClassLibrary">
     <property name="myClass">
            <ref local="theTargetObject"/>
    </property>

the shorthand notation for this is:

<object id="theClientObject" type="TestClassLibrary.ClientObject, TestClassLibrary">
     <property name="myClass ref="theTargetObject"/>

hth, Erich

查看更多
登录 后发表回答