{"action":"created","issue":{"url":"https://api.github.com/repos/castleproject/Core/issues/220","repository_url":"https://api.github.com/repos/castleproject/Core","labels_url":"https://api.github.com/repos/castleproject/Core/issues/220/labels{/name}","comments_url":"https://api.github.com/repos/castleproject/Core/issues/220/comments","events_url":"https://api.github.com/repos/castleproject/Core/issues/220/events","html_url":"https://github.com/castleproject/Core/issues/220","id":183934257,"number":220,"title":"DynamicProxy: ParameterBuilder.SetConstant doesn't completely support C#, leading to exceptions","user":{"login":"cyanite","id":8737626,"avatar_url":"https://avatars.githubusercontent.com/u/8737626?v=3","gravatar_id":"","url":"https://api.github.com/users/cyanite","html_url":"https://github.com/cyanite","followers_url":"https://api.github.com/users/cyanite/followers","following_url":"https://api.github.com/users/cyanite/following{/other_user}","gists_url":"https://api.github.com/users/cyanite/gists{/gist_id}","starred_url":"https://api.github.com/users/cyanite/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cyanite/subscriptions","organizations_url":"https://api.github.com/users/cyanite/orgs","repos_url":"https://api.github.com/users/cyanite/repos","events_url":"https://api.github.com/users/cyanite/events{/privacy}","received_events_url":"https://api.github.com/users/cyanite/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-10-19T11:20:58Z","updated_at":"2016-10-19T11:38:58Z","closed_at":null,"body":"Microsoft's `System.Reflection.Emit.ParameterBuilder.SetConstant` method, used when defining methods with parameters with default values in `Castle.DynamicProxy.Generators.Emitters.MethodEmitter.DefineParameters` unfortunately (and inexplicably) doesn't support the full C#-standard with respect to what kinds of default parameters and expressions are allowed. This results in an exception from the framework when trying to create a proxy for a type with a method with certain default parameters.\r\n\r\nThe `System.Reflection.Emit.ParameterBuilder.SetConstant` method supports:\r\n> defaultValue is restricted to the following types: Boolean, SByte, Int16, Int32, Int64, Byte, UInt16, UInt32, UInt64, Single, Double, DateTime, Char, String, and Enum.\r\n\r\nThe C#-standard (here 5.0) has no direct restriction on the *type*, but for the expression supports:\r\n\r\n> The expression in a default-argument must be one of the following:\r\n> * a constant-expression \r\n>* an expression of the form new S() where S is a value type\r\n>* an expression of the form default(S) where S is a value type\r\n>\r\n> The expression must be implicitly convertible by an identity or nullable conversion to the type of the parameter.\r\n\r\nThis means that the following is valid C# code but can't be emitted using `ParameterBuilder`:\r\n`void Test(decimal x = 42M) { }`\r\n\r\nSince this is, in my view, a bug in the framework, I don't know what the solution should be. Perhaps check for the list of allowed types and simply skip the code to add default values if it doesn't match."},"comment":{"url":"https://api.github.com/repos/castleproject/Core/issues/comments/254788177","html_url":"https://github.com/castleproject/Core/issues/220#issuecomment-254788177","issue_url":"https://api.github.com/repos/castleproject/Core/issues/220","id":254788177,"user":{"login":"jonorossi","id":126297,"avatar_url":"https://avatars.githubusercontent.com/u/126297?v=3","gravatar_id":"","url":"https://api.github.com/users/jonorossi","html_url":"https://github.com/jonorossi","followers_url":"https://api.github.com/users/jonorossi/followers","following_url":"https://api.github.com/users/jonorossi/following{/other_user}","gists_url":"https://api.github.com/users/jonorossi/gists{/gist_id}","starred_url":"https://api.github.com/users/jonorossi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jonorossi/subscriptions","organizations_url":"https://api.github.com/users/jonorossi/orgs","repos_url":"https://api.github.com/users/jonorossi/repos","events_url":"https://api.github.com/users/jonorossi/events{/privacy}","received_events_url":"https://api.github.com/users/jonorossi/received_events","type":"User","site_admin":false},"created_at":"2016-10-19T11:38:58Z","updated_at":"2016-10-19T11:38:58Z","body":"@cyanite yep, we've run into various problems with SetConstant in the 2 years. We worked around the problems with decimal in #87, then decided to completely remove optional parameters in #141. These changes are in v4.0."}}
{"actor":{"display_login":"jonorossi"}}