{"id":3802,"date":"2014-03-30T05:44:23","date_gmt":"2014-03-30T05:44:23","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/system-dynamic-bug-collection-of-common-programming-errors\/"},"modified":"2014-03-30T05:44:23","modified_gmt":"2014-03-30T05:44:23","slug":"system-dynamic-bug-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/system-dynamic-bug-collection-of-common-programming-errors\/","title":{"rendered":"System.Dynamic bug?-Collection of common programming errors"},"content":{"rendered":"<p>While I playing with the C# 4.0 dynamic, I found strange things happening with the code like this:<\/p>\n<pre><code>using System.Dynamic;\n\nsealed class Foo : DynamicObject\n{\n    public override bool TryInvoke(\n        InvokeBinder binder, object[] args, out object result)\n    {\n        result = new object();\n        return true;\n    }\n\n    static void Main()\n    {\n        dynamic foo = new Foo();\n\n        var t1 = foo(0);\n        var t2 = foo(0);\n        var t3 = foo(0);\n        var t4 = foo(0);\n        var t5 = foo(0);\n    }\n}\n<\/code><\/pre>\n<p>Ok, it works but&#8230; take a look at IntelliTrace window:<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/img717.imageshack.us\/img717\/4914\/10435230.png\" \/><\/p>\n<p>So every invokation (and other operations too on dynamic object) causes throwing and catching strange exceptions twice!<\/p>\n<p>I understand, that sometimes exceptions mechanism may be used for optimizations, for example first call to dynamic may be performed to some stub delegate, that simply throws exception &#8211; this may be like a signal to dynamic binder to resolve an correct member and re-point delegate. Next call to the same delegate will be performed without any checks.<\/p>\n<p>But&#8230; behavior of the code above looks very strange. Maybe throwing and catching exceptions twice per any operation on DynamicObject &#8211; is a bug?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>While I playing with the C# 4.0 dynamic, I found strange things happening with the code like this: using System.Dynamic; sealed class Foo : DynamicObject { public override bool TryInvoke( InvokeBinder binder, object[] args, out object result) { result = new object(); return true; } static void Main() { dynamic foo = new Foo(); var [&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-3802","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3802","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=3802"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3802\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3802"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3802"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3802"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}