{"id":4081,"date":"2014-03-30T07:28:10","date_gmt":"2014-03-30T07:28:10","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/why-cant-i-inject-value-null-with-ninjects-constructorargument-collection-of-common-programming-errors\/"},"modified":"2014-03-30T07:28:10","modified_gmt":"2014-03-30T07:28:10","slug":"why-cant-i-inject-value-null-with-ninjects-constructorargument-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/why-cant-i-inject-value-null-with-ninjects-constructorargument-collection-of-common-programming-errors\/","title":{"rendered":"Why can&#39;t I inject value null with Ninjects ConstructorArgument?-Collection of common programming errors"},"content":{"rendered":"<p>Looking at the source (and the stack trace I got by reproing which you omitted :P)<\/p>\n<p>This is because it&#8217;s binding to a different overload of the <code>ConstructorArgument<\/code> ctor than the normal usage (i.e., where you&#8217;re passing a Value Type or a non-null Reference Type) does.<\/p>\n<p>The workaround is to cast the null to Object:-<\/p>\n<pre><code>var ninja = kernel.Get( new ConstructorArgument( \"weapon\", (object)null ) );\n<\/code><\/pre>\n<p>Ninject 2 source:<\/p>\n<pre><code>public class ConstructorArgument : Parameter\n{\n    \/\/\/ \n    \/\/\/ Initializes a new instance of the  class.\n    \/\/\/ \n    \/\/\/ The name of the argument to override.\n    \/\/\/ The value to inject into the property.\n    public ConstructorArgument(string name, object value) : base(name, value, false) { }\n\n    \/\/\/ \n    \/\/\/ Initializes a new instance of the  class.\n    \/\/\/ \n    \/\/\/ The name of the argument to override.\n    \/\/\/ The callback to invoke to get the value that should be injected.\n    public ConstructorArgument(string name, Func valueCallback) : base(name, valueCallback, false) { }\n}\n<\/code><\/pre>\n<p>Repro:<\/p>\n<pre><code>public class ReproAndResolution\n{\n    public interface IWeapon\n    {\n    }\n\n    public class Ninja\n    {\n        private readonly IWeapon _weapon;\n        public Ninja( IWeapon weapon )\n        {\n            _weapon = weapon;\n        }\n    }\n\n    [Fact]\n    public void TestMethod()\n    {\n        var kernel = new StandardKernel();\n        var ninja = kernel.Get( new ConstructorArgument( \"weapon\", (object)null ) );\n    }\n}\n<\/code><\/pre>\n<p>Lesson? You&#8217;d be crazy not to download the latest source and look at it. Great comments, nice clean codebase. Thanks again to @Ian Davis for that tip\/prodding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Looking at the source (and the stack trace I got by reproing which you omitted :P) This is because it&#8217;s binding to a different overload of the ConstructorArgument ctor than the normal usage (i.e., where you&#8217;re passing a Value Type or a non-null Reference Type) does. The workaround is to cast the null to Object:- [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4081","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4081","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4081"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4081\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4081"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4081"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}