Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 24, 2025

Problem

The code examples for TypeBuilder.DefineMethodOverride at https://learn.microsoft.com/en-us/dotnet/api/system.reflection.emit.typebuilder.definemethodoverride?view=net-9.0#examples were using .NET Framework-specific APIs that are not available in .NET Core, .NET 5+, or .NET 9. This made the examples non-compilable for modern .NET versions despite being displayed on the .NET 9 documentation page.

Changes

Updated both C# and Visual Basic code examples to use modern .NET APIs:

C# Changes

  • Updated Project.csproj to target net9.0 (was net48) and changed OutputType to Exe
  • Replaced AppDomain.CurrentDomain.DefineDynamicAssembly() with AssemblyBuilder.DefineDynamicAssembly() (modern API pattern)
  • Changed AssemblyBuilderAccess.RunAndSave to AssemblyBuilderAccess.Run (the RunAndSave option is not supported in .NET Core+)
  • Updated DefineDynamicModule(name, fileName) to DefineDynamicModule(name) to match the modern method signature
  • Removed ab.Save() call and related comment (assembly persistence is not supported in .NET Core+)

Visual Basic Changes

  • Created Project.vbproj targeting net9.0 with OutputType set to Exe
  • Applied the same API modernization changes as the C# version

Testing

Both examples now:

  • Build successfully with .NET 9 SDK
  • Run correctly and produce the expected output demonstrating DefineMethodOverride functionality
  • Maintain the same educational value showing how to use explicit interface implementations with dynamic types

The core functionality demonstrated by the examples (using DefineMethodOverride to associate a method body with an interface method declaration) remains unchanged and works identically on .NET 9.

Fixes #[issue number]

Original prompt

Please make this code example compilable for .NET 9 instead of just .NET Framework: https://learn.microsoft.com/en-us/dotnet/api/system.reflection.emit.typebuilder.definemethodoverride?view=net-9.0#examples


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@gewarren gewarren changed the title [WIP] Make code example compilable for .NET 9 [WIP] [UUF] Make code example compilable for .NET 9 Oct 24, 2025
Copilot AI and others added 2 commits October 24, 2025 00:08
Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com>
Co-authored-by: gewarren <24882762+gewarren@users.noreply.github.com>
Copilot AI changed the title [WIP] [UUF] Make code example compilable for .NET 9 Update TypeBuilder.DefineMethodOverride code examples to target .NET 9 Oct 24, 2025
Copilot AI requested a review from gewarren October 24, 2025 00:16
Copilot finished work on behalf of gewarren October 24, 2025 00:16
@gewarren gewarren marked this pull request as ready for review October 24, 2025 00:21
@gewarren gewarren requested a review from a team as a code owner October 24, 2025 00:21
@Copilot Copilot AI review requested due to automatic review settings October 24, 2025 00:21
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR updates the code examples for TypeBuilder.DefineMethodOverride to target .NET 9 instead of .NET Framework 4.8, making them compilable and runnable on modern .NET versions. The changes replace deprecated .NET Framework-specific APIs with their modern equivalents while preserving the educational purpose of demonstrating explicit interface implementation with dynamic types.

Key Changes:

  • Migrated from .NET Framework 4.8 to .NET 9 with modern API patterns
  • Replaced AppDomain.CurrentDomain.DefineDynamicAssembly() with AssemblyBuilder.DefineDynamicAssembly()
  • Removed assembly persistence functionality (Save() method) which is not supported in .NET Core+

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
snippets/csharp/System.Reflection.Emit/TypeBuilder/DefineMethodOverride/Project.csproj Updated target framework from net48 to net9.0 and changed OutputType from Library to Exe
snippets/csharp/System.Reflection.Emit/TypeBuilder/DefineMethodOverride/source.cs Modernized reflection emit APIs and removed assembly save functionality
snippets/visualbasic/System.Reflection.Emit/TypeBuilder/DefineMethodOverride/Project.vbproj Created new project file targeting net9.0 as an executable
snippets/visualbasic/System.Reflection.Emit/TypeBuilder/DefineMethodOverride/source.vb Applied same API modernization changes as C# version

@gewarren gewarren changed the title Update TypeBuilder.DefineMethodOverride code examples to target .NET 9 [UUF] Update TypeBuilder.DefineMethodOverride code examples to target .NET 9 Oct 24, 2025
…dOverride/source.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@gewarren gewarren enabled auto-merge (squash) October 24, 2025 00:24
Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM @gewarren

Let's :shipit:

@gewarren gewarren merged commit dfd3682 into main Oct 24, 2025
5 checks passed
@gewarren gewarren deleted the copilot/update-code-example-for-dotnet9 branch October 24, 2025 13:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants