My goal is to have a collection of various generic types which will be used to populate a drop down list. The end user will select an item from the list which will then make an API call and return data based on the custom type stored in the list.
So I created an interface that my generic class will inherit from:
using System.Type;
namespace A
{
public interface ICustomCollection
{
int Index { get; set; }
string Name { get; set; }
string ResourceName { get; set; }
Type TType { get; }
Type YType { get; }
}
}
Then the generic class which inherits from it:
using System.Type;
namespace A
{
public class CustomCollection<T, Y> : ICustomCollection
where T : ITType
where Y : IYType
{
public int Index { get; set; }
public string Name { get; set; }
public string ResourceName { get; set; }
public Type TType { get { return typeof(T); } }
public Type YType { get { return typeof(Y); } }
}
}
Then, I instantiate the list when the component is initialized and PerformWork once the user selects an object from the drop down list:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Reflection;
using Microsoft.JSInterop;
using Microsoft.AspNetCore.Components;
namespace A
{
public partial class DoWork : ApiBase
{
public List<ICustomCollection> Items { get; set; }
public ICustomCollection SelectedItem { get; set; }
protected override async Task OnInitializedAsync()
{
Items = new List<ICustomCollection>()
{
new CustomCollection<ClassA, ClassB>()
{
Index = 1,
Name = "Option 1",
ResourceName = "url/for/api"
}
};
await base.OnInitializedAsync();
}
public async Task PerformWork()
{
//At this point, SelectedItem has been populated with the current ICustomCollection Item
var selectedT = SelectedItem.TType;
var selectedY = SelectedItem.YType;
//fails here with conversion error
return returnedData = await CallApiHelper((dynamic)selectedT, (dynamic)selectedY);
}
public async Task<string> CallApiHelper<T, Y>(T object1, Y object2)
where T : ITType
where Y : IYType
{
//base.CallApi implements the same generic type constraints as CallApiHelper
return await base.CallApi<T, Y>(SelectedItem.ResourceName);
}
}
}
The error message I'm getting specifically is:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: The type 'System.Reflection.TypeInfo' cannot be used as type parameter 'T' in the generic type or method 'CallApiHelper<T,Y>(T, Y)'. There is no implicit reference conversion from 'System.Reflection.TypeInfo' to 'ITType'.
which makes perfect sense.
Is there a way to extract the underlying type and pass that instead? I'm open to suggestions....