问题
How do I create and populate a Dictionary using Unity's xml configuration?
I need to create a Dictionary and populate the objects using dependency references in unity.
How can I accomplish this?
回答1:
This works
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<alias alias="MyStuffObject" type="MyStuffObject, MyStuff"/>
<!-- dictionary stuff -->
<alias alias="IDictionaryOfStuff" type="System.Collections.Generic.IDictionary`2[[System.String, mscorlib],[MyStuffObject, MyStuff]], mscorlib"/>
<alias alias="DictionaryOfStuff" type="System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[MyStuffObject, MyStuff]], mscorlib"/>
<!-- Containers -->
<container name="Stuff">
<register type="IDictionaryOfStuff" mapTo="DictionaryOfStuff" name="dictionaryOfStuff">
<constructor />
<method name="Add">
<param name="key" type="System.String" value="Cool_Key"/>
<param name="value" type="IClinicalDocumentParser" dependencyName="coolStuffObject"/>
</method>
</register>
<register type="MyStuffObject" mapTo="MyStuffObject" name="coolStuffObject" />
</container>
</unity>
回答2:
You would have to create some sort of parsing system.
You can use the XML system in .Net to parse the XML code and then add it to the dictionary by doing dictionary.Add();
来源:https://stackoverflow.com/questions/5597492/how-do-i-create-and-populate-a-dictionarystring-object-using-unitys-xml-confi