1

I'm creating a Container that can store any type of data. To get data from container i make method: AnyTypeResult<T> Get<T>(string name) - that returns from the container data that is registered as "name" and cast it to T. The business logic requires that when we Add data to container as GenericType of DerivedClass it is possible to Get it as GenericType of BaseClass. What is the best way to do it.

The same issue is when we want to cast List<B> to List<A>. It can be done witch Linq extension method Cast<T>

public class A {}
public class B : A {}
public class C<T> {}

var a = new C<A>();
var b = new C<B>();

var x = (C<A>)b; // cannot convert type
H D
  • 31
  • 2

0 Answers0