Esempio n. 1
0
        public static void Run(string assemblyPath, string outputPath)
        {
            var generator = new ApiContractGenerator
            {
                IgnoredNamespaces = { "NUnit.Framework.Internal" }
            };

            var assemblyResolver = new CompositeAssemblyReferenceResolver(
                new GacAssemblyReferenceResolver(),
                new SameDirectoryAssemblyReferenceResolver(Path.GetDirectoryName(assemblyPath)),
                new SameDirectoryAssemblyReferenceResolver(Path.GetDirectoryName(typeof(object).Assembly.Location)));

            using var metadataReferenceResolver = new MetadataReaderReferenceResolver(() => File.OpenRead(assemblyPath), assemblyResolver);
            using var source     = new MetadataReaderSource(File.OpenRead(assemblyPath), metadataReferenceResolver);
            using var outputFile = File.CreateText(outputPath);
            generator.Generate(source, metadataReferenceResolver, new CSharpTextFormatter(outputFile, metadataReferenceResolver));
        }
Esempio n. 2
0
        private static string GenerateContract(ApiContractGenerator generator, StringWriter writer, MemoryStream assemblyStream, IAssemblyReferenceResolver assemblyResolver = null)
        {
            assemblyStream.Seek(0, SeekOrigin.Begin);

            if (assemblyResolver == null)
            {
                assemblyResolver = new CompositeAssemblyReferenceResolver(
                    new GacAssemblyReferenceResolver(),
                    new SameDirectoryAssemblyReferenceResolver(Path.GetDirectoryName(typeof(object).GetTypeInfo().Assembly.Location)));
            }

            using (var metadataReferenceResolver = new MetadataReaderReferenceResolver(() => assemblyStream.CreateReadOnlyView(), assemblyResolver))
                using (var source = new MetadataReaderSource(assemblyStream, metadataReferenceResolver))
                    generator.Generate(source, metadataReferenceResolver, new CSharpTextFormatter(writer, metadataReferenceResolver));

            return(writer.ToString());
        }
Esempio n. 3
0
        public bool Execute()
        {
            if (Assemblies == null || Assemblies.Length == 0)
            {
                return(true);
            }

            var generator = new ApiContractGenerator();

            if (IgnoredNamespaces != null)
            {
                generator.IgnoredNamespaces.UnionWith(IgnoredNamespaces);
            }

            foreach (var assembly in Assemblies)
            {
                var assemblyPath = assembly.GetMetadata("ResolvedAssemblyPath");
                var outputPath   = assembly.GetMetadata("ResolvedOutputPath");
                if (!string.IsNullOrEmpty(outputPath))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(outputPath));
                }

                var assemblyResolver = new CompositeAssemblyReferenceResolver(
                    new GacAssemblyReferenceResolver(),
                    new SameDirectoryAssemblyReferenceResolver(Path.GetDirectoryName(assemblyPath)),
                    new SameDirectoryAssemblyReferenceResolver(Path.GetDirectoryName(typeof(object).GetTypeInfo().Assembly.Location)));

                using var metadataReferenceResolver = new MetadataReaderReferenceResolver(() => File.OpenRead(assemblyPath), assemblyResolver);
                using var source     = new MetadataReaderSource(File.OpenRead(assemblyPath), metadataReferenceResolver);
                using var outputFile = File.CreateText(outputPath);
                generator.Generate(source, metadataReferenceResolver, new CSharpTextFormatter(outputFile, metadataReferenceResolver));
            }

            return(true);
        }