예제 #1
0
        // ---

        public TypeCollector(string csProjPath, IEnumerable <string> conditinalSymbols, bool disallowInternal, bool isForceUseMap)
        {
            this.csProjPath = csProjPath;
            var compilation = RoslynExtensions.GetCompilationFromProject(csProjPath, conditinalSymbols.Concat(new[] { CodegeneratorOnlyPreprocessorSymbol }).ToArray()).GetAwaiter().GetResult();

            this.typeReferences   = new ReferenceSymbols(compilation);
            this.disallowInternal = disallowInternal;
            this.isForceUseMap    = isForceUseMap;

            targetTypes = compilation.GetNamedTypeSymbols()
                          .Where(x =>
            {
                if (x.DeclaredAccessibility == Accessibility.Public)
                {
                    return(true);
                }
                if (!disallowInternal)
                {
                    return(x.DeclaredAccessibility == Accessibility.Friend);
                }

                return(false);
            })
                          .Where(x =>
                                 ((x.TypeKind == TypeKind.Interface) && x.GetAttributes().Any(x2 => x2.AttributeClass == typeReferences.UnionAttribute)) ||
                                 ((x.TypeKind == TypeKind.Class && x.IsAbstract) && x.GetAttributes().Any(x2 => x2.AttributeClass == typeReferences.UnionAttribute)) ||
                                 ((x.TypeKind == TypeKind.Class) && x.GetAttributes().Any(x2 => x2.AttributeClass == typeReferences.MessagePackObjectAttribute)) ||
                                 ((x.TypeKind == TypeKind.Struct) && x.GetAttributes().Any(x2 => x2.AttributeClass == typeReferences.MessagePackObjectAttribute))
                                 )
                          .ToArray();
        }
예제 #2
0
        public TypeCollector(string csProjPath, IEnumerable <string> conditinalSymbols, bool disallowInternal, bool isForceUseMap)
        {
            this.csProjPath = csProjPath;
            Compilation compilation = RoslynExtensions.GetCompilationFromProject(csProjPath, conditinalSymbols.Concat(new[] { CodegeneratorOnlyPreprocessorSymbol }).ToArray()).GetAwaiter().GetResult();

            Diagnostic[] compilationErrors = compilation.GetDiagnostics().Where(x => x.Severity == DiagnosticSeverity.Error).ToArray();
            if (compilationErrors.Length != 0)
            {
                throw new InvalidOperationException($"detect compilation error:{string.Join("\n", compilationErrors.Select(x => x.ToString()))}");
            }

            this.typeReferences   = new ReferenceSymbols(compilation);
            this.disallowInternal = disallowInternal;
            this.isForceUseMap    = isForceUseMap;

            this.targetTypes = compilation.GetNamedTypeSymbols()
                               .Where(x =>
            {
                if (x.DeclaredAccessibility == Accessibility.Public)
                {
                    return(true);
                }

                if (!disallowInternal)
                {
                    return(x.DeclaredAccessibility == Accessibility.Friend);
                }

                return(false);
            })
                               .Where(x =>
                                      ((x.TypeKind == TypeKind.Interface) && x.GetAttributes().Any(x2 => x2.AttributeClass == this.typeReferences.UnionAttribute)) ||
                                      (x.TypeKind == TypeKind.Class && x.IsAbstract && x.GetAttributes().Any(x2 => x2.AttributeClass == this.typeReferences.UnionAttribute)) ||
                                      ((x.TypeKind == TypeKind.Class) && x.GetAttributes().Any(x2 => x2.AttributeClass == this.typeReferences.MessagePackObjectAttribute)) ||
                                      ((x.TypeKind == TypeKind.Struct) && x.GetAttributes().Any(x2 => x2.AttributeClass == this.typeReferences.MessagePackObjectAttribute)))
                               .ToArray();
        }