값 형 또는 null 이외의 Reference Type를 건네주는 경우).
해결 방법은 Object로 널 캐스팅하는 것입니다 : -
var ninja = kernel.Get<Ninja>(new ConstructorArgument("weapon", (object)null));
Ninject에 2 출처 :
public class ConstructorArgument : Parameter
{
/// <summary>
/// Initializes a new instance of the <see cref="ConstructorArgument"/> class.
/// </summary>
/// <param name="name">The name of the argument to override.</param>
/// <param name="value">The value to inject into the property.</param>
public ConstructorArgument(string name, object value) : base(name, value, false) { }
/// <summary>
/// Initializes a new instance of the <see cref="ConstructorArgument"/> class.
/// </summary>
/// <param name="name">The name of the argument to override.</param>
/// <param name="valueCallback">The callback to invoke to get the value that should be injected.</param>
public ConstructorArgument(string name, Func<IContext, object> valueCallback) : base(name, valueCallback, false) { }
}
생식 :
public class ReproAndResolution
{
public interface IWeapon
{
}
public class Ninja
{
private readonly IWeapon _weapon;
public Ninja(IWeapon weapon)
{
_weapon = weapon;
}
}
[Fact]
public void TestMethod()
{
var kernel = new StandardKernel();
var ninja = kernel.Get<Ninja>(new ConstructorArgument("weapon", (object)null));
}
}
레슨? 당신은 최신 소스를 다운로드하지 말고 미친 듯이 보일 것입니다. 위대한 의견, 좋은 깨끗한 코드베이스. @ Ian Davis에게 다시 한번 감사의 말을 전한다. 내 장치에서 사용하려는
저는 ninject에 익숙하지 않지만 IoC 컨테이너가 적절한 생성자를 찾기 위해 유형 정보를 사용하고 null 값으로 찾을 수 없다고 생각합니다. 문서 나 코드에서 답변을 검색하는 것이 좋습니다. 하나의 라이브러리에서 해당 Type 객체의 인스턴스를 사용하는 것이 null 대신 전달되어야합니다. 여기에도 비슷한 해결책이있을 수 있습니다. – SergGr
감사합니다 .-) – stiank81