lundi 15 mars 2021

How do I create an instance of a generic type with a parameter of type list

I have a generic class Result that looks like this

public class Result<T>
{
    public List<Error> Errors { get; set; } = new List<Error>();

    public T ResultObject { get; set; }

    public Result(List<Error> errors)
    {
        Errors = errors;
        Success = false;
    }
}

I try to create an instance like this:

Activator.CreateInstance(typeof(TResponse), new object[] { failures.Select(x => new Error(x.ErrorMessage)).ToList() })

TResponse is of type Result<GetUserFlow>. GetUserFlow is a dto but not important in this case. When I run my code I get the following error:

System.MissingMethodException: Constructor on type 'Heimdall.Application.Result`1[[Heimdall.Application.Dtos.GetUserFlow, Heimdall.Application, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' not found.

I guess the problem is that when passing a list as parameter it isn't seen as one parameter but as many. Each created Error is one parameter. I couldn't find anything about this topic.





Aucun commentaire:

Enregistrer un commentaire