IoC spring.net injecting System.Type

断了今生、忘了曾经 提交于 2019-12-11 07:36:47

问题


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&lt;System.Type,string&gt;">
    <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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!