// Generates an expression that creates an NbtTag for given property of a directly-mappable types.
// Directly-mappable types are: primitives, enums, byte[], int[], and string.
// HandlePrimitiveOrEnum is actually more efficient (and preferred by NbtCompiler) for primitives and enums,
// because it skips boxing and NullPolicy checks. This one is pretty much only used for byte[]/int[]/string.
public override Expression HandleDirectlyMappedType(string tagName, PropertyInfo property, NullPolicy selfPolicy)
{
// declare a local var, which will hold the property's value
ParameterExpression varValue = Expression.Parameter(property.PropertyType);
// Fallback path, in case value is null and NullPolicy is InsertDefaults
Expression defaultVal = Expression.Constant(SerializationUtil.GetDefaultValue(property.PropertyType));
// varRootTag.Add( new NbtTag(tagName, <defaultVal>) );
Expression defaultValExpr =
Expression.Call(varRootTag,
NbtCompoundAddMethod,
MakeNbtTagCtor(property.PropertyType,
Expression.Constant(tagName, typeof(string)),
defaultVal));
// varRootTag.Add( new NbtTag(tagName, <varValue>) );
Expression makeTagExpr =
Expression.Call(varRootTag,
NbtCompoundAddMethod,
MakeNbtTagCtor(property.PropertyType,
Expression.Constant(tagName, typeof(string)),
varValue));
// Getter for the property value
Expression getPropertyExpr = Expression.MakeMemberAccess(argValue, property);
// generate the appropriate enclosing expressions, depending on NullPolicy
return(NbtCompiler.MakeNullHandler(varValue, getPropertyExpr, selfPolicy,
makeTagExpr, defaultValExpr, MakePropertyNullMessage(property)));
}