Working general lists of base types and inherited types

Basically, I use ORM (specifically LLBLGen), which creates entity objects for all my tables. All these types of objects are inherited from the base class (or indeed their set). I want to create an extension method that takes a list of the base class and returns some string, but I want to pass the inherited types without an explicit cast.

For example, I have a function like:

string GetString(List<EntityBase2> list); // also tried List<IEntityCore>, which the base class implements

And I want to give him something like this:

List<ProductEntity> products = ... // populate it

string v = GetString(products);

But I get compiler errors.

How can I create this helper method. I want to avoid casting if I can, but if this is the best way, at least I could get some confirmation.

+3
1

:

string GetString<T>(List<T> list) where T : IEntityCore {...}

, , IEntityCore. , , . 2.0.

+3

Source: https://habr.com/ru/post/1733790/


All Articles