Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActivatorUtilities.CreateInstance is creating non-empty generic collections #109034

Open
miguelnunes94 opened this issue Oct 18, 2024 · 3 comments
Labels
area-Extensions-DependencyInjection untriaged New issue has not been triaged by the area owner

Comments

@miguelnunes94
Copy link

Description

Generic collections that have copy constructors (with IEnumerable<T> as parameter) are initialized with elements, when using ActivatorUtilities.CreateInstance/ActivatorUtilities.GetServiceOrCreateInstance.

Reproduction Steps

    public interface ITest { }

    public class Test : ITest { }

    public class Test2 : ITest { }

    public static void Main(string[] args)
    {

        var serviceCollection = new ServiceCollection();

        serviceCollection.AddTransient<ITest, Test>();
        serviceCollection.AddTransient<ITest, Test2>();

        var services = serviceCollection.BuildServiceProvider();

        var collection = ActivatorUtilities.CreateInstance<List<ITest>>(services);
    }

Expected behavior

The collection object is an empty instance of List, because ActivatorUtilities.CreateInstance uses the constructor public List()

Actual behavior

The collection object is an instance of List containing two elements: one instance of Test and one of Test2. Currently, ActivatorUtilities.CreateInstance is selecting the constructor public List(IEnumerable<T> collection).

Regression?

We found this issue while upgrading from .Net 6 to .Net 8

Known Workarounds

No response

Configuration

.Net SDK 8.0.403

Other information

In such cases, the parameterless constructor may be preferable - particularly for generic collections with copy constructors. Consider annotating the expected constructors with the ActivatorUtilitiesConstructorAttribute to ensure the correct constructor is chosen.

It should be related with:

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Oct 18, 2024
Copy link
Contributor

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

@julealgon
Copy link

If you want an empty list... why go through ActivatorUtilities for that?

@miguelnunes94
Copy link
Author

The ActivatorUtilities is being used in a serialization & deserialization context where many different types are involved, including system generic collections. We used to have a clean solution by using the it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Extensions-DependencyInjection untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

2 participants