Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 12, 2025

When using PersistedAssemblyBuilder to create generic types, calling MakeGenericType() returns a TypeBuilderInstantiation object. Previously, attempting to access members like constructors, methods, or fields on this instantiated type would throw unhelpful NotSupportedException messages like "Specified method is not supported."

This PR improves the error messages to provide clear guidance on how to properly access members using the static TypeBuilder methods:

Before:

Type instantiatedType = createdType.MakeGenericType(typeof(string));
instantiatedType.GetConstructors(); // NotSupportedException: "Specified method is not supported."

After:

Type instantiatedType = createdType.MakeGenericType(typeof(string));
instantiatedType.GetConstructors(); // NotSupportedException: "This type does not support resolving constructors. Use TypeBuilder.GetConstructor instead."

Changes made:

  1. Added error message resources to Strings.resx for methods that have corresponding TypeBuilder static alternatives:

    • NotSupported_TypeBuilderInstantiation_GetConstructor
    • NotSupported_TypeBuilderInstantiation_GetMethod
    • NotSupported_TypeBuilderInstantiation_GetField
  2. Updated error messages for methods that have TypeBuilder static alternatives:

    • GetConstructor* methods → Direct users to TypeBuilder.GetConstructor
    • GetMethod* methods → Direct users to TypeBuilder.GetMethod
    • GetField* methods → Direct users to TypeBuilder.GetField
  3. Maintained default error messages for methods that don't have TypeBuilder equivalents (Interface, Event, Property, NestedType operations) to avoid providing misleading guidance.

  4. Added comprehensive tests to validate the improved error messages and ensure the suggested workarounds continue to function correctly.

The correct workaround is to use the static TypeBuilder methods:

// Instead of: instantiatedType.GetConstructor(Type.EmptyTypes)
ConstructorInfo ctor = TypeBuilder.GetConstructor(instantiatedType, originalConstructor);

// Instead of: instantiatedType.GetMethod("MethodName")  
MethodInfo method = TypeBuilder.GetMethod(instantiatedType, originalMethod);

// Instead of: instantiatedType.GetField("FieldName")
FieldInfo field = TypeBuilder.GetField(instantiatedType, originalField);

This change maintains full backward compatibility while significantly improving the developer experience by providing actionable guidance when encountering this limitation.

Fixes #118506.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…eBuilder static methods

Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Copilot AI changed the title [WIP] [PersistedAssemblyBuilder] There is no ability to access any members of a generic class Improve TypeBuilderInstantiation error messages to guide users to TypeBuilder static methods Aug 12, 2025
Copilot AI requested a review from jkotas August 12, 2025 15:05
Copilot finished work on behalf of jkotas August 12, 2025 15:05
@jkotas
Copy link
Member

jkotas commented Aug 12, 2025

@copilot Please address the feedback above

… messages where appropriate

Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Aug 12, 2025

@copilot Please address the feedback above

Addressed both feedback items: moved error messages to Strings.resx and reverted methods without TypeBuilder alternatives to use default error messages. Commit c17083d.

Copilot finished work on behalf of jkotas August 12, 2025 15:42
@jkotas
Copy link
Member

jkotas commented Aug 12, 2025

@copilot Please address the feedback above

…zed resource string

Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Aug 12, 2025

@copilot Please address the feedback above

Completed. The error messages have been de-duplicated into a single parameterized resource string using nameof(TypeBuilder.GetConstructor), nameof(TypeBuilder.GetMethod), and nameof(TypeBuilder.GetField) as suggested. Commit d04f6ef.

Copilot AI requested a review from jkotas August 12, 2025 17:04
Copilot finished work on behalf of jkotas August 12, 2025 17:04
…ilder/TypeBuilderInstantiationErrorMessageTests.cs
Copilot AI requested a review from jkotas August 12, 2025 17:55
Copilot finished work on behalf of jkotas August 12, 2025 17:55
…peBuilder directory

Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Copilot finished work on behalf of jkotas August 12, 2025 18:22
@jkotas jkotas marked this pull request as ready for review August 12, 2025 18:24
Copy link
Contributor

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 improves error messages for TypeBuilderInstantiation objects created when calling MakeGenericType() on a TypeBuilder. Previously, attempting to access constructors, methods, or fields on these instantiated types would throw generic "Specified method is not supported" errors. Now they provide helpful guidance directing users to the appropriate TypeBuilder static methods.

Key changes:

  • Enhanced error messages to guide users to TypeBuilder.GetConstructor, TypeBuilder.GetMethod, and TypeBuilder.GetField static methods
  • Added comprehensive test coverage to validate the improved error messages
  • Maintained backward compatibility while significantly improving developer experience

Reviewed Changes

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

File Description
TypeBuilderInstantiationErrorMessageTests.cs New test file validating improved error messages for TypeBuilderInstantiation member access
TypeBuilderInstantiation.cs Updated constructor, method, and field access methods to use helpful error messages
Strings.resx Added new resource string for TypeBuilderInstantiation error messages

@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-reflection-emit
See info in area-owners.md if you want to be subscribed.

…t/TypeBuilderInstantiation.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@jkotas
Copy link
Member

jkotas commented Aug 13, 2025

/ba-g helix infrastructure failures without logs.

@jkotas jkotas merged commit cc1bffe into main Aug 13, 2025
131 of 138 checks passed
@jkotas jkotas deleted the copilot/fix-118506 branch August 13, 2025 00:36
@github-actions github-actions bot locked and limited conversation to collaborators Sep 12, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[PersistedAssemblyBuilder] There is no ability to access any members of a generic class

3 participants