Пример #1
0
 /// <summary>
 /// Returns a compile-time constant default expression for the given type model.
 /// </summary>
 public static string GetTypeDefaultExpression(this ITypeModel typeModel)
 {
     if (typeModel.IsNonNullableClrValueType())
     {
         return($"default({typeModel.GetCompilableTypeName()})");
     }
     else
     {
         return("null");
     }
 }
Пример #2
0
        public static string GetNullableAnnotationTypeName(this ITypeModel typeModel, FlatBufferSchemaType context)
        {
            var typeName = typeModel.GetCompilableTypeName();

            if (typeModel.ClassifyContextually(context).IsOptionalReference())
            {
                typeName += "?";
            }

            return(typeName);
        }
    internal static string CreateLoopStatic(
        ITypeModel typeModel,
        FlatBufferSerializerOptions options,
        string vectorVariableName,
        string numberofItemsVariableName,
        string expectedVariableName,
        string body)
    {
        string ListBody(string variable)
        {
            return($@"
                int i;
                for (i = 0; i < {numberofItemsVariableName}; i = unchecked(i + 1))
                {{
                    var {expectedVariableName} = {variable}[i];
                    {body}
                }}");
        }

        if (options.Devirtualize)
        {
            return($@"
                if ({vectorVariableName} is {typeModel.GetCompilableTypeName()}[] array)
                {{
                    {ArrayVectorTypeModel.CreateLoopStatic(options, "array", "current", body)}
                }}
                else if ({vectorVariableName} is List<{typeModel.GetCompilableTypeName()}> realList)
                {{
                    {ListBody("realList")}
                }}
                else
                {{
                    {ListBody(vectorVariableName)}
                }}");
        }
        else
        {
            return(ListBody(vectorVariableName));
        }
    }