好吧,我被一个看似微不足道的功能难倒了。
如何使用XML配置让StructureMap初始化从容器检索到的类型实例的属性(不幸的是,我必须使用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;
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>