public override CodeGeneratedMethod CreateSerializeMethodBody(SerializationCodeGenContext context) { List <string> body = new List <string>(); for (int i = 0; i < this.Members.Count; ++i) { var memberInfo = this.Members[i]; string propertyAccessor = $"{context.ValueVariableName}.{memberInfo.PropertyInfo.Name}"; if (!memberInfo.ItemTypeModel.ClrType.IsValueType) { // Force members of structs to be non-null. propertyAccessor += $" ?? new {CSharpHelpers.GetCompilableTypeName(memberInfo.ItemTypeModel.ClrType)}()"; } var propContext = context.With( offsetVariableName: $"({memberInfo.Offset} + {context.OffsetVariableName})", valueVariableName: $"({propertyAccessor})"); body.Add(propContext.GetSerializeInvocation(memberInfo.ItemTypeModel.ClrType) + ";"); } return(new CodeGeneratedMethod { MethodBody = string.Join("\r\n", body) }); }
public CodeGeneratedMethod CreateSerializeMethodBody(SerializationCodeGenContext context) { string invocation = context .With(valueVariableName: GetConvertToUnderlyingInvocation(context.ValueVariableName)) .GetSerializeInvocation(typeof(TUnderlying)); return(new CodeGeneratedMethod($"{invocation};") { IsMethodInline = true, }); }
public override CodeGeneratedMethod CreateSerializeMethodBody(SerializationCodeGenContext context) { // NULL FORGIVENESS string variableName = context.ValueVariableName; string body = context.With(valueVariableName: $"{variableName}!.Value").GetSerializeInvocation(this.underlyingType); return(new CodeGeneratedMethod($"{body};") { IsMethodInline = true, }); }
public override CodeGeneratedMethod CreateSerializeMethodBody(SerializationCodeGenContext context) { Type underlyingType = this.underlyingTypeModel.ClrType; string underlyingTypeName = CSharpHelpers.GetCompilableTypeName(underlyingType); string body = context.With(valueVariableName: $"({underlyingTypeName}){context.ValueVariableName}") .GetSerializeInvocation(underlyingType); return(new CodeGeneratedMethod($"{body};") { IsMethodInline = true, }); }
public override CodeGeneratedMethod CreateSerializeMethodBody(SerializationCodeGenContext context) { List <string> body = new List <string>(); body.Add($"Span<byte> scopedSpan = {context.SpanVariableName}.Slice({context.OffsetVariableName}, {this.PhysicalLayout[0].InlineSize});"); for (int i = 0; i < this.Members.Count; ++i) { var memberInfo = this.Members[i]; string propertyAccessor = $"{context.ValueVariableName}.{memberInfo.PropertyInfo.Name}"; if (memberInfo.CustomGetter is not null) { propertyAccessor = $"{context.ValueVariableName}.{memberInfo.CustomGetter}"; } var propContext = context.With( spanVariableName: "scopedSpan", offsetVariableName: $"{memberInfo.Offset}", valueVariableName: $"{propertyAccessor}"); string invocation = propContext.GetSerializeInvocation(memberInfo.ItemTypeModel.ClrType) + ";"; if (!memberInfo.ItemTypeModel.ClrType.IsValueType) { // Force members of structs to be non-null. int start = memberInfo.Offset; int length = memberInfo.Length; invocation = $@" var index{i}Value = {propertyAccessor}; if (index{i}Value is null) {{ scopedSpan.Slice({start}, {length}).Clear(); }} else {{ {invocation} }} "; } body.Add(invocation); } return(new CodeGeneratedMethod(string.Join("\r\n", body))); }