public static void TypedConstant(this LiteralEncoder encoder, TypedConstant constant) { switch (constant.Kind) { case TypedConstantKind.Primitive: encoder.Scalar().Constant(constant.Value); break; case TypedConstantKind.Enum: encoder.Scalar().Constant(constant.Value); break; // This looks more correct, but the code above matches what the C# compiler produces // encoder.TaggedScalar( // type => type.Enum(constant.Type.ToString()), // scalar => scalar.Constant(constant.Value) //); case TypedConstantKind.Type: encoder.Scalar().SystemType(constant.Type.ToString()); break; case TypedConstantKind.Array: { LiteralsEncoder arrayEncoder = encoder.Vector().Count(constant.Values.Length); foreach (var arrayConstant in constant.Values) { arrayEncoder.AddLiteral().TypedConstant(arrayConstant); } break; } } }
static void EncodeLiteral(LiteralEncoder litEnc, CustomAttributeTypedArgument arg) { if (arg.Value is Type type) { // Type reference litEnc.Scalar().SystemType(type.FullName); } else if (arg.Value is ReadOnlyCollection <CustomAttributeTypedArgument> array) { // Array of values var subLitEnc = litEnc.Vector().Count(array.Count); foreach (var el in array) { EncodeLiteral(subLitEnc.AddLiteral(), el); } } else if (arg.Value is null) { if (arg.ArgumentType.IsArray) { litEnc.Scalar().NullArray(); } else { litEnc.Scalar().Constant(null); } } else { // Check argument type supported (ie: simple scalar values) PrimitiveTypeCodeFromSystemTypeCode(arg.Value.GetType()); litEnc.Scalar().Constant(arg.Value); } }
public void LiteralEncoder_Scalar() { var b = new BlobBuilder(); var e = new LiteralEncoder(b); Assert.Same(b, e.Builder); var s = e.Scalar(); AssertEx.Equal(new byte[0], b.ToArray()); Assert.Same(b, s.Builder); }
public static void EncodeConstant(this LiteralEncoder encoder, object constant) { encoder.Scalar().Constant(constant); }