{"action":"created","issue":{"url":"https://api.github.com/repos/ninject/Ninject/issues/175","labels_url":"https://api.github.com/repos/ninject/Ninject/issues/175/labels{/name}","comments_url":"https://api.github.com/repos/ninject/Ninject/issues/175/comments","events_url":"https://api.github.com/repos/ninject/Ninject/issues/175/events","html_url":"https://github.com/ninject/Ninject/issues/175","id":87289485,"number":175,"title":"InvalidProgramException due to invalid IL being generated","user":{"login":"mattwarren","id":157298,"avatar_url":"https://avatars.githubusercontent.com/u/157298?v=3","gravatar_id":"","url":"https://api.github.com/users/mattwarren","html_url":"https://github.com/mattwarren","followers_url":"https://api.github.com/users/mattwarren/followers","following_url":"https://api.github.com/users/mattwarren/following{/other_user}","gists_url":"https://api.github.com/users/mattwarren/gists{/gist_id}","starred_url":"https://api.github.com/users/mattwarren/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mattwarren/subscriptions","organizations_url":"https://api.github.com/users/mattwarren/orgs","repos_url":"https://api.github.com/users/mattwarren/repos","events_url":"https://api.github.com/users/mattwarren/events{/privacy}","received_events_url":"https://api.github.com/users/mattwarren/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":15,"created_at":"2015-06-11T10:05:47Z","updated_at":"2015-08-21T18:32:06Z","closed_at":"2015-07-17T16:31:12Z","body":"If you setup a binding like so:\r\n``` csharp\r\nBind<Func<ILogging>>().ToMethod(ctx => () => new MockLogging());\r\n```\r\n\r\nIn [DynamicMethodInjectorFactory it creates](https://github.com/ninject/Ninject/blob/master/src/Ninject/Injection/DynamicMethodInjectorFactory.cs#L32) some IL for the `Func<ILogging>` constructor (`ctor(System.Object, IntPtr)`) that looks like this:\r\n```\r\nldarg.0\r\nldc.i4, 0\r\nldelem.ref\r\ncastclass, System.Object\r\nldarg.0\r\nldc.i4, 1\r\nldelem.ref\r\nunbox.any, System.IntPtr\r\nnewobj, Void .ctor(System.Object, IntPtr) System.Func`1[ILogging]\r\nret\r\n```\r\n\r\nThe problem is that you can't use this method when dealing with an `IntPtr`, because it's really a pointer to a method. To do this you have to emit the IL like so:\r\n```\r\nil.Emit(OpCodes.Ldftn, methodInfo);\r\nil.Emit(OpCodes.Newobj, constructorInfo);\r\n```\r\nSo you have to use the [Ldftn OpCode](https://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.ldftn%28v=vs.110%29.aspx) and work directly with a [MethodInfo](https://msdn.microsoft.com/en-us/library/system.reflection.methodinfo%28v=vs.110%29.aspx). See [this SO answer](http://stackoverflow.com/questions/3915827/get-ldftn-function-pointer-in-c-sharp/3915967#3915967) for more info. \r\n\r\nSo normally this isn't a problem, because the IL that is compiled into a delegate doesn't *seem* to get executed when you use a binding as shown above. It uses the [`CallbackProvider`](https://github.com/ninject/Ninject/blob/master/src/Ninject/Activation/Providers/CallbackProvider.cs) that doesn't use the [`ConstructorInjector`](https://github.com/ninject/Ninject/blob/master/src/Ninject/Injection/ConstructorInjector.cs) (like the [`StandardProvider`](https://github.com/ninject/Ninject/blob/master/src/Ninject/Activation/Providers/StandardProvider.cs) does). If the compiled delegate was ever executed, then it would throw an `InvalidProgramException`.\r\n\r\n**But** if Ninject is used in conjunction with any product that uses the [.NET profiling API](https://msdn.microsoft.com/en-us/library/bb384493%28v=vs.110%29.aspx), this exception is instead thrown when the delegate is [*compiled*](https://github.com/ninject/Ninject/blob/d1660fb7f4816b2fae2c42dd662350d4e223ef60/src/Ninject/Injection/DynamicMethodInjectorFactory.cs#L57), which is a problem. This includes profilers, code coverage tools and production monitoring systems (the last of which the company I work for makes).\r\n\r\n**Questions**\r\n1. Am I right in thinking that when using a `Binding` as shown at the top of this post, the `ConstructorInjector` is never used? (I can't see how, but I just want to check this)\r\n2. Is it possible to get a fix in Ninject, so that the invalid IL isn't generated (especially if it turns out to be redundant, based on the answer to 1).\r\n\r\nI know that normally the solution to this is to use code like this:\r\n``` csharp\r\nINinjectSettings settings = new NinjectSettings\r\n{\r\n    UseReflectionBasedInjection = true,     // disable code generation for partial trust\r\n    InjectNonPublic = false,                // disable private reflection for partial trust\r\n    InjectParentPrivateProperties = false,  // reduce magic\r\n    LoadExtensions = false                  // reduce magic\r\n};\r\n```\r\nBut the problem with this is that `UseReflectionBasedInjection` is slower, so there's a performance hit for all the injection done by Ninject.\r\n\r\nBTW this problem *could* be the same as [this issue](https://groups.google.com/d/msg/ninject/M64UuvBoLUU/fQr8OhwOa_MJ) and [this one](http://stackoverflow.com/questions/5772860/exception-when-using-ninject)"},"comment":{"url":"https://api.github.com/repos/ninject/Ninject/issues/comments/133525254","html_url":"https://github.com/ninject/Ninject/issues/175#issuecomment-133525254","issue_url":"https://api.github.com/repos/ninject/Ninject/issues/175","id":133525254,"user":{"login":"BrunoJuchli","id":2090172,"avatar_url":"https://avatars.githubusercontent.com/u/2090172?v=3","gravatar_id":"","url":"https://api.github.com/users/BrunoJuchli","html_url":"https://github.com/BrunoJuchli","followers_url":"https://api.github.com/users/BrunoJuchli/followers","following_url":"https://api.github.com/users/BrunoJuchli/following{/other_user}","gists_url":"https://api.github.com/users/BrunoJuchli/gists{/gist_id}","starred_url":"https://api.github.com/users/BrunoJuchli/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/BrunoJuchli/subscriptions","organizations_url":"https://api.github.com/users/BrunoJuchli/orgs","repos_url":"https://api.github.com/users/BrunoJuchli/repos","events_url":"https://api.github.com/users/BrunoJuchli/events{/privacy}","received_events_url":"https://api.github.com/users/BrunoJuchli/received_events","type":"User","site_admin":false},"created_at":"2015-08-21T18:32:06Z","updated_at":"2015-08-21T18:32:06Z","body":"probably here: https://teamcity.bbv.ch/viewType.html?buildTypeId=bt7&tab=buildTypeHistoryList&branch_Ninject=%3Cdefault%3E\r\n\r\n(use guest login)"}}