0

I have an interface which uses a generic type extending another interface with the where clause. Example:

public interface IEntityDataExtractor<T> where T : IEntity
{
    public string[] ExtractRawStringData(T entity);
}

The IEntity interface and a type extending this interface look like this:

public interface IEntity
{
    int Id { get; set; }
}

public class Tender : IEntity
{
    ...
}

When i try to implement a concrete EntityDataExtractor for the Tender entity, i get the error that:

enter image description here

I do not understand why it is not accepted. I declare that the generic types used for the implementations should be of type IEntity some time down the hierarchy with the where clause. Tender implements, and therefore is an, IEntity. Now i understand that i could solve this by simply having a completely unrestricted generic type T, but the reason i am doing all this is because i need to store a list of the generic IEntityDataExtractor interfaces, with the assigned ones being specific, because i want the type to be known within the specific implementation (e.g. TenderDataExtractor) so i do not have to use forced casts or type conversion in the ExtractRawStringData method. The result i need to achieve should look somewhat like this logic wise:

public class SomeClass
{
    public List<IEntityDataExtractor<IEntity>> extractors { get; set; }
}

...Instantiating code

public void buildSomeClass()
{
    List<IEntityDataExtractor<IEntity>> extractors = new();
    extractors.Add(new TenderDataExtractor());
    extractors.Add(new SomeOtherDataExtractor()); // etc.
    SomeClass someClass = new();
    someClass.extractors = extractors;
    return someClass;
}

...Example of the tender data extractor

public class TenderDataExtractor : IEntityDataExtractor<Tender>
{
    public string[] ExtractRawStringData(Tender entity)
    {
        // I can directly access members of Tender, not just IEntity, 
        // without having to do manual casts or type conversions here
    }
}

The thing is, i cannot need to know the specific type at time of declaration (list in SomeClass for example), but will know it at time of assignment. For SomeClass it should just hand over any object of type IEntity to them, but the actual implementation called should know at compile them as shown above. The exact case is a bit different for the declaration part, but for brevity i made a simpler example. Is this not possible in C#?

Furious Gamer
  • 359
  • 1
  • 3
  • 16
  • 3
    Are you sure you don't have two `Tender` classes? – DavidG Dec 05 '22 at 22:14
  • 2
    I was not able to repro your error with `TenderDataExtractor` (compiles just fine for me - [demo](https://dotnetfiddle.net/uie0cO)), but even if you fix your compilation error - the `List>` will not work - see [here](https://stackoverflow.com/a/17440148/2501279) why. – Guru Stron Dec 05 '22 at 22:16
  • @DavidG I checked, but there is only one Tender class present in my project and IDE is also correctly referring to the right namespace. – Furious Gamer Dec 05 '22 at 22:17
  • @GuruStron I see, well i somewhat feared that to be honest. But i wanted to get there first and try it, as i'm not an expert in C# so to speak. Guess my idea of a more generalized solution will not work in this case (without the use of such things as reflection at least i guess, which i want to avoid). – Furious Gamer Dec 05 '22 at 22:18
  • As for current compilation errors - also check if you have multiple `IEntity` interfaces. – Guru Stron Dec 05 '22 at 22:21
  • @GuruStron I checked, there is only one IEntity interface and file in the project. However, i am pretty bummed this won't work out anyway. I wasted a lot of time on this rewrite so far, but there is only me to blame for. Guess i will stick to the hardcoded version then :/ – Furious Gamer Dec 05 '22 at 22:48
  • @FuriousGamer This compiled for me as well . . . what version of .Net are you targeting here? – Vic F Dec 06 '22 at 01:28
  • @FuriousGamer Are you sure the Tender class actually implements the interface IEntity? By having a property Id declared in it? – James Braz Dec 06 '22 at 02:51

0 Answers0