Home > Enterprise >  Can't Find Constructor on Generic Class Activator
Can't Find Constructor on Generic Class Activator

Time:03-24

I'm trying to create an AutoFixture.ISpecimenBuilder for this class:

public class DiscosObjectRelationship<T> where T : DiscosModelBase
{
    private readonly string _linkAddress;
    private readonly IDiscosClient _client;
    private T? _relatedObject;
    
    public async Task<T?> GetRelatedObject()
    {
        return _relatedObject ?? await LazyLoad();
    }
    internal DiscosObjectRelationship(string linkAddress, IDiscosClient client)
    {
        _linkAddress = linkAddress;
        _client = client;
    }

    private async Task<T?> LazyLoad()
    {
        _relatedObject = (await _client.Get<T>(_linkAddress))?.Attributes;
        return _relatedObject;
    }
}

Essentially, all I want it to do is to set the _linkAddress to a random URL from Faker.Net and the _client to a mock from NSubstitute. I've tried to do this like so:

public class DiscosRelationshipSpecimenBuilder: ISpecimenBuilder
{
    public object Create(object request, ISpecimenContext context)
    {
        if (request is Type {IsGenericType: true} type && type.GetGenericTypeDefinition() == typeof(DiscosObjectRelationship<>))
        {
            return Activator.CreateInstance(type, Faker.Internet.Url(), Substitute.For<IDiscosClient>()) ?? new NoSpecimen();
        }

        return new NoSpecimen();
    }
}

However, it's throwing an error about not being able to find the constructor for the type. This has me confused as (as far as I can tell) I'm providing the correct arguments (string and IDiscosClient respectively).

The full exception is:

System.MissingMethodException: Constructor on type 'DISCOSweb_Sdk.Models.Relationships.DiscosObjectRelationship`1[[DISCOSweb_Sdk.Models.ResponseModels.Reentries.Reentry, DISCOSweb-Sdk, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' not found.
    at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture)
    at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
    at System.Activator.CreateInstance(Type type, Object[] args)
    at DISCOSweb_Sdk.Tests.Fixtures.AutoFixture.DiscosRelationshipSpecimenBuilder.Create(Object request, ISpecimenContext context) in /home/james/repos/DISOSweb-sdk/src/DISCOSweb-Sdk/DISCOSweb-Sdk.Tests/Fixtures/AutoFixture/DiscosRelationshipSpecimenBuilder.cs:line 17
    at AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context)
    at AutoFixture.CustomizationNode.Create(Object request, ISpecimenContext context)
    at AutoFixture.Kernel.CompositeSpecimenBuilder.Create(Object request, ISpecimenContext context)
    at AutoFixture.Kernel.TerminatingWithPathSpecimenBuilder.Create(Object request, ISpecimenContext context)

What am I doing wrong?

CodePudding user response:

From what I can tell, you can't make the constructor internal if you are using Activate reflection call here unless you provide the proper flags. Normally, it will only work with public constructors, I believe.

  • Related