I have an interface
public interface ISomething<TValue>
where TValue : IConvertible
{
...
}
Then I also have some non-generic class with a generic method:
public class Provider
{
public static void RegisterType<TValue>(ISomething<TValue> instance)
where TValue : IConvertible
{
...
}
}
This seems all fine until I want to automatically register all applicable types in my particular assembly.
public void InitializeApp()
{
foreach(Type t in Assembly.GetExecutingAssembly().GetTypes().Where(t => typeof(ISomething<>).IsAssignableFrom(T)))
{
// ERROR
Provider.RegisterType(Activator.CreateInstance(t));
}
}
This code results in error as argument type can't be inferred from usage. I should either
- provide explicit generic type with my generic method
RegisterType
(I don't think I can do this due to dynamic nature of my code) or - cast results from
Activator.CreateInstance
to appropriate type so argument type could be inferred from usage
But I don't know how to do either? Maybe there's a third option I'm not aware of.