hi, its me again.
i would like to ask why there is an error when i use an "int" type in Fmt.Serialize? The detail error is """The type 'int' must be a reference type in order to use it as parameter 'T' in the generic type or method 'Kds.Serialization.Formatter<F>.Serialize<T>(T)""".
the same error also occurs when using Fmt.Deserialize.
my code goes like this.
public class Sample
{
internal int id;
internal string code;
public int Id {
get { return id; }
set { id = value; }
}
public string Code {
get { return code; }
set { code = value; }
}
}
public class SampleField: ReferenceField<Sample>
{
public SampleField(Formatter fmt, bool isDefault) : base(fmt, isDefault) { }
If you look at the Serialize overloads in the source code, you will see that there are two ways to serialize value types:
a) Formatter.Serialize<T>(T value) where T is a *nullable" value type (e.g. int? in C#)
b) Formatter.Serialize<T>(ref T value) where T is a regular value type.
The serializer is meant to deal with nulls even for structs.
You can just assign your int to a temporary nullable int, or change your int declaration to int?
Have a look at the getting started demo.
Karl
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
hi, its me again.
i would like to ask why there is an error when i use an "int" type in Fmt.Serialize? The detail error is """The type 'int' must be a reference type in order to use it as parameter 'T' in the generic type or method 'Kds.Serialization.Formatter<F>.Serialize<T>(T)""".
the same error also occurs when using Fmt.Deserialize.
my code goes like this.
public class Sample
{
internal int id;
internal string code;
public int Id {
get { return id; }
set { id = value; }
}
public string Code {
get { return code; }
set { code = value; }
}
}
public class SampleField: ReferenceField<Sample>
{
public SampleField(Formatter fmt, bool isDefault) : base(fmt, isDefault) { }
protected override void SerializeValue(Sample value) {
Fmt.Serialize<int>(value.Id); <----- Error -----
Fmt.Serialize<string>(value.Code);
}
protected override void DeserializeInstance(ref Sample instance) {
if (instance == null)
instance = new Sample();
}
protected override void DeserializeMembers(Sample instance) {
Fmt.Deserialize<string>(ref instance.id); <----- Error -----
Fmt.Deserialize<string>(ref instance.code);
}
protected override void SkipValue() {
if (Fmt.Skip<int>())
Fmt.Skip<string>();
}
}
thank you very much!
If you look at the Serialize overloads in the source code, you will see that there are two ways to serialize value types:
a) Formatter.Serialize<T>(T value) where T is a *nullable" value type (e.g. int? in C#)
b) Formatter.Serialize<T>(ref T value) where T is a regular value type.
The serializer is meant to deal with nulls even for structs.
You can just assign your int to a temporary nullable int, or change your int declaration to int?
Have a look at the getting started demo.
Karl
thank you very much. have a nice day! =)