예제 #1
0
        protected override IEnumerable <string> GetValidationErrors()
        {
            try
            {
                var tree = (CSharpSyntaxTree)CSharpSyntaxTree.ParseText(Code);

                var portableExecutableReferences =
                    ReferencedAssemblies.Select(path => MetadataReference.CreateFromFile(path)).ToArray();
                var syntaxTrees = new[] { tree }.Concat(OtherCodeDependencies.Select(c => CSharpSyntaxTree.ParseText(c)));

                var compilation = CSharpCompilation.Create(
                    Guid.NewGuid().ToString(),
                    syntaxTrees,
                    portableExecutableReferences,
                    new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
                    );

                var assembly = compilation.CompileToAssembly();
                var model    = compilation.GetSemanticModel(tree);

                var validationVisitor = new CSharpCodeSafetyVisitor(this, model);
                validationVisitor.Visit(tree.GetCompilationUnitRoot());
                Validator.Validate(compilation, tree, model, assembly);
                return(Enumerable.Empty <string>());
            }
            catch (CodeValidationException ex)
            {
                return(new[] { ex.Message });
            }
        }