Jake Matthews
Jake Matthews

Reputation: 115

C# Unable to serialize Class<T>

I am unable to serialize the following class...

[Serializable]
public class ClassA <T>
{
    public T Name;
}

when I attempt to serialize this class as an example...

[Serializable]
public class ClassB
{
    public int num;
    public ClassA<MyEnum> classA;
}

The json file outputted will contain ClassB with the num variable but will not include classA

Is it possible to serialize Class A <T> ?

To note, I am using UnityEngine.JsonUtility

Upvotes: 0

Views: 68

Answers (1)

derHugo
derHugo

Reputation: 90659

No, afaik you can't directly serialize genrics but rather have to inherit from the generic class in order to make it work:

public class ClassA <T>
{
    public T Name;
}

[Serializable]
public class SerializeableClassA : ClassA<MyEnum> { }

[Serializable]
public class ClassB
{
    public int num;
    public SerializeableClassA classA;
}

Upvotes: 2

Related Questions