[英]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
输出的json文件将包含带有
num
变量的ClassB
,但不包括classA
Is it possible to serialize Class A <T>
? 是否可以序列化
Class A <T>
?
To note, I am using UnityEngine.JsonUtility 注意,我正在使用UnityEngine.JsonUtility
No, afaik you can't directly serialize genrics but rather have to inherit from the generic class in order to make it work: 不,afaik您不能直接序列化泛型,而是必须从泛型类继承才能使其正常工作:
public class ClassA <T>
{
public T Name;
}
[Serializable]
public class SerializeableClassA : ClassA<MyEnum> { }
[Serializable]
public class ClassB
{
public int num;
public SerializeableClassA classA;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.