vendredi 5 juin 2020

Interface To Generic Casting issue

I'm implementing a custom data store against an in memory state tree and I'm running into some issues with my indexing. My indexes are meant to be covering, so they should return the object not just a position. An index has a name, and a List of objects. Those objects can be different underlying types so the indexed objects are IHasUUID which indicates an item has a UUID.

public class DataIndex
{
    public string Name;
    public IDictionary<string, List<IHasUUID>> Index;
}

public class Indexer
{

    private List<DataIndex> Indexes;
...
    public List<IHasUUID> GetIndexedItems(List<IHasUUID> indexBy) 
    {
        var indexer = GetIndexByKeys<IHasUUID>(indexBy);
        var indexHash = GetHashKey(indexBy);

        return GetIndexValues<IHasUUID>(indexer, indexHash);
    }

    private List<T> GetIndexValues<T>(DataIndex indexBy, string indexHash) where T : IHasUUID
    {
        if (indexBy == null)
            return new List<T>();

        return ((IList<T>)indexBy.Index[indexHash]).ToList();
    }

}

I generate the key to the dictionary using a reflection method where I look at the things being used as the index key and append the type string names

So I ask my Engine to FindRecords, no problem

    public List<T> FindRecords<T>(IHasUUID indexBy) where T : IHasUUID
    {
        var indexedIds = Indexer.GetIndexedItems(new List<IHasUUID>() { indexBy });

        return ((IList<T>)indexedIds).ToList();
    }

Here I run into a wall on the FindRecords return

I have

return ((IList<T>)indexedIds).ToList();

and I tried

    return indexedIds.ToList();

Neither one is able to cast up to T. Is this possible?

Thanks in advance





Aucun commentaire:

Enregistrer un commentaire