I have a requirement to perform YAML file version conversion. The method I use is to use features to identify the versions of some fields in the entity class, and then verify the version during serialization to choose whether to serialize this field. Is there any method? The structure of the entity class will look like:
Class A{
[Version (“version 1”)]
Int a;
}
Among them, Version is a custom feature that I use to identify the version
I tried using reflection to grab features and determine whether to write to a file
Type type = obj.GetType();
foreach (var property in type.GetProperties())
{
var attribute = property.GetCustomAttribute<VersionAttribute>();
if (VersionUtil.ValidateVersion(type, attribute, mainViewModel.ConvertVersion))
{
serializer.Serialize(writer, new Dictionary<string, object> { { property.Name, property.GetValue(obj) } });
}
}
But if the serialized object is a collection, it will result in one less line indentation
mo-harlan is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.