gpt4 book ai didi

c# - 统一: Build up a dictionary

转载 作者:太空狗 更新时间:2023-10-29 21:31:30 24 4
gpt4 key购买 nike

我正在将 CaSTLe/Monorails 应用程序转换为 Unity/Asp.NET MVC 应用程序,我一直在尝试转换此组件配置:

<component
id="ComponentBaseConfiguration"
service="MyFakeNamespace.BOL.IConfiguration, MyFakeAppDll"
type="MyFakeNamespace.BOL.ConfigurableConfiguration, MyFakeAppDll">
<parameters>
<!-- Setting Configuration (Dictionary<string,string>)-->
<Config>
<dictionary>
<entry key="localHost">#{LocalHost}</entry>
<entry key="contentHost">#{ContentHost}</entry>
<entry key="virtualDir">#{VirtualDir}</entry>
</dictionary>
</Config>
</parameters>

似乎Unity支持Array但不支持Dictionary,我想做这样的事情:

<unity>
<containers>
<container>
<types>
<type name="ComponentBaseConfiguration" type="MyFakeNamespace.BOL.IConfiguration, MyFakeAppDll" mapTo="MyFakeNamespace.BOL.ConfigurableConfiguration, MyFakeAppDll">
<typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration">
<property name="Config" propertyType="System.Collections.Generic.Dictionary`2[[System.String, mscorlib], [System.String, mscorlib]],mscorlib">
<dictionary>
<entry key="localHost">127.0.0.1</keyedValue>
<entry key="contentHost">\\content</keyedValue>
<entry key="virtualDir">/</keyedValue>
</dictionary>
</property>
</typeConfig>
</type>
</types>
</container>
</containers></unity>

我怎样才能实现这样的目标?

最佳答案

我认为您必须使用 method 元素来存档它。这不是很好,而是一种解决方法。

您的类型必须定义一个方法 Add(string key, string value),统一容器使用该方法注入(inject)值。

<method name="Add">
<param name="key" parameterType="string">
<value value="localHost"/>
</param>
<param name="value" parameterType="string">
<value value="127.0.0.1"/>
</param>
</method>

Unity 绝对不支持容器配置的字典。参见 Build Dictionaries using Unity container?

关于c# - 统一: Build up a dictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1427077/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com