/// <summary>
        /// Returns the raw custom debug info or null if there was an error
        /// </summary>
        /// <param name="metadata">Metadata</param>
        /// <param name="context">Writer context</param>
        /// <param name="method">Method</param>
        /// <param name="customDebugInfos">Custom debug infos to write</param>
        /// <returns></returns>
        public static byte[] Write(Metadata metadata, MethodDef method, PdbCustomDebugInfoWriterContext context, IList <PdbCustomDebugInfo> customDebugInfos)
        {
            var writer = new PdbCustomDebugInfoWriter(metadata, method, context);

            return(writer.Write(customDebugInfos));
        }
        void Write(MethodDef method, List <PdbCustomDebugInfo> cdiBuilder)
        {
            uint rid = metadata.GetRid(method);

            if (rid == 0)
            {
                Error("Method {0} ({1:X8}) is not defined in this module ({2})", method, method.MDToken.Raw, module);
                return;
            }

            var info        = new CurrentMethod(this, method, instrToOffset);
            var body        = method.Body;
            var symbolToken = new MDToken(MD.Table.Method, rid);

            writer.OpenMethod(symbolToken);
            seqPointsHelper.Write(this, info.Method.Body.Instructions);

            var pdbMethod = body.PdbMethod;

            if (pdbMethod == null)
            {
                body.PdbMethod = pdbMethod = new PdbMethod();
            }
            var scope = pdbMethod.Scope;

            if (scope == null)
            {
                pdbMethod.Scope = scope = new PdbScope();
            }
            if (scope.Namespaces.Count == 0 && scope.Variables.Count == 0 && scope.Constants.Count == 0)
            {
                if (scope.Scopes.Count == 0)
                {
                    // We must open at least one sub scope (the sym writer creates the 'method' scope
                    // which covers the whole method) or the native PDB reader will fail to read all
                    // sequence points.
                    writer.OpenScope(0);
                    writer.CloseScope((int)info.BodySize);
                }
                else
                {
                    var scopes = scope.Scopes;
                    int count  = scopes.Count;
                    for (int i = 0; i < count; i++)
                    {
                        WriteScope(ref info, scopes[i], 0);
                    }
                }
            }
            else
            {
                // C++/.NET (some methods)
                WriteScope(ref info, scope, 0);
            }

            GetPseudoCustomDebugInfos(method.CustomDebugInfos, cdiBuilder, out var asyncMethod);
            if (cdiBuilder.Count != 0)
            {
                customDebugInfoWriterContext.Logger = GetLogger();
                var cdiData = PdbCustomDebugInfoWriter.Write(metadata, method, customDebugInfoWriterContext, cdiBuilder);
                if (cdiData != null)
                {
                    writer.SetSymAttribute(symbolToken, "MD2", cdiData);
                }
            }

            if (asyncMethod != null)
            {
                if (!writer.SupportsAsyncMethods)
                {
                    Error("PDB symbol writer doesn't support writing async methods");
                }
                else
                {
                    WriteAsyncMethod(ref info, asyncMethod);
                }
            }

            writer.CloseMethod();
        }