Any reason why concrete type inheriting from a generic type implementing IEnumerable is not supported?

Discussion and help for Easy Save 3
Post Reply
TheQuack
Posts: 1
Joined: Fri Aug 05, 2022 4:11 am

Any reason why concrete type inheriting from a generic type implementing IEnumerable is not supported?

Post by TheQuack »

Considering this code, any way to work around the limitation that generic base type implementing IEnumerable is not allowed but is generic base not implementing IEnumerable is allowed?

Code: Select all

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SaveTest : MonoBehaviour
{
    void Start()
    {
        var list = new ConcreteList();
        ES3.Save("save", list);
        var list2 = ES3.Load("save", new ConcreteList());
        list2.Print();
    }
}

[Serializable]
public class ConcreteType
{
    public string name = "test";
}

[Serializable]
public class ThisWorkList<T>
{
    [SerializeField] protected List<T> list = new();
    public void Print() => Debug.Log(list.ToString());
}

[Serializable]
public class ThisDontWorkList<T> : IEnumerable<T> 
{
    [SerializeField] protected List<T> list = new();
    public void Print() => Debug.Log(list.ToString());
    public IEnumerator<T> GetEnumerator() => list.GetEnumerator();
    IEnumerator IEnumerable.GetEnumerator() => list.GetEnumerator();
}

[Serializable]
public class ConcreteList : ThisDontWorkList<ConcreteType>
{
}
User avatar
Joel
Moodkie Staff
Posts: 4826
Joined: Wed Nov 07, 2012 10:32 pm

Re: Any reason why concrete type inheriting from a generic type implementing IEnumerable is not supported?

Post by Joel »

Hi there,

In this case you would need to inherit from, or convert to and from a type which is supported.

Alternatively if you private message me your invoice number I can send over a version which will actually allow it to be serialized, ignoring serialization of the IEnumerable parts.

All the best,
Joel
Joel @ Moodkie Interactive
Purchase Easy Save | Contact | Guides | Docs | Getting started
Post Reply