4 using System.Reflection;
7 using SharpYaml.Events;
8 using SharpYaml.Serialization;
9 using SiliconStudio.Core;
10 using SiliconStudio.Core.Yaml;
12 namespace SiliconStudio.Assets.Serializers
14 [YamlSerializerFactory]
17 public override bool CanVisit(Type type)
28 for (Type t = type; t != null; t = t.BaseType)
29 if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(
PropertyKey<>))
35 public override object ConvertFrom(ref ObjectContext objectContext, Scalar fromScalar)
37 var lastDot = fromScalar.Value.LastIndexOf(
'.');
41 var className = fromScalar.Value.Substring(0, lastDot);
43 var containingClass = objectContext.SerializerContext.TypeFromTag(
"!" + className);
44 if (containingClass == null)
46 throw new YamlException(fromScalar.Start, fromScalar.End,
"Unable to find class from tag [{0}]".ToFormat(className));
49 var propertyName = fromScalar.Value.Substring(lastDot + 1);
50 var propertyField = containingClass.GetField(propertyName, BindingFlags.Public | BindingFlags.Static);
51 if (propertyField == null)
53 throw new YamlException(fromScalar.Start, fromScalar.End,
"Unable to find property [{0}] in class [{1}]".ToFormat(propertyName, containingClass.Name));
56 return propertyField.GetValue(null);
59 protected override void WriteScalar(ref ObjectContext objectContext, ScalarEventInfo scalar)
63 scalar.IsPlainImplicit =
true;
64 base.WriteScalar(ref objectContext, scalar);
67 public override string ConvertTo(ref ObjectContext objectContext)
69 var propertyKey = (
PropertyKey)objectContext.Instance;
71 return AssetPropertyKeyNameResolver.ComputePropertyKeyName(objectContext.SerializerContext, propertyKey);
A class that represents a tag propety.