0

I have below class LibrarySourceTableInput and having structure like as this

public class LibrarySourceTableInput<T> where T: ISourceOfData
{      
    public LibrarySourceTableInput(List<T> libraries, string mappedLibrarySource)
    {
        this.LibrarySourceRowInputs = libraries?.Select(l => new LibrarySourceRowInput()
            {
                LibrarySourceId = l.Id,
                SourceOfDataId = l.SourceOfData.Id
            }).ToList() ?? new(),
         this.MappedLibrarySource = mappedLibrarySource;
    }
    public List<LibrarySourceRowInput> LibrarySourceRowInputs { get; set; }
    public string MappedLibrarySource { get; set; }
}

and then the interface ISourceOfData structure looks like as this below

public interface ISourceOfData : IIdentity
{
    public new Guid Id { get; set; }
    public CodeStandardGuideline SourceOfData { get; set; }
}

And i am calling above class constructor in other place like as this

var mechanicalData = spaceTypeObject.TargetObject.MechanicalData;                           
 var librarySourceTableInputs = new List<LibrarySourceTableInput<ISourceOfData>>
 {
     new LibrarySourceTableInput<ISourceOfData>(mechanicalData?.Environments, mappedLibrarySource), // I am getting conversion error here 
     new LibrarySourceTableInput<ISourceOfData>(mechanicalData?.AirflowsA621 , mappedLibrarySource)
     .......
     .......
 }

and the structure for mechanicaldata.environments is looks like this

public class MechanicalData
{
    public List<LibraryEnvironment> Environments { get; set; }
    public List<LibraryA621> AirflowsA621 { get; set; }
    .......
}

and then last one libraryEnvironment class is looks like this

 public class LibraryEnvironment : ISourceOfData
 {
    public virtual CodeStandardGuideline SourceOfData { get; set; }
    .....
    ......
    public Guid Id {get; set;}     
 }

I have got the below error at this line new LibrarySourceTableInput<ISourceOfData>(mechanicalData?.Environments, mappedLibrarySource)

and the error is Cannot convert from Generic.List<LibraryEnvironment> to Generic.List<IsourceOfData>

Could any one please let me know or any idea on how to send that generic list to that class constructor, Many thanks in advance.

Glory Raj
  • 17,397
  • 27
  • 100
  • 203
  • If you absolutely must work with a generic class, you might want to take a look at this question: [Collection of generic types](https://stackoverflow.com/q/3215402) – greenjaed Mar 18 '21 at 21:25
  • 1
    never mind i figured out that just need to cast to corresponding interface – Glory Raj Mar 18 '21 at 22:58

1 Answers1

0

Instead of specifying the interface ISourceOfData (your class already knows the type will have that interface), specify the actual class:

var librarySourceTableInputs = new List<LibrarySourceTableInput<LibraryEnvironment>>
{
    new LibrarySourceTableInput<LibraryEnvironment>(mechanicalData?.Environments, mappedLibrarySource),
     .......
     .......
}
greenjaed
  • 589
  • 8
  • 20