代码之家  ›  专栏  ›  技术社区  ›  Paul Suart Wes

如何使用StructureMap传递null作为接口的默认实例?

  •  3
  • Paul Suart Wes  · 技术社区  · 16 年前

    ObjectFactory.Initialize(x =>
    {
        x.ForRequestedType<IFarmManager>().TheDefault.Is.Object(null);
    };
    

    但是,运行时会引发ArgumentNullException:

    Value cannot be null.
    Parameter name: anObject
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
    
    Exception Details: System.ArgumentNullException: Value cannot be null.
    Parameter name: anObject
    

    是否可以使用null作为接口的默认实例?

    我知道这个问题: StructureMap and passing null parameter to instance 但它没有任何答案。

    1 回复  |  直到 9 年前
        1
  •  2
  •   Paul Suart Wes    16 年前

    ObjectFactory.Initialize(x =>
    {
        x.ForRequestedType<IFarmManager>().TheDefault.Is.ConstructedBy(() => null);
    });