0

Having following class structure, how can I get instance of Configuration<X> where class X is not known at the build time?

Initial setup:

public interface IConfiguration
{
}

public class Configuration<T> : IConfiguration
{
}


List<IConfiguration> configList;

configList.Add( new Configuration<X>());
configList.Add( new Configuration<Y>());
configList.Add( new Configuration<Z>());




     Foreach(var config in configList)
{
   Proccess(config) // errors .  I need to cast in here
}

Proccess<T>(Configuration<T>) {}

Boxing or reflection is allowed. I need to cast base class to unknown generic type class.

eren arslan
  • 197
  • 2
  • 11
  • but I dont know type in compile time. I need to cast in rune time – eren arslan May 27 '21 at 08:57
  • 4
    What would you want to do *after* casting to it? You can't know any of the members at compile-time, if you don't know the type at compile-time... – Jon Skeet May 27 '21 at 08:58
  • _"but I dont know type in compile time. I need to cast in rune time"_ please give an example that shows your problem. The given code shoud work just fine (given you actually instantiate the list before adding to it). – Fildor May 27 '21 at 08:59
  • 2
    Ah, wait. `X` is unknown at compile-time?? I guess then Generics is not the correct tool. – Fildor May 27 '21 at 09:03
  • Maybe you need instantiate the collection : `List configList = new List();` – vernou May 27 '21 at 09:03
  • 1
    Your example still doesn't describe what you want to _do_ with `result`, after having hypothetically being able to cast it to Configuration for an unknown value of X – Jonas Høgh May 27 '21 at 09:04
  • @vernou That's missing in the snippet, but I think OP's actual problem is the unknown type of `X` at compile time. ( i.e. `new Configuration()` ) – Fildor May 27 '21 at 09:05
  • 2
    Maybe you can move the method `Process` in the interface `IConfiguration`. – vernou May 27 '21 at 09:12
  • The method `Process()` is unclear. Passing in `IConfiguration` as parameter to the method might also be an option. – Standard_101 May 27 '21 at 09:16
  • 1
    This just seems to move the problem a step. If you don't know X at compile time, how can Process ...process it? – ProgrammingLlama May 27 '21 at 09:16
  • proccess do same thing for all. but in proccess I need to use type – eren arslan May 27 '21 at 09:19
  • 1
    Does this answer your question? [Assigning List as List](https://stackoverflow.com/questions/2033912/c-sharp-variance-problem-assigning-listderived-as-listbase/) and [How to create List of open generic type](https://stackoverflow.com/questions/58570948/how-to-create-list-of-open-generic-type-of-classt) and [How to do generic polymorphism on open types](https://stackoverflow.com/questions/58247604/how-to-do-generic-polymorphism-on-open-types-in-c/) and [What is polymorphism](https://stackoverflow.com/questions/1031273/what-is-polymorphism-what-is-it-for-and-how-is-it-used) –  May 27 '21 at 09:32
  • I think you probably want to constrain `T` like this `public class Configuration : IConfiguration where T : IProcessable` and that interface would have functions that allow you to process. Now you can consider every `T` to also be `IProcessable` – Charlieface May 27 '21 at 10:26
  • What is the actual purpose? What is it that you are trying to do? The only way to work with types that are not known at compile time is thru reflection, and that is cumbersome and slow. – JonasH May 27 '21 at 10:39

0 Answers0