好的,我被一个看似微不足道的功能难住了。
如何让 StructureMap 使用 XML 配置初始化从容器中检索到的类型实例的属性(不幸的是我必须使用 XML)?
我目前的代码是:
类型和接口:
public interface IMyType
{
decimal MyProperty { get; set; }
}
public MyType : IMyType
{
public decimal MyProperty {get; set; }
}
容器初始化和实例检索代码:
ObjectFactory
.Initialize(x => x.AddConfigurationFromXmlFile(@"StructureMap.config"));
IMyType instance = ObjectFactory.GetNamedInstance<IMyType>("Blah");
var myPropertyValue = instance.MyProperty; //expected 1, is actually 0
XML 配置:
<?xml version="1.0" encoding="utf-8" ?>
<StructureMap MementoStyle="Attribute">
<AddInstance
PluginType="MyNamespace.IMyType, MyAssemblyName"
PluggedType="MyNamespace.MyType, MyAssemblyName"
Key="Blah"
Name="Blah
MyProperty="1" />
</StructureMap>