问题
I am trying to initiate a class that is expecting a System.Type in it's CTOR.
Is there a way in spring.net config file to accomplish this, and preferable pass the type of a spring initialised object?
Thanks,
Lihnid
回答1:
I think that this should do it:
<constructor-arg name="argumentname" expression="T(namespace.to.my.type, assemblyname)" />
回答2:
This also works:
<constructor-arg name="argumentname" value="MyNamespace.MyType, MyAssembly"/>
Use VS2010 Add-In for intellisense: http://www.springframework.net/vsaddin/
回答3:
You can use an expression to inject a type:
<!-- use an expression to specify type -->
<constructor-arg type="System.Type, mscorlib"
expression="T(MyNameSpace.MySecondClass)" />
To get the type of a spring-managed object, you can use the expression="@(object-id-here)"
syntax to use a spring-managed object in your expression and simply call GetType()
on it:
<!-- inject the type of MySecondObject, configured elsewhere -->
<constructor-arg type="System.Type, mscorlib"
expression="@(MySecondObject).GetType()" />
回答4:
Just in case if anyone is wondering how to do this with using the System.Type as the dictionary key.
<object id="myDictionary" type="System.Collections.Generic.Dictionary<System.Type,string>">
<constructor-arg name="dictionary">
<dictionary key-type="System.Type" value-type="string">
<entry value="myStringValue">
<key>
<expression value="T(namespace.to.MyClassType)"></expression>
</key>
</entry>
</dictionary>
</constructor-arg>
</object>
来源:https://stackoverflow.com/questions/8433676/ioc-spring-net-injecting-system-type