コード例 #1
0
        /// <summary>
        /// Enumerate members which will be serialized for the given type.
        ///
        /// Note that these members are generated ahead of time with a source gneerator,
        ///   and cannot be changed at runtime.
        /// </summary>
        public IEnumerable <SerializableMember> EnumerateMembersToSerialize(TypeInfo forType)
        {
            var paired = GetPairedType(forType, SERIALIZER_KIND);

            if (paired == null)
            {
                return(Enumerable.Empty <SerializableMember>());
            }

            var colNamesField = paired.GetFieldNonNull("ColumnNames", PublicStatic);
            var colNames      = (ImmutableArray <string>)colNamesField.GetValueNonNull(null);

            var ret = ImmutableArray.CreateBuilder <SerializableMember>(colNames.Length);

            for (var i = 0; i < colNames.Length; i++)
            {
                var name = colNames[i];

                var colWriterName = $"__Column_{i}";
                var colWriterMtd  = paired.GetMethodNonNull(colWriterName, PublicStatic);

#pragma warning disable CS0618 // This is obsolete to prevent clients from using them, but they are fine for us.
                var emitsDefaultValue = colWriterMtd.GetCustomAttribute <DoesNotEmitDefaultValueAttribute>() == null;
#pragma warning restore CS0618

                var shouldSerializeName = $"__Column_{i}_ShouldSerialize";
                var shouldSerializeMtd  = paired.GetMethod(shouldSerializeName, PublicStatic);
                var shouldSerialize     = (ShouldSerialize?)shouldSerializeMtd;

                var getterName = $"__Column_{i}_Getter";
                var getterMtd  = paired.GetMethodNonNull(getterName, PublicStatic);
                var getter     = Getter.ForMethod(getterMtd);

                var       formatterName = $"__Column_{i}_Formatter";
                var       formatterMtd  = paired.GetMethod(formatterName, PublicStatic);
                Formatter formatter;
                if (formatterMtd == null)
                {
                    // if a method isn't provided, it must be using the default
                    formatter = Utils.NonNull(Formatter.GetDefault(getter.Returns));
                }
                else
                {
                    formatter = Formatter.ForMethod(formatterMtd);
                }


                ret.Add(SerializableMember.ForGeneratedMethod(name, colWriterMtd, getter, formatter, shouldSerialize, emitsDefaultValue));
            }

            return(ret.ToImmutable());
        }