{"id":9921,"date":"2006-07-24T16:12:00","date_gmt":"2006-07-24T16:12:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2006\/07\/24\/casting-a-scriptblock-to-an-arbitrary-delegate-signature\/"},"modified":"2019-02-18T13:21:29","modified_gmt":"2019-02-18T20:21:29","slug":"casting-a-scriptblock-to-an-arbitrary-delegate-signature","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/casting-a-scriptblock-to-an-arbitrary-delegate-signature\/","title":{"rendered":"Casting a scriptblock to an arbitrary delegate signature"},"content":{"rendered":"<p>Arild&nbsp;asked the following&nbsp;in <a href=\"http:\/\/arildf.spaces.msn.com\/blog\/cns!E99F8B43533149B0!140.entry?_c%3dBlogPart\">http:\/\/arildf.spaces.msn.com\/blog\/cns!E99F8B43533149B0!140.entry?_c%3dBlogPart<\/a>&nbsp;:<\/p>\n<p>PSMDTAG:FAQ: Delegates &#8211; how to cast a scriptblock to an arbitrary delegate signature? ANSWER &#8211; You can&#8217;t in&nbsp;V1.0. <\/p>\n<p>In version 1.0 Windows PowerShell only supports casting scriptblocks to delegates with the signature:<\/p>\n<p>void Method(Object Sender, object e)<\/p>\n<p>For instance, here is a WinForms script which uses a scriptblock delegate when a button is clicked:<\/p>\n<p><font face=\"Courier New\" size=\"1\">&nbsp; [void][Reflection.Assembly]::LoadWithPartialName(&#8220;System.Windows.Forms&#8221;)<br \/>&nbsp; $form = new-object Windows.Forms.Form<br \/>&nbsp; $button = new-object Windows.Forms.Button<br \/>&nbsp; $button.Dock = &#8220;fill&#8221;<br \/>&nbsp; $button.Text = &#8220;Click to exit&#8221;<\/font><\/p>\n<p><font face=\"Courier New\" size=\"1\">&nbsp; ###################################<br \/>&nbsp; # DELEGATE SUPPORT <br \/>&nbsp; ###################################<br \/>&nbsp; $button.add_click(<br \/>&nbsp;&nbsp;&nbsp;&nbsp; { <br \/>&nbsp;write-host @&#8221;<br \/>`$this is a handle to the button: <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $this&#8221;<br \/>$($this | out-string)<\/font><\/p>\n<p><font face=\"Courier New\" size=\"1\">=====================================<br \/>`$_ is a pointer to MouseEventArgs: <br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $_<br \/>$($_ | out-string)<br \/>&#8220;@ <\/font><\/p>\n<p><font face=\"Courier New\" size=\"1\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $form.close()<br \/>&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>&nbsp; )<br \/>&nbsp; $form.Add_Shown(<br \/>&nbsp;&nbsp;&nbsp;&nbsp; {$form.Activate()<br \/>&nbsp;&nbsp;&nbsp;&nbsp; }<br \/>&nbsp; )<\/font><\/p>\n<p><font face=\"Courier New\" size=\"1\">&nbsp; $form.controls.add($button)<br \/>&nbsp; $form.showDialog()<\/font><\/p>\n<p>This works because the delegate for the WinForms button click has the signature that we support in V1.0 (no coincidence there).<\/p>\n<p>Support for delegates of any signature is definitely something we want to do ASAP (after V1.0).<\/p>\n<p>Jeffrey Snover [MSFT]<br \/>Windows PowerShell\/Aspen Architect<br \/>Visit the Windows PowerShell Team blog at:&nbsp;&nbsp;&nbsp; <a href=\"http:\/\/blogs.msdn.com\/PowerShell\">http:\/\/blogs.msdn.com\/PowerShell<\/a><br \/>Visit the Windows PowerShell ScriptCenter at:&nbsp; <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/a><\/p>\n<p>&nbsp;<\/p>\n<p>PSMDTAG:DOTNET: Delegate signatures<\/p>\n<p><a href=\"https:\/\/msdnshared.blob.core.windows.net\/media\/MSDNBlogsFS\/prod.evol.blogs.msdn.com\/CommunityServer.Components.PostAttachments\/00\/00\/67\/70\/88\/h.ps1\">h.ps1<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Arild&nbsp;asked the following&nbsp;in http:\/\/arildf.spaces.msn.com\/blog\/cns!E99F8B43533149B0!140.entry?_c%3dBlogPart&nbsp;: PSMDTAG:FAQ: Delegates &#8211; how to cast a scriptblock to an arbitrary delegate signature? ANSWER &#8211; You can&#8217;t in&nbsp;V1.0. In version 1.0 Windows PowerShell only supports casting scriptblocks to delegates with the signature: void Method(Object Sender, object e) For instance, here is a WinForms script which uses a scriptblock delegate when a [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[53,14,10,54,55],"class_list":["post-9921","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","tag-delegates","tag-dotnet","tag-faq","tag-scriptblock","tag-winforms"],"acf":[],"blog_post_summary":"<p>Arild&nbsp;asked the following&nbsp;in http:\/\/arildf.spaces.msn.com\/blog\/cns!E99F8B43533149B0!140.entry?_c%3dBlogPart&nbsp;: PSMDTAG:FAQ: Delegates &#8211; how to cast a scriptblock to an arbitrary delegate signature? ANSWER &#8211; You can&#8217;t in&nbsp;V1.0. In version 1.0 Windows PowerShell only supports casting scriptblocks to delegates with the signature: void Method(Object Sender, object e) For instance, here is a WinForms script which uses a scriptblock delegate when a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/9921","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=9921"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/9921\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=9921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=9921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=9921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}