Skip to content

Attempt to use of dynamically created type in the attribute leads to exception #95829

Closed as not planned

Description

@SnarkOrBoojum2

Description

I dynamically create the type with method that have an attribute. Attribute(not dynamic itself) have a parameter of type System.Type. As parameter I use another dynamically created type. That is lead to exception whenever I try to get that attribute later using System.Reflection.

Reproduction Steps

Here, the simple console project that reproduces that problem. Extract the archive, build the project, run ConsoleApp2 application. There will be exception at the last line of method Main().
ReproductionSolution.zip

Expected behavior

It must be allowed to use dynamically created types in the attributes of dynamically created types.

Actual behavior

While dynamic generation of attributed type by specified method in itself does not lead to exception, the later attempt to read attributes from that type with help of Syste.Reflection spawns it (System.IO.FileNotFound).

Regression?

The same functionality works OK under .NET Framework

Known Workarounds

I don't know any workaround.
Attempt to help .NET find the dynamic assembly through Resolving event from AssemblyLoadContext leads to unmanaged exception inside the module.

Configuration

.NET 6
Windows 10
x64

Other information

No response

Activity

  1. ghost added
    needs-area-label
    on Dec 9, 2023
  2. ghost added
    untriaged
    on Dec 9, 2023
  3. changed the title [-]Attempt to use of dynamically creates type in the attribute leads to exception[/-] [+]Attempt to use of dynamically created type in the attribute leads to exception[/+] on Dec 10, 2023
  4. added and removed
    needs-area-label
    on Dec 10, 2023
  5. self-assigned this
    on Dec 13, 2023
  6. buyaa-n commented on Feb 14, 2024

    @buyaa-n
    Contributor

    Attempt to help .NET find the dynamic assembly through Resolving event from AssemblyLoadContext leads to unmanaged exception inside the module.

    @elinor-fung could you take a look? The repro is attached in the issue description

  7. added this to the 9.0.0 milestone on Feb 14, 2024
  8. ghost removed
    untriaged
    on Feb 14, 2024
  9. removed their assignment
    on Feb 15, 2024
  10. buyaa-n commented on Feb 15, 2024

    @buyaa-n
    Contributor

    Regression?

    The same functionality works OK under .NET Framework

    @SnarkOrBoojum2 the same example actually throws the same exception, not sure what I missed ...

  11. added
    needs-author-action
    on Feb 15, 2024
  12. elinor-fung commented on Feb 15, 2024

    @elinor-fung
    Member

    Attempt to help .NET find the dynamic assembly through Resolving event from AssemblyLoadContext leads to unmanaged exception inside the module.

    @elinor-fung could you take a look? The repro is attached in the issue description

    AssemblyLoadContext.Resolving does not allow returning dynamically-emitted assemblies (as it affects how they get cached for the ALC). AppDomain.AssemblyResolve should allow it.

  13. SnarkOrBoojum2 commented on Feb 15, 2024

    @SnarkOrBoojum2
    Author
  14. ghost added
    needs-further-triage
    on Feb 15, 2024
  15. ghost removed
    needs-author-action
    on Feb 15, 2024
  16. buyaa-n commented on Feb 15, 2024

    @buyaa-n
    Contributor

    With the event handler it works for me on .NET Core, but I believe you should not unconditionally return the assembllyBuillder, only when the unresolved assembly matches with the assembllyBuillder:

           AppDomain.CurrentDomain.AssemblyResolve += (sender, eventArgs) =>
            {
                if (eventArgs.Name == assemblyBuilder.FullName)
                    return assemblyBuilder;
                return null;
            };
  17. SnarkOrBoojum2 commented on Feb 16, 2024

    @SnarkOrBoojum2
    Author
  18. SnarkOrBoojum2 commented on Feb 16, 2024

    @SnarkOrBoojum2
    Author
  19. elinor-fung commented on Feb 16, 2024

    @elinor-fung
    Member

    But this doesnt not work(engine exception)

    We don't allow returning a dynamically emitted assembly from AssemblyLoadContext.Resolving, so the error is expected. In .NET 7 and later, this should come through more clearly:

    Unhandled exception. System.IO.FileLoadException: Could not load file or assembly 'DynamicAssemblyExample, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'. An operation is not legal in the current state. (0x80131509)
    File name: 'DynamicAssemblyExample, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'
     ---> System.InvalidOperationException: Dynamically emitted assemblies are unsupported during host-based resolution.
    

    Whereas it looks like .NET 6 just reported internal error.

  20. SnarkOrBoojum2 commented on Feb 16, 2024

    @SnarkOrBoojum2
    Author
  21. locked and limited conversation to collaborators on Mar 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Reflectionneeds-further-triage

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions