StructureMap 未正确实例化类型(使用 XML 配置)
好吧,我被一个看似微不足道的功能难住了。
如何使用 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; //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>
OK, I'm stumped over a seemingly trivial piece of functionality.
How can I get StructureMap to initialize the properties on type instances retrieved from the container, using XML configuration (unfortunately I have to use XML)?
My current code is:
The type and interface:
public interface IMyType
{
decimal MyProperty { get; set; }
}
public MyType : IMyType
{
public decimal MyProperty {get; set; }
}
The container initialization and instance retrieval code:
ObjectFactory
.Initialize(x => x.AddConfigurationFromXmlFile(@"StructureMap.config"));
IMyType instance = ObjectFactory.GetNamedInstance<IMyType>("Blah");
var myPropertyValue = instance.MyProperty; //expected 1, is actually 0
XML Configuration:
<?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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这看起来像是 StructureMap 的打字问题。使用 int、float 或 double 可以工作。使用小数则不然。
解决方法是使用另一种浮点类型,例如 float 或 double。
This looks like a typing issue with StructureMap. Using an int, float or double works. Using a decimal does not.
Workaround is to use another floating point type such as float or double.