代码之家  ›  专栏  ›  技术社区  ›  Ben Aston

StructureMap未正确实例化类型(使用XML配置)

  •  0
  • Ben Aston  · 技术社区  · 15 年前

    好吧,我被一个看似微不足道的功能难倒了。

    如何使用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>
    
    1 回复  |  直到 15 年前
        1
  •  1
  •   Ben Aston    15 年前

    这看起来像是StructureMap的键入问题。使用int、float或double可以工作。使用小数点并不重要。

    解决方法是使用另一种浮点类型,如float或double。

    推荐文章