Ejemplo n.º 1
0
        internal TypeDocumentation(AssemblyDocumentation asmDoc, Type type, XElement xml)
            : base(xml)
        {
            if (asmDoc == null)
            {
                throw new ArgumentNullException("asmDoc");
            }

            if (type == null)
            {
                throw new ArgumentNullException("type");
            }

            this.Assembly = asmDoc;
            this.ClrType  = type;

            this.Reset();
        }
        // Public Methods (7) 

        /// <summary>
        /// Creates a new instance from an <see cref="Assembly" /> object.
        /// </summary>
        /// <param name="asm">The underlying assembly object.</param>
        /// <param name="ignoreXmlDocErrors">
        /// Ignore errors if XML documentation could not be loaded or (re-)throw exceptions.
        /// </param>
        /// <returns>The created instance.</returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="asmFile" /> is <see langword="null" />.
        /// </exception>
        public static AssemblyDocumentation FromAssembly(Assembly asm,
                                                         bool ignoreXmlDocErrors = true,
                                                         bool useCache           = true)
        {
            if (asm == null)
            {
                throw new ArgumentNullException("asm");
            }

            if (useCache)
            {
                AssemblyDocumentation cachedAsm;
                if (_CACHE.TryGetValue(asm, out cachedAsm))
                {
                    return(cachedAsm);
                }
            }

            // try find XML file
            FileInfo xmlFile = null;

            try
            {
                var path = asm.Location;
                if (string.IsNullOrWhiteSpace(path) == false)
                {
                    var asmFile = new FileInfo(path);

                    xmlFile = CollectionHelper.SingleOrDefault(asmFile.Directory.GetFiles(),
                                                               f => f.Name.ToLower().Trim() ==
                                                               string.Format("{0}.xml",
                                                                             Path.GetFileNameWithoutExtension(f.Name)).ToLower().Trim());
                }
            }
            catch
            {
                xmlFile = null;
            }

            XElement xml = null;

            if (xmlFile != null)
            {
                try
                {
                    if (xmlFile.Exists)
                    {
                        using (var stream = xmlFile.OpenRead())
                        {
                            xml = XDocument.Load(stream).Root;
                        }
                    }
                }
                catch
                {
                    xml = null;

                    if (ignoreXmlDocErrors == false)
                    {
                        throw;
                    }
                }
            }

            if (xml == null)
            {
                xml = new XElement("doc");
            }

            var result = new AssemblyDocumentation(xml)
            {
                ClrAssembly = asm,
            };

            if (useCache)
            {
                _CACHE[asm] = result;
            }

            return(result);
        }