0

I have the following classes in order to build a List of Flow

public abstract class Flow
{
    public string Name { get; set; }
}
    
public class Flow<T> : Flow
{
    public Entity<Personnel, T> MyEntity { get; set; }
}

Here is my list of Flow (each Flow are of type Flow, where T can be different)

private List<Flow> _flow = new List<Flow>();

Then I have added some Flow into my List :

_flow.Add(new Flow<Type1>() { MyEntity = new ... , FileName = ... });
_flow.Add(new Flow<Type2>() { MyEntity = new ... , FileName = ... });
_flow.Add(new Flow<Type3>() { MyEntity = new ... , FileName = ... });
_flow.Add(new Flow<Type4>() { MyEntity = new ... , FileName = ... });

And finally, I want to call the function Entity.Map on each element of the list as below but this is not working :

foreach (var flow in _flow)
{
     var currentResult= flow.MyEntity.Map();
     var currentFilename = flow.FileName;
     ...
}

It tells me "Flow does not contain a definition for MyEntity"

Jacob Shanley
  • 893
  • 1
  • 8
  • 19
Gosfly
  • 1,240
  • 1
  • 8
  • 14
  • 1
    Does this answer your question? [How to create List of open generic type of class?](https://stackoverflow.com/questions/58570948/how-to-create-list-of-open-generic-type-of-classt) and [How to do generic polymorphism on open types in C#?](https://stackoverflow.com/questions/58247604/how-to-do-generic-polymorphism-on-open-types-in-c/) and [C# generic inheritance and covariance part 2](https://stackoverflow.com/questions/14263964/c-sharp-generic-inheritance-and-covariance-part-2/) –  Jun 16 '21 at 14:39
  • I need to access a child attribute which is MyEntity, this is not a method – Gosfly Jun 16 '21 at 14:46
  • 1
    @Gosfly "this is not a method" ohh man, this is apparently been a long day. Sorry for that – Mong Zhu Jun 16 '21 at 14:47
  • 1
    the general thought is still valid. You need to hide the generic dependency. make an abstract method in the base class, override it in the child class and let it return whatever `MyEntity.Map()` returns. What does it return? `Personnel` ? or also a generic `T` ? – Mong Zhu Jun 16 '21 at 14:50
  • @Gosfly Method or property or field, this does not change what is stated in the suggested duplicates. [Polymorphism](https://stackoverflow.com/questions/1031273/#58197730) | [Abstraction](https://stackoverflow.com/questions/58765776/#58766026) | [OOP](https://www.c-sharpcorner.com/UploadFile/mkagrahari/introduction-to-object-oriented-programming-concepts-in-C-Sharp) –  Jun 16 '21 at 14:53
  • @MongZhu Unfortunately, it returns another type that uses the generic T – Gosfly Jun 16 '21 at 14:54
  • 1
    "Unfortunately, it returns another type that uses the generic T " that is bad .... Does T have any restriction? may be another interface? or a baseclass of some sort? It has to have something of that sort, otherwise you are screwed and have to circumvent the returning of a value. Because it is simply not know at compiletime! – Mong Zhu Jun 16 '21 at 14:55
  • @MongZhu What do you mean ? it returns a EntityMapped which is a class that doesn't inherit from anything. – Gosfly Jun 16 '21 at 14:59
  • 1
    @Gosfly please replace `var` in your sample with explicit type (I really wish SO can limit usage of `var` in C# code samples to C# gold badge holders or just Jon Skeet). It feels like code in the question treats `var ` as `dynamic` (at best) which may incorrectly imply you have no idea what you are talking about... – Alexei Levenkov Jun 16 '21 at 15:15
  • @Gosfly do the classes Type1, 2,3 and 4 inherit from the same class or implement a common interface? – Mong Zhu Jun 16 '21 at 20:44

1 Answers1

1

The compiler is correct - Flow does not contain a methods called MyEntity - Flow<T> does.

In addition, you're adding generic "flows" that have different generic parameters- how do you know what the type of MyEntity is if all of the elements all have different entity types? In other words, what is the type of currentResult?

You could use dynamic and defer all of this binding until runtime, which means that you have to ensure that every element in _flow has a MyEntity property, but you won't know the type of that variable at compile type (meaning it will have to be dynamic as well).

All that so say there's not an obvious solution - you have a list of loosely-related entities - meaning they're related only by the fact that they're all instances of a generic type, but you don't know what those types are at compile-time.

D Stanley
  • 149,601
  • 11
  • 178
  • 240