コード例 #1
0
        /// <summary>
        /// 根据DLL路径添加单个引用
        /// </summary>
        /// <param name="path">DLL文件路径</param>
        /// <param name="excludePaths">需要排除的依赖文件路径</param>
        public virtual void AddReferencesFromDllFile(string path, params string[] excludePaths)
        {
            if (excludePaths.Length == 0)
            {
                OtherReferencesFromFile[Path.GetFileNameWithoutExtension(path)] = MetadataReference.CreateFromFile(path);
            }
            else
            {
                HashSet <string> exclude;
                if (excludePaths == default)
                {
                    exclude = new HashSet <string>();
                }
                else
                {
                    exclude = new HashSet <string>(excludePaths);
                }

                if (!exclude.Contains(path))
                {
                    OtherReferencesFromFile[Path.GetFileNameWithoutExtension(path)] = MetadataReference.CreateFromFile(path);
                }
            }
        }
コード例 #2
0
        // 根据生成的类,动态编译把json转成protobuf
        private static void ExportExcelProtobuf(ConfigType configType)
        {
            string            classPath   = GetClassDir(configType);
            List <SyntaxTree> syntaxTrees = new List <SyntaxTree>();
            List <string>     protoNames  = new List <string>();

            foreach (string classFile in Directory.GetFiles(classPath, "*.cs"))
            {
                protoNames.Add(Path.GetFileNameWithoutExtension(classFile));
                syntaxTrees.Add(CSharpSyntaxTree.ParseText(File.ReadAllText(classFile)));
            }

            List <PortableExecutableReference> references = new List <PortableExecutableReference>();

            Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
            foreach (Assembly assembly in assemblies)
            {
                try
                {
                    if (assembly.IsDynamic)
                    {
                        continue;
                    }

                    if (assembly.Location == "")
                    {
                        continue;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }

                PortableExecutableReference reference = MetadataReference.CreateFromFile(assembly.Location);
                references.Add(reference);
            }

            CSharpCompilation compilation = CSharpCompilation.Create(
                null,
                syntaxTrees.ToArray(),
                references.ToArray(),
                new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

            using MemoryStream memSteam = new MemoryStream();

            EmitResult emitResult = compilation.Emit(memSteam);

            if (!emitResult.Success)
            {
                StringBuilder stringBuilder = new StringBuilder();
                foreach (Diagnostic t in emitResult.Diagnostics)
                {
                    stringBuilder.AppendLine(t.GetMessage());
                }
                throw new Exception($"动态编译失败:\n{stringBuilder}");
            }

            memSteam.Seek(0, SeekOrigin.Begin);

            Assembly ass = Assembly.Load(memSteam.ToArray());

            string dir = GetProtoDir(configType);

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            foreach (string protoName in protoNames)
            {
                Type type    = ass.GetType($"ET.{protoName}Category");
                Type subType = ass.GetType($"ET.{protoName}");
                Serializer.NonGeneric.PrepareSerializer(type);
                Serializer.NonGeneric.PrepareSerializer(subType);


                string json        = File.ReadAllText(Path.Combine(string.Format(jsonDir, configType), $"{protoName}.txt"));
                object deserialize = BsonSerializer.Deserialize(json, type);

                string path = Path.Combine(dir, $"{protoName}Category.bytes");

                using FileStream file = File.Create(path);
                Serializer.Serialize(file, deserialize);
            }
        }
コード例 #3
0
            private static void CreateSharedAssemblies()
            {
                string sharedAssembliesPath = Path.Combine(ScriptRoot, "SharedAssemblies");

                if (Directory.Exists(sharedAssembliesPath))
                {
                    Directory.Delete(sharedAssembliesPath, true);
                }

                Directory.CreateDirectory(sharedAssembliesPath);

                string secondaryDependencyPath = Path.Combine(sharedAssembliesPath, "SecondaryDependency.dll");

                string      primaryReferenceSource   = @"
using SecondaryDependency;

namespace PrimaryDependency
{
    public class Primary
    {
        public string GetValue()
        {
            var secondary = new Secondary();
            return secondary.GetSecondaryValue();
        }
    }
}";
                string      secondaryReferenceSource = @"
namespace SecondaryDependency
{
    public class Secondary
    {
        public string GetSecondaryValue()
        {
            return ""secondary type value"";
        }
    }
}";
                var         secondarySyntaxTree      = CSharpSyntaxTree.ParseText(secondaryReferenceSource);
                Compilation secondaryCompilation     = CSharpCompilation.Create("SecondaryDependency", new[] { secondarySyntaxTree })
                                                       .WithReferences(MetadataReference.CreateFromFile(typeof(object).Assembly.Location))
                                                       .WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

                secondaryCompilation.Emit(secondaryDependencyPath);

                var         primarySyntaxTree  = CSharpSyntaxTree.ParseText(primaryReferenceSource);
                Compilation primaryCompilation = CSharpCompilation.Create("PrimaryDependency", new[] { primarySyntaxTree })
                                                 .WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
                                                 .WithReferences(MetadataReference.CreateFromFile(secondaryDependencyPath), MetadataReference.CreateFromFile(typeof(object).Assembly.Location));

                primaryCompilation.Emit(Path.Combine(sharedAssembliesPath, "PrimaryDependency.dll"));
            }
コード例 #4
0
        public void RollbackProcess_ShouldRollbackAllCompileErrors()
        {
            var syntaxTree = CSharpSyntaxTree.ParseText(@"using System;

namespace ExampleProject
{
    public class Calculator
    {
        public string Subtract(string first, string second)
        {
            if(Environment.GetEnvironmentVariable(""ActiveMutation"") == ""6"") {
                while (first.Length > 2)
                {
                    return first - second;
                } 
                while (first.Length < 2)
                {
                    return second + first;
                }
                return null;
            } else {
                while (first.Length > 2)
                {
                    return first + second;
                } 
                while (first.Length < 2)
                {
                    return System.Environment.GetEnvironmentVariable(""ActiveMutation"") == ""7"" ? second - first : second + first;
                }
                return null;
            }
        }
    }
}");
            var root       = syntaxTree.GetRoot();

            var mutantIf1 = root.DescendantNodes().OfType <IfStatementSyntax>().First();

            root = root.ReplaceNode(
                mutantIf1,
                mutantIf1.WithAdditionalAnnotations(new SyntaxAnnotation("MutationIf", "6"))
                );
            var mutantIf2 = root.DescendantNodes().OfType <ConditionalExpressionSyntax>().First();

            root = root.ReplaceNode(
                mutantIf2,
                mutantIf2.WithAdditionalAnnotations(new SyntaxAnnotation("MutationConditional", "7"))
                );

            var annotatedSyntaxTree = root.SyntaxTree;

            var compiler = CSharpCompilation.Create("TestCompilation",
                                                    syntaxTrees: new Collection <SyntaxTree>()
            {
                annotatedSyntaxTree
            },
                                                    options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary),
                                                    references: new List <PortableExecutableReference>()
            {
                MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(Environment).Assembly.Location)
            });

            var target = new RollbackProcess();

            using (var ms = new MemoryStream())
            {
                var compileResult = compiler.Emit(ms);

                var fixedCompilation = target.Start(compiler, compileResult.Diagnostics);

                var rollbackedResult = fixedCompilation.Compilation.Emit(ms);

                rollbackedResult.Success.ShouldBeTrue();
                fixedCompilation.RollbackedIds.ShouldBe(new Collection <int> {
                    6, 7
                });
            }
        }
コード例 #5
0
ファイル: Form1.cs プロジェクト: 7UKECREAT0R/CSharpTester
        // events
        private void RunToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SyntaxTree systree = CSharpSyntaxTree.ParseText(input.Text);

            Assembly[] assemblies = new List <Assembly> {
                typeof(object).GetTypeInfo().Assembly,
                typeof(Console).GetTypeInfo().Assembly,
                typeof(Enumerable).GetTypeInfo().Assembly,
                typeof(File).GetTypeInfo().Assembly,
                typeof(Assembly).GetTypeInfo().Assembly,
                typeof(CSharpArgumentInfo).GetTypeInfo().Assembly,
                typeof(CSharpSyntaxTree).GetTypeInfo().Assembly,
                typeof(System.Dynamic.CallInfo).GetTypeInfo().Assembly,
                typeof(RuntimeBinderException).GetTypeInfo().Assembly,
                typeof(System.Dynamic.ConvertBinder).GetTypeInfo().Assembly,
                typeof(ExpressionType).GetTypeInfo().Assembly,
                typeof(MessageBox).GetTypeInfo().Assembly,
                //Assembly.Load(new AssemblyName("Microsoft.CSharp")),
                //Assembly.Load(new AssemblyName("netstandard")),
                //Assembly.Load(new AssemblyName("System.Dynamic.Runtime")),
                //Assembly.Load(new AssemblyName("System.Runtime")),
                //Assembly.Load(new AssemblyName("mscorlib"))
            }.Distinct().ToArray();

            string[] assemblyPaths = new string[assemblies.Length];
            for (int x = 0; x < assemblies.Length; x++)
            {
                assemblyPaths[x] = assemblies[x].Location;
            }

            List <PortableExecutableReference> references = assemblyPaths.Select
                                                                (r => MetadataReference.CreateFromFile(r)).ToList();

            var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
            CSharpCompilation compilation = CSharpCompilation.Create(
                "csharptestbuild",
                syntaxTrees: new[] { systree },
                references: references,
                options: options);

            using (var ms = new MemoryStream())
            {
                EmitResult result = compilation.Emit(ms);
                if (!result.Success)
                {
                    IEnumerable <Diagnostic> failures = result.Diagnostics.Where(diagnostic =>
                                                                                 diagnostic.IsWarningAsError ||
                                                                                 diagnostic.Severity == DiagnosticSeverity.Error);
                    DisplayErrors(failures);
                    return;
                }
                else
                {
                    ms.Flush();
                    ms.Seek(0, SeekOrigin.Begin);
                    Assembly assembly = Assembly.Load(ms.ToArray());
                    var      type     = assembly.GetTypes()[0];
                    if (!type.GetMethods().Any(mi => mi.IsStatic))
                    {
                        MessageBox.Show("No (PUBLIC STATIC) method found to use as the entry point.");
                        return;
                    }
                    var methd = type.GetMethods().First(mi => mi.IsStatic) as MethodInfo;

                    try
                    {
                        bool   alloc         = allocConsoleToolStripMenuItem.Checked;
                        IntPtr consoleHandle = IntPtr.Zero;
                        if (alloc)
                        {
                            consoleHandle = GetConsoleWindow();
                            Console.Clear();
                            ShowWindow(consoleHandle, SW_SHOW);
                        }
                        methd.Invoke(null, new object[] { new string[] { "Hello", "World!" } });
                        if (consoleHandle != IntPtr.Zero)
                        {
                            ShowWindow(consoleHandle, SW_HIDE);
                        }
                    }
                    catch (Exception exc)
                    {
                        MessageBox.Show(exc.Message, "Runtime Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }
            }
        }
コード例 #6
0
        private static async Task <ProjectHandler> TryGetPhpProjectAsync(string projectFile, ILogTarget log)
        {
            try
            {
                Project project = LoadProject(projectFile);

                if (!IsPhpProject(project))
                {
                    return(null);
                }

                SetupMultitargetingIfNecessary(project);

                var buildResult = await ResolveReferencesAsync(project);

                var projectInstance = buildResult.ProjectStateAfterBuild;

                var metadataReferences = GatherReferences(project, projectInstance, buildResult)
                                         .Select(path => MetadataReference.CreateFromFile(path, documentation: XmlDocumentationProvider.CreateFromFile(Path.ChangeExtension(path, "xml"))))
                                         .ToArray();

                if (metadataReferences.Length == 0)
                {
                    // dotnet restore hasn't run yet
                    log?.LogMessage($"{Path.GetFileName(projectFile)}: could not be built; ");
                    return(null);
                }

                var options = new PhpCompilationOptions(
                    outputKind: OutputKind.DynamicallyLinkedLibrary,
                    baseDirectory: Path.GetDirectoryName(projectFile), // compilation expects platform-specific slashes (backslashes on windows)
                    specificDiagnosticOptions: null,
                    sdkDirectory: null);

                // TODO: Get from MSBuild
                string projectName = Path.GetFileNameWithoutExtension(projectFile);

                var encoding = TryParseEncodingName(projectInstance.GetPropertyValue("CodePage")) ?? Encoding.UTF8;

                var nowarn = projectInstance.GetPropertyValue("NoWarn");
                if (nowarn != null)
                {
                    options = options.WithSpecificDiagnosticOptions(ParseNoWarn(nowarn));
                }

                var syntaxTrees = ParseSourceFiles(projectInstance, encoding);

                var compilation = PhpCompilation.Create(
                    projectName,
                    syntaxTrees,
                    references: metadataReferences,
                    options: options);

                return(new ProjectHandler(compilation, projectInstance, encoding));
            }
            catch (Exception ex)
            {
                log?.LogMessage($"{Path.GetFileName(projectFile)}: {ex.Message}");
                return(null);
            }
        }
コード例 #7
0
        static void Main(string[] args)
        {
            // EXE実行中のパスを取得
            _executingAsmPath = Directory.GetParent(Assembly.GetExecutingAssembly().Location).FullName;

            string _sourceDirPath = null;
            string _dllPath       = null;

            bool isSingleSource = false;

            if (args.Length == 0)
            {
                // デバッグ用
                //isSingleSource = true;

                // テスト用
                _sourceDirPath = TEST_SOURCE_PATH;
                _dllPath       = TEST_DLL_PATH;
            }
            else if (args.Length < 2)
            {
                Console.Error.WriteLine("使い方①: RoslynAnalysisSampleConsole [ソース格納ディレクトリパス] [bin格納ディレクトリパス]");
                Console.Error.WriteLine($"使い方②: RoslynAnalysisSampleConsole [ソースファイルパス] [bin格納ディレクトリパス] {ARG_HINT_SINGLE_SOURCE}");
                return;
            }
            else
            {
                _sourceDirPath = args[0];
                _dllPath       = args[1];
            }

            if (!isSingleSource)
            {
                foreach (string arg in args)
                {
                    if (arg?.ToLower() == ARG_HINT_SINGLE_SOURCE)
                    {
                        isSingleSource = true;
                        break;
                    }
                }
            }

            if (!isSingleSource && !Directory.Exists(_sourceDirPath))
            {
                Console.Error.WriteLine($"指定されたソース格納ディレクトリパスが見つかりません({_sourceDirPath})");
                return;
            }

            if (isSingleSource && !File.Exists(_sourceDirPath))
            {
                Console.Error.WriteLine($"指定されたソースファイルが見つかりません({_sourceDirPath})");
                return;
            }

            if (!Directory.Exists(_dllPath))
            {
                Console.Error.WriteLine($"指定されたbin格納ディレクトリパスが見つかりません({_sourceDirPath})");
                return;
            }

            try
            {
                _exceptNamespacesStartsWith = new string[Properties.Settings.Default.ExceptNamespacesStartsWith.Count];
                Properties.Settings.Default.ExceptNamespacesStartsWith.CopyTo(_exceptNamespacesStartsWith, 0);

                _autoGeneratedFiles = new string[Properties.Settings.Default.AutoGeneratedFiles.Count];
                Properties.Settings.Default.AutoGeneratedFiles.CopyTo(_autoGeneratedFiles, 0);

                List <PortableExecutableReference> asmList = new List <PortableExecutableReference>();

                FileInfo msCorlib    = new FileInfo(typeof(object).Assembly.Location);
                string   msCorlibDir = msCorlib.DirectoryName;
                asmList.Add(MetadataReference.CreateFromFile(msCorlib.FullName));

                foreach (string path in Directory.GetFiles(_dllPath)
                         .Where(p => p.EndsWith(".dll") || p.EndsWith(".exe")))
                {
                    var asm = MetadataReference.CreateFromFile(path);
                    asmList.Add(asm);
                }

                List <string> asmDirs = new List <string>();
                asmDirs.Add(msCorlibDir);
                asmDirs.AddRange(_loadingAssemblyDirs);

                foreach (string path in asmDirs)
                {
                    foreach (string asmPath in Directory.GetFiles(path)
                             .Where(p => p.EndsWith(".dll") || p.EndsWith(".exe")))
                    {
                        var asm = MetadataReference.CreateFromFile(asmPath);
                        asmList.Add(asm);
                    }
                }

                if (isSingleSource)
                {
                    SyntaxTree tree;
                    using (TextReader reader = new StreamReader(_sourceDirPath))
                    {
                        tree = CSharpSyntaxTree.ParseText(reader.ReadToEnd());
                    }

                    Dictionary <string, SyntaxTree> dic = new Dictionary <string, SyntaxTree> {
                        { _sourceDirPath, tree }
                    };
                    WriteSingleFileAnalyzeResult(_sourceDirPath, asmList, dic);
                }
                else
                {
                    var sourceFilePathList =
                        from s in Directory.GetFiles(_sourceDirPath, "*.cs", SearchOption.AllDirectories)
                        where !_exceptDirs.Any(s2 => s.ToLower().Contains(s2))
                        select s;

                    Dictionary <string, SyntaxTree> treeDic = new Dictionary <string, SyntaxTree>();

                    var filePathList = sourceFilePathList as string[] ?? sourceFilePathList.ToArray();
                    foreach (string tmpPath in filePathList)
                    {
                        using (TextReader reader = new StreamReader(tmpPath))
                        {
                            treeDic.Add(tmpPath, CSharpSyntaxTree.ParseText(reader.ReadToEnd()));
                        }
                    }

                    foreach (string sourcePath in filePathList)
                    {
                        if (_autoGeneratedFiles.Any(s => sourcePath.ToLower().EndsWith(s)))
                        {
                            continue;
                        }

                        WriteSingleFileAnalyzeResult(sourcePath, asmList, treeDic);
                    }
                }
            }
            catch (Exception ex)
            {
                WriteExceptionLog(ex);
                Environment.Exit(-1);
            }
        }
コード例 #8
0
ファイル: CompilerBridge.cs プロジェクト: chenrensong/T4
 protected CompilerBridge(string source, bool debug, IEnumerable <string> references, string cmdLineArguments)
 {
     if (source == null)
     {
         throw new ArgumentNullException("source");
     }
     if (references == null)
     {
         throw new ArgumentNullException("references");
     }
     this.debug          = debug;
     this.source         = source;
     rawCmdLineArguments = (cmdLineArguments ?? string.Empty);
     templateReferences  = references.Select((string x) => (MetadataReference)(object)MetadataReference.CreateFromFile(x, default(MetadataReferenceProperties), (DocumentationProvider)null));
 }
コード例 #9
0
ファイル: FSMFactory.cs プロジェクト: kroehne/LogFSM
        public Assembly GetAssembly()
        {
            ImplementInterface();
            CreateConstructor();

            string _source = "//------------------------------------------------------------------------------" + Environment.NewLine +
                             "// <auto-generated>" + Environment.NewLine +
                             "//     Dynamically created source code. Provisionally created as C# syntax, " + Environment.NewLine +
                             "//     because CodeDom is not supported under .NET Core. " + Environment.NewLine +
                             "// </auto-generated>" + Environment.NewLine +
                             "//------------------------------------------------------------------------------" + Environment.NewLine;

            _source += "namespace " + CodeNameSpace + Environment.NewLine;
            _source += "{" + Environment.NewLine;
            _source += "    using System;" + Environment.NewLine;
            _source += "    using Stateless;" + Environment.NewLine;
            _source += "    using Stateless.Graph;" + Environment.NewLine;
            _source += "    using LogFSMShared;" + Environment.NewLine;
            _source += "    using System.Linq;" + Environment.NewLine;

            //_source += "    using System.ComponentModel.Primitives;" + Environment.NewLine;
            _source += "    using System.IO;" + Environment.NewLine;
            _source += "    using System.Collections;" + Environment.NewLine;
            _source += "    using System.Collections.Generic;" + Environment.NewLine;

            _source += "    public class " + ClassName + ": LogFSMShared.LogFSMBase, LogFSMShared.ILogFSM " + Environment.NewLine;
            _source += "    {" + Environment.NewLine;
            _source += "        private Dictionary<int,Stateless.StateMachine<State,Trigger>> machines;" + Environment.NewLine;

            _source += constructorCodeFragment;

            _source += createProcessEventMethodCodeFragment;

            _source += createRulesFunctionCodeFragment;

            _source += createTriggerFunctionCodeFragment;

            _source += enumDefinitionCodeFragment;

            _source += triggerDefinitionCodeFragment;

            _source += "     } " + Environment.NewLine;
            _source += " } " + Environment.NewLine;

            // export generated cs code for inspection
            if (parsedCommandLineArguments.Flags.Contains("EXPORT_GENERATED_SOURCE") || parsedCommandLineArguments.IsDebug)
            {
                using (StreamWriter sourceWriter = new StreamWriter(sourceFileName))
                {
                    sourceWriter.Write(_source);
                }
            }

            Assembly createdAssembly = null;

            var options = new CSharpCompilationOptions(
                OutputKind.DynamicallyLinkedLibrary,
                optimizationLevel: OptimizationLevel.Release,
                allowUnsafe: false);

            var compilation = CSharpCompilation.Create(Path.GetRandomFileName(), options: options);

            SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(_source);

            compilation = compilation.AddSyntaxTrees(syntaxTree);

            // TODO: Optimize!

            #region Add Rferences

            var assemblyPath = Path.GetDirectoryName(typeof(object).Assembly.Location);
            List <MetadataReference> references = new List <MetadataReference>();

            references.Add(MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "System.Private.CoreLib.dll")));
            references.Add(MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "System.Console.dll")));
            references.Add(MetadataReference.CreateFromFile(Path.Combine(assemblyPath, "System.Runtime.dll")));

            var usings = compilation.SyntaxTrees.Select(tree => tree.GetRoot().DescendantNodes().OfType <UsingDirectiveSyntax>()).SelectMany(s => s).ToArray();

            foreach (var u in usings)
            {
                if (File.Exists(Path.Combine(assemblyPath, u.Name.ToString() + ".dll")))
                {
                    references.Add(MetadataReference.CreateFromFile(Path.Combine(assemblyPath, u.Name.ToString() + ".dll")));
                }
            }

            references.Add(MetadataReference.CreateFromFile(Path.Combine(parsedCommandLineArguments.RuntimePath, "Stateless.dll")));
            references.Add(MetadataReference.CreateFromFile(Assembly.Load("netstandard, Version=2.0.0.0").Location));
            references.Add(MetadataReference.CreateFromFile(Path.Combine(parsedCommandLineArguments.RuntimePath, "LogFSMShared.dll")));

            #endregion

            compilation = compilation.AddReferences(references);

            //compile

            using (var ms = new MemoryStream())
            {
                EmitResult result = compilation.Emit(ms);

                if (!result.Success)
                {
                    IEnumerable <Diagnostic> failures = result.Diagnostics.Where(diagnostic =>
                                                                                 diagnostic.IsWarningAsError ||
                                                                                 diagnostic.Severity == DiagnosticSeverity.Error);

                    foreach (Diagnostic diagnostic in failures)
                    {
                        CompileErrors.Add(diagnostic.Id + ";" + diagnostic.GetMessage() + ";" + diagnostic.Location);
                    }
                }
                else
                {
                    ms.Seek(0, SeekOrigin.Begin);
                    AssemblyLoadContext context = AssemblyLoadContext.Default;
                    createdAssembly = context.LoadFromStream(ms);
                }
            }

            if (HasErrors)
            {
                foreach (string l in CompileErrors)
                {
                    File.AppendAllText(Path.Combine(parsedCommandLineArguments.OutputPath, "logfsmlasterror.txt"), l);
                }
            }

            return(createdAssembly);
        }
コード例 #10
0
        public static bool TryGetJsonForExpressionSyntax(this string anonymousTypeString, out string json)
        {
            json = null;

            foreach (var substitution in Substitutions)
            {
                anonymousTypeString = anonymousTypeString.Replace(substitution.Key, substitution.Value);
            }

            var text =
                $@"
					using System;
                    using System.Collections.Generic;
					using System.ComponentModel;
					using System.Runtime.Serialization;
					using Newtonsoft.Json.Linq;
					using Newtonsoft.Json;

					namespace Temporary
					{{
						public class Json
						{{
							public string Write()
							{{
								var o = {anonymousTypeString};
								var json = JsonConvert.SerializeObject(o, Formatting.Indented);
								return json;
							}}
						}}
					}}"                    ;

            var syntaxTree   = CSharpSyntaxTree.ParseText(text);
            var assemblyName = Path.GetRandomFileName();
            var references   = new List <MetadataReference>
            {
                MetadataReference.CreateFromFile(typeof(object).GetTypeInfo().Assembly.Location),
                MetadataReference.CreateFromFile(typeof(Enumerable).GetTypeInfo().Assembly.Location),
                MetadataReference.CreateFromFile(typeof(JsonConvert).GetTypeInfo().Assembly.Location),
                MetadataReference.CreateFromFile(typeof(ITypedList).GetTypeInfo().Assembly.Location),
            };
            var systemReferences = new[]
            {
                "System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
                "System.ObjectModel, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
                "System.Dynamic.Runtime, Version=4.0.10.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
                "System.Linq.Expressions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
                "netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51"
            };

            foreach (var r in systemReferences)
            {
                var location = Assembly.Load(r).Location;
                references.Add(MetadataReference.CreateFromFile(location));
            }

            var compilation =
                CSharpCompilation.Create(
                    assemblyName,
                    new[] { syntaxTree },
                    references,
                    new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

            using (var ms = new MemoryStream())
            {
                var result = compilation.Emit(ms);

                if (!result.Success)
                {
                    var failures = result.Diagnostics.Where(diagnostic =>
                                                            diagnostic.IsWarningAsError ||
                                                            diagnostic.Severity == DiagnosticSeverity.Error);

                    var builder = new StringBuilder($"Unable to serialize the following C# anonymous type string to json: {anonymousTypeString}");
                    foreach (var diagnostic in failures)
                    {
                        builder.AppendLine($"{diagnostic.Id}: {diagnostic.GetMessage()}");
                    }
                    builder.AppendLine(new string('-', 30));

                    Console.Error.WriteLine(builder.ToString());
                    return(false);
                }

                ms.Seek(0, SeekOrigin.Begin);

                var assembly = Assembly.Load(ms.ToArray());
                var type     = assembly.GetType("Temporary.Json");
                var obj      = Activator.CreateInstance(type);

                var output = type.InvokeMember("Write",
                                               BindingFlags.Default | BindingFlags.InvokeMethod,
                                               null,
                                               obj,
                                               new object[] { });

                json = output.ToString();
                return(true);
            }
        }
コード例 #11
0
        private Type BuildRepositoryImplType <TEntity, ID, TRepository>()
            where TEntity : class
            where TRepository : class
        {
            Type          repositoryImplType;
            StringBuilder sbCode = new StringBuilder();

            sbCode.AppendLine(@"using Microsoft.EntityFrameworkCore;
                using System;
                using System.Collections.Generic;
                using System.Linq;
                using ZackData.NetStandard.Exceptions;
                using ZackData.NetStandard;");
            string repositoryInterfaceName      = typeof(TRepository).Name;
            string repositoryInterfaceNamespace = typeof(TRepository).Namespace;
            string repositoryImplName           = repositoryInterfaceName + "Impl";
            string entityTypeFullName           = typeof(TEntity).FullName;
            string idTypeFullName = typeof(ID).FullName;

            sbCode.Append(@"namespace ").AppendLine(repositoryInterfaceNamespace);
            sbCode.AppendLine(@"
            {");
            sbCode.AppendLine($"public class {repositoryImplName} : BaseEFCrudRepository<{entityTypeFullName},{idTypeFullName}>,{repositoryInterfaceName}");
            sbCode.AppendLine(@"
                {");
            sbCode.AppendLine($"public {repositoryImplName}(DbContext dbCtx):base(dbCtx)");
            sbCode.AppendLine("{}");

            //all the methods of interfaces must be implemented
            List <Type> interfaces = new List <Type>();

            interfaces.Add(typeof(TRepository));

            //if the customed IXXXRepository has customed parent interface,like IMyCrudRepository
            Helper.GetAllParentInterface(typeof(TRepository), interfaces);
            interfaces.Remove(typeof(ICrudRepository <TEntity, ID>));//don't implement methods of ICrudRepository
            //because the methods of ICrudRepository already are implemented by BaseEFCrudRepository

            foreach (var intfType in interfaces)
            {
                foreach (var intfMethod in intfType.GetMethods())
                {
                    string methodName = intfMethod.Name;
                    if (methodName.StartsWithIgnoreCase("Find") || methodName.StartsWithIgnoreCase("Query") ||
                        methodName.StartsWithIgnoreCase("Get"))
                    {
                        sbCode.AppendLine().Append(CreateFindMethod <TEntity>(intfMethod)).AppendLine();
                    }
                    else if (methodName.StartsWithIgnoreCase("Delete"))
                    {
                        sbCode.AppendLine().Append(CreateDeleteMethod <TEntity>(intfMethod)).AppendLine();
                    }
                    else if (methodName.StartsWithIgnoreCase("Update"))
                    {
                        sbCode.AppendLine().Append(CreateUpdateMethod <TEntity>(intfMethod)).AppendLine();
                    }
                    else if (methodName.StartsWithIgnoreCase("Count"))
                    {
                        sbCode.AppendLine().Append(CreateCountMethod <TEntity>(intfMethod)).AppendLine();
                    }
                    else
                    {
                        throw new ConventionException("MethodName must start with Find, Delete or Update");
                    }
                }
            }


            sbCode.AppendLine(@"
                }
            }");
            string repositoryImplAssemblyName = repositoryInterfaceNamespace + "." + repositoryImplName;

            //todo 1:Cache the assembly
            //todo 2:use Emit instead of Roslyn
            var currentAssemblies = AppDomain.CurrentDomain.GetAssemblies();
            List <MetadataReference> metaReferences = new List <MetadataReference>();

            foreach (var asm in currentAssemblies)
            {
                if (asm.IsDynamic)
                {
                    continue;
                }
                if (string.IsNullOrWhiteSpace(asm.Location))
                {
                    continue;
                }
                metaReferences.Add(MetadataReference.CreateFromFile(asm.Location));
            }
            metaReferences.Add(MetadataReference.CreateFromFile(typeof(DynamicQueryableExtensions).Assembly.Location));

            var syntaxTree = SyntaxFactory.ParseSyntaxTree(sbCode.ToString());
            CSharpCompilationOptions compilationOpt = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
            var compilation = CSharpCompilation.Create(repositoryImplAssemblyName, new SyntaxTree[] { syntaxTree }, metaReferences, compilationOpt);

            using (var ms = new MemoryStream())
            {
                var emitResult = compilation.Emit(ms);
                if (!emitResult.Success)
                {
                    StringBuilder sbError = new StringBuilder();
                    foreach (var diag in emitResult.Diagnostics)
                    {
                        sbError.AppendLine(diag.GetMessage());
                    }
                    throw new Exception(sbError.ToString());
                }
                ms.Position = 0;
                Assembly asm = Assembly.Load(ms.ToArray());
                repositoryImplType = asm.GetType($"{repositoryInterfaceNamespace}.{repositoryImplName}");
            }

            return(repositoryImplType);
        }
コード例 #12
0
        protected virtual NPath CompileCSharpAssemblyWithRoslyn(CompilerOptions options)
        {
            var parseOptions       = new CSharpParseOptions(preprocessorSymbols: options.Defines);
            var compilationOptions = new CSharpCompilationOptions(
                outputKind: options.OutputPath.FileName.EndsWith(".exe") ? OutputKind.ConsoleApplication : OutputKind.DynamicallyLinkedLibrary,
                assemblyIdentityComparer: DesktopAssemblyIdentityComparer.Default
                );
            // Default debug info format for the current platform.
            DebugInformationFormat debugType = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? DebugInformationFormat.Pdb : DebugInformationFormat.PortablePdb;
            bool emitPdb = false;

            if (options.AdditionalArguments != null)
            {
                foreach (var option in options.AdditionalArguments)
                {
                    switch (option)
                    {
                    case "/unsafe":
                        compilationOptions = compilationOptions.WithAllowUnsafe(true);
                        break;

                    case "/optimize+":
                        compilationOptions = compilationOptions.WithOptimizationLevel(OptimizationLevel.Release);
                        break;

                    case "/debug:full":
                    case "/debug:pdbonly":
                        // Use platform's default debug info. This behavior is the same as csc.
                        emitPdb = true;
                        break;

                    case "/debug:portable":
                        emitPdb   = true;
                        debugType = DebugInformationFormat.PortablePdb;
                        break;

                    case "/debug:embedded":
                        emitPdb   = true;
                        debugType = DebugInformationFormat.Embedded;
                        break;
                    }
                }
            }
            var emitOptions = new EmitOptions(debugInformationFormat: debugType);
            var pdbPath     = (!emitPdb || debugType == DebugInformationFormat.Embedded) ? null : options.OutputPath.ChangeExtension(".pdb").ToString();

            var syntaxTrees = options.SourceFiles.Select(p =>
                                                         CSharpSyntaxTree.ParseText(
                                                             text: p.ReadAllText(),
                                                             options: parseOptions
                                                             )
                                                         );

            var compilation = CSharpCompilation.Create(
                assemblyName: options.OutputPath.FileNameWithoutExtension,
                syntaxTrees: syntaxTrees,
                references: options.References.Select(r => MetadataReference.CreateFromFile(r)),
                options: compilationOptions
                );

            var manifestResources = options.Resources.Select(r => {
                var fullPath = r.ToString();
                return(new ResourceDescription(
                           resourceName: Path.GetFileName(fullPath),
                           dataProvider: () => new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite),
                           isPublic: true
                           ));
            });

            EmitResult result;

            using (var outputStream = File.Create(options.OutputPath.ToString()))
                using (var pdbStream = (pdbPath == null ? null : File.Create(pdbPath))) {
                    result = compilation.Emit(
                        peStream: outputStream,
                        pdbStream: pdbStream,
                        manifestResources: manifestResources,
                        options: emitOptions
                        );
                }

            var errors = new StringBuilder();

            if (result.Success)
            {
                return(options.OutputPath);
            }

            foreach (var diagnostic in result.Diagnostics)
            {
                errors.AppendLine(diagnostic.ToString());
            }
            throw new Exception("Roslyn compilation errors: " + errors);
        }
コード例 #13
0
        public async Task <(ProjectInfo info, List <string> projectReferences, string targetPath)> LoadProject(string solutionDirectory, string projectFile)
        {
            lock (outputLines)
            {
                outputLines.Clear();
                errorLines.Clear();
            }

            return(await Task.Run(() =>
            {
                var project = XDocument.Load(projectFile);

                var projectReferences = project.Descendants("ProjectReference").Select(e => e.Attribute("Include").Value).ToList();

                var frameworks = GetTargetFrameworks(project);

                var targetFramework = frameworks.FirstOrDefault();

                if (targetFramework != null)
                {
                    Console.WriteLine($"Automatically selecting {targetFramework} as TargetFramework");
                }
                else
                {
                    //throw new Exception("Must specify target framework to load project.");
                    Console.WriteLine($"Non-Dotnet core project trying anyway.");
                    targetFramework = "";
                }

                ProjectInfoResponse loadData = null;

                try
                {
                    loadData = SendRequest(new ProjectInfoRequest {
                        SolutionDirectory = solutionDirectory, FullPath = projectFile, TargetFramework = targetFramework
                    });
                }
                catch (Exception)
                {
                    return (null, null, null);
                }

                requestComplete.WaitOne();

                if (loadData.CscCommandLine != null && loadData.CscCommandLine.Count > 0)
                {
                    var projectOptions = ParseArguments(loadData.CscCommandLine.Skip(1));

                    var projectInfo = ProjectInfo.Create(
                        ProjectId.CreateNewId(),
                        VersionStamp.Create(),
                        name: Path.GetFileNameWithoutExtension(projectFile),
                        assemblyName: Path.GetFileNameWithoutExtension(projectOptions.outputFile),
                        language: LanguageNames.CSharp,
                        filePath: projectFile,
                        outputFilePath: projectOptions.outputFile,
                        compilationOptions: projectOptions.compilationOptions,
                        parseOptions: projectOptions.parseOptions,
                        metadataReferences: loadData.MetaDataReferences.Select(ar => MetadataReference.CreateFromFile(ar, documentation: IoC.Get <AvalonStudio.Projects.OmniSharp.Roslyn.DocumentationProvider>()?.GetDocumentationProvider(ar))));

                    return (projectInfo, projectReferences, loadData.TargetPath);
                }
                else
                {
                    IoC.Get <IConsole>($"Project may have failed to load correctly: {Path.GetFileNameWithoutExtension(projectFile)}");

                    var projectInfo = ProjectInfo.Create(
                        ProjectId.CreateNewId(),
                        VersionStamp.Create(),
                        Path.GetFileNameWithoutExtension(projectFile), Path.GetFileNameWithoutExtension(projectFile),
                        LanguageNames.CSharp,
                        projectFile);

                    return (projectInfo, projectReferences, loadData?.TargetPath);
                }
            }));
        }
コード例 #14
0
        static GenerationHelpers()
        {
            // this "core assemblies hack" is from https://stackoverflow.com/a/47196516/4418060
            var coreAssemblyPath  = Path.GetDirectoryName(typeof(object).Assembly.Location) !;
            var coreAssemblyNames = new[] {
                "mscorlib.dll",
                "netstandard.dll",
                "System.dll",
                "System.Core.dll",
#if NETCOREAPP
                "System.Private.CoreLib.dll",
#endif
                "System.Runtime.dll",
            };
            var coreMetaReferences =
                coreAssemblyNames.Select(x => MetadataReference.CreateFromFile(Path.Combine(coreAssemblyPath, x)));
            var otherAssemblies = new[] {
                typeof(CSharpCompilation).Assembly,
                typeof(GenerateHandlerMethodsAttribute).Assembly,
                typeof(IDebugAdapterClientRegistry).Assembly,
                typeof(Unit).Assembly,
                typeof(JToken).Assembly,
                typeof(ILanguageServerRegistry).Assembly,
            };

            MetadataReferences = coreMetaReferences
                                 .Concat <MetadataReference>(otherAssemblies.Distinct().Select(x => MetadataReference.CreateFromFile(x.Location)))
                                 .ToImmutableArray();
        }
コード例 #15
0
 protected override IEnumerable <MetadataReference> GetAdditionalReferences() => new[]
 {
     MetadataReference.CreateFromFile(typeof(IFileSystem).Assembly.Location)
 };
コード例 #16
0
        /// <summary>
        /// Generates and compiles an assembly for the provided grains.
        /// </summary>
        /// <param name="generatedSyntax">
        /// The generated code.
        /// </param>
        /// <param name="assemblyName">
        /// The name for the generated assembly.
        /// </param>
        /// <returns>
        /// The raw assembly.
        /// </returns>
        /// <exception cref="CodeGenerationException">
        /// An error occurred generating code.
        /// </exception>
        public static byte[] CompileAssembly(GeneratedSyntax generatedSyntax, string assemblyName)
        {
            // Add the generated code attribute.
            var code = AddGeneratedCodeAttribute(generatedSyntax);

            // Reference everything which can be referenced.
            var assemblies =
                AppDomain.CurrentDomain.GetAssemblies()
                .Where(asm => !asm.IsDynamic && !string.IsNullOrWhiteSpace(asm.Location))
                .Select(asm => MetadataReference.CreateFromFile(asm.Location))
                .Cast <MetadataReference>()
                .ToArray();
            var logger = TraceLogger.GetLogger("CodeGenerator");

            // Generate the code.
            var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);

            string source = null;

            if (logger.IsVerbose3)
            {
                source = GenerateSourceCode(code);

                // Compile the code and load the generated assembly.
                if (logger.IsVerbose3)
                {
                    logger.LogWithoutBulkingAndTruncating(
                        Logger.Severity.Verbose3,
                        ErrorCode.CodeGenSourceGenerated,
                        "Generating assembly {0} with source:\n{1}",
                        assemblyName,
                        source);
                }
            }

            var compilation =
                CSharpCompilation.Create(assemblyName)
                .AddSyntaxTrees(code.SyntaxTree)
                .AddReferences(assemblies)
                .WithOptions(options);

            using (var stream = new MemoryStream())
            {
                var compilationResult = compilation.Emit(stream);
                if (!compilationResult.Success)
                {
                    source = source ?? GenerateSourceCode(code);
                    var errors = string.Join("\n", compilationResult.Diagnostics.Select(_ => _.ToString()));
                    logger.Warn(
                        ErrorCode.CodeGenCompilationFailed,
                        "Compilation of assembly {0} failed with errors:\n{1}\nGenerated Source Code:\n{2}",
                        assemblyName,
                        errors,
                        source);
                    throw new CodeGenerationException(errors);
                }

                logger.Verbose(ErrorCode.CodeGenCompilationSucceeded, "Compilation of assembly {0} succeeded.", assemblyName);
                return(stream.ToArray());
            }
        }
コード例 #17
0
        public override async Task <CodeCompilerResult> CompileFile(
            CodeCompilerArguments arguments,
            TextWriter log,
            CancellationToken token)
        {
            var references = new List <MetadataReference> ();

            foreach (var assemblyReference in AssemblyResolver.GetResolvedReferences(runtime, arguments.AssemblyReferences))
            {
                references.Add(MetadataReference.CreateFromFile(assemblyReference));
                try {
                    Assembly.LoadFrom(assemblyReference);
                } catch (Exception) { }
            }

            var syntaxTrees = new List <SyntaxTree> ();

            foreach (var sourceFile in arguments.SourceFiles)
            {
                using var stream = File.OpenRead(sourceFile);
                var sourceText = SourceText.From(stream, Encoding.UTF8, canBeEmbedded: true);
                syntaxTrees.Add(CSharpSyntaxTree.ParseText(sourceText, path: sourceFile));
            }

            var compilationoptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);

            if (arguments.Debug)
            {
                compilationoptions = compilationoptions.WithOptimizationLevel(OptimizationLevel.Debug);
            }

            var compilation = CSharpCompilation.Create(
                "GeneratedTextTransformation",
                syntaxTrees,
                references,
                compilationoptions
                );

            EmitOptions         emitOptions   = null;
            List <EmbeddedText> embeddedTexts = null;
            string pdbPath = null;

            if (arguments.Debug)
            {
                pdbPath       = Path.ChangeExtension(arguments.OutputPath, "pdb");
                embeddedTexts = syntaxTrees.Where(st => !string.IsNullOrEmpty(st.FilePath)).Select(st => EmbeddedText.FromSource(st.FilePath, st.GetText())).ToList();
                emitOptions   = new EmitOptions(debugInformationFormat: DebugInformationFormat.PortablePdb, pdbFilePath: pdbPath);
            }

            EmitResult result;

            using var fs = File.OpenWrite(arguments.OutputPath);
            {
                if (pdbPath != null)
                {
                    using var pdb = File.OpenWrite(pdbPath);
                    result        = compilation.Emit(fs, pdbStream: pdb, options: emitOptions, embeddedTexts: embeddedTexts);
                }
                else
                {
                    result = compilation.Emit(fs, options: emitOptions, embeddedTexts: embeddedTexts);
                }
            }

            if (result.Success)
            {
                return(new CodeCompilerResult {
                    Output = new List <string> (),
                    Success = true,
                    Errors = new List <CodeCompilerError> ()
                });
            }

            var failures = result.Diagnostics.Where(x => x.IsWarningAsError || x.Severity == DiagnosticSeverity.Error);
            var errors   = failures.Select(x => {
                var location          = x.Location.GetMappedLineSpan();
                var startLinePosition = location.StartLinePosition;
                var endLinePosition   = location.EndLinePosition;
                return(new CodeCompilerError {
                    Message = x.GetMessage(),
                    Column = startLinePosition.Character,
                    Line = startLinePosition.Line,
                    EndLine = endLinePosition.Line,
                    EndColumn = endLinePosition.Character,
                    IsError = x.Severity == DiagnosticSeverity.Error,
                    Origin = location.Path
                });
            }).ToList();

            return(new CodeCompilerResult {
                Success = false,
                Output = new List <string> (),
                Errors = errors
            });
        }
コード例 #18
0
        public Assembly CompileFile(string path)
        {
            string code = "";

            for (int i = 0; i < 100; i++)
            {
                try
                {
                    code = System.IO.File.ReadAllText(path);
                    break;
                }
                catch
                {
                    Thread.Sleep(100);
                }
            }
            SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(code);

            string assemblyName = Path.GetRandomFileName();

            MetadataReference[] references = new MetadataReference[]
            {
                MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(System.Drawing.Image).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(System.Net.WebClient).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(ImageProcessor.ImageFactory).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(ImageProcessor.Imaging.TextLayer).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(ImageProcessor.Plugins.Effects.Ascii).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(ImageProcessor.Plugins.Cair.ImageFactoryExtensions).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(ImageProcessor.Plugins.Cair.Processors.ContentAwareResize).Assembly.Location),
                MetadataReference.CreateFromFile(System.Reflection.Assembly.GetEntryAssembly().Location)
            };
            CSharpCompilation compilation = CSharpCompilation.Create(
                assemblyName,
                syntaxTrees: new[] { syntaxTree },
                references: references,
                options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));

            using (var ms = new MemoryStream())
            {
                EmitResult result = compilation.Emit(ms);

                if (!result.Success)
                {
                    IEnumerable <Diagnostic> failures = result.Diagnostics.Where(diagnostic =>
                                                                                 diagnostic.IsWarningAsError ||
                                                                                 diagnostic.Severity == DiagnosticSeverity.Error);

                    Console.WriteLine("Compilation of: " + path + " failed! Errors: ");
                    foreach (Diagnostic diagnostic in failures)
                    {
                        Console.WriteLine("{0}: {1}", diagnostic.Id, diagnostic.GetMessage());
                    }
                }
                else
                {
                    ms.Seek(0, SeekOrigin.Begin);
                    Assembly assembly = Assembly.Load(ms.ToArray());
                    return(assembly);
                }
            }

            return(null);
        }
コード例 #19
0
        private IView CompileAndInstance(string code, Assembly modelAssembly)
        {
            modelAssembly = modelAssembly ?? Assembly.GetEntryAssembly();

            var compilation = CSharpCompilation.Create("AppViewAssembly")
                              .WithOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary))
                              .AddReferences(MetadataReference.CreateFromFile(typeof(object).Assembly.Location))
                              .AddReferences(MetadataReference.CreateFromFile(typeof(Object).Assembly.Location))
                              .AddReferences(MetadataReference.CreateFromFile(typeof(IView).Assembly.Location))
                              .AddReferences(MetadataReference.CreateFromFile(Assembly.GetEntryAssembly().Location))
                              .AddReferences(MetadataReference.CreateFromFile(Assembly.Load(new AssemblyName("netstandard")).Location))
                              .AddReferences(MetadataReference.CreateFromFile(modelAssembly.Location));

            var netStandartAssembly = Assembly.Load(new AssemblyName("netstandart")).GetReferencedAssemblies();

            foreach (var assembly in netStandartAssembly)
            {
                compilation.AddReferences(MetadataReference.CreateFromFile(Assembly.Load(assembly).Location));
            }

            compilation = compilation.AddSyntaxTrees(SyntaxFactory.ParseSyntaxTree(code));

            using (var memoryStream = new MemoryStream())
            {
                var compilationResult = compilation.Emit(memoryStream);

                if (!compilationResult.Success)
                {
                    var errors     = compilationResult.Diagnostics.Where(x => x.Severity == DiagnosticSeverity.Error);
                    var errorsHtml = new StringBuilder();
                    errorsHtml.AppendLine($"<h1>{errors.Count()} errors:</h1>");

                    foreach (var error in errors)
                    {
                        errorsHtml.AppendLine($"<div>{error.Location} => {error.GetMessage()}</div>");
                    }

                    errorsHtml.AppendLine($"<pre>{WebUtility.HtmlEncode(code)}</pre>");
                    return(new ErrorView(errorsHtml.ToString()));
                }

                memoryStream.Seek(0, SeekOrigin.Begin);

                var assemblyBytes = memoryStream.ToArray();
                var assembly      = Assembly.Load(assemblyBytes);
                var type          = assembly.GetType("AppViewContentNamespace.AppViewCode");

                if (type == null)
                {
                    Console.WriteLine("AppViewCode not found.");

                    return(null);
                }

                var instance = Activator.CreateInstance(type);

                if (instance == null)
                {
                    Console.WriteLine("AppViewCode cannot be instanciated.");

                    return(null);
                }

                return(instance as IView);
            }
        }
コード例 #20
0
ファイル: Compiler.cs プロジェクト: Erdroy/Stationeers.Addons
        public static string Compile(string addonName, string[] sourceFiles, bool trustedCode = false)
        {
            var syntaxTrees = new List <SyntaxTree>();

            // Parse all files
            foreach (var sourceFile in sourceFiles)
            {
                Console.WriteLine($"Compiling file '{sourceFile}'...");

                if (!File.Exists(sourceFile))
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine($"Could not find source file '{sourceFile}'.");
                    Console.ResetColor();
                    return(string.Empty);
                }

                var syntaxTree = CSharpSyntaxTree.ParseText(File.ReadAllText(sourceFile));

                if (!trustedCode)
                {
                    if (!ValidateSyntaxTree(syntaxTree, out var failMessage))
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine($"Source file '{sourceFile}' did not pass security check! Fail Message: '{failMessage}'.");
                        Console.ResetColor();
                        return(string.Empty);
                    }
                }

                syntaxTrees.Add(syntaxTree);
            }

            // Check server/game instance (might make this more thorough or include it as part of signature)
            var installDirectory = Directory.Exists("../rocketstation_Data/Managed/") ? "../rocketstation_Data/Managed/" : "../rocketstation_DedicatedServer_Data/Managed/";

            // Setup reference list
            var references = new List <MetadataReference>();

            foreach (var file in GameAssemblies)
            {
                var reference = MetadataReference.CreateFromFile(Path.Combine(Environment.CurrentDirectory, installDirectory, file));
                references.Add(reference);
            }

            foreach (var file in AdditionalAssemblies)
            {
                var reference = MetadataReference.CreateFromFile(Path.Combine(Environment.CurrentDirectory, file));
                references.Add(reference);
            }

            var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);

            // Compile
            Console.WriteLine($"Linking addon '{addonName}'...");

            var assemblyName = addonName + "-Assembly";
            var compilation  = CSharpCompilation.Create($"{assemblyName}-{DateTime.UtcNow.Ticks}")
                               .WithReferences(references.ToArray())
                               .WithOptions(options)
                               .AddSyntaxTrees(syntaxTrees);

            using (var ms = new MemoryStream())
            {
                var result = compilation.Emit(ms);
                var output = result.Diagnostics;

                if (!result.Success)
                {
                    foreach (var error in output)
                    {
                        var prefix = "";
                        switch (error.Severity)
                        {
                        case DiagnosticSeverity.Hidden:
                        case DiagnosticSeverity.Info: continue;

                        case DiagnosticSeverity.Warning:
                            Console.ForegroundColor = ConsoleColor.Yellow;
                            prefix = "[Plugin Compiler - WARNING]";
                            break;

                        case DiagnosticSeverity.Error:
                            Console.ForegroundColor = ConsoleColor.Red;
                            prefix = "[Plugin Compiler - ERROR]";
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }

                        var errorMessage = error.GetMessage();

                        // Ignore mscorlib warnings for now.
                        // Task: https://trello.com/c/akuaJbdp/25-mscorlib-plugin-build-issues
                        if (errorMessage.Contains("mscorlib, Version=2.0.0.0,"))
                        {
                            continue;
                        }

                        Console.WriteLine(prefix + " " + errorMessage);
                    }
                    Console.ResetColor();
                    return(string.Empty);
                }

                // Output as AddonsCache/AddonName-Assembly.dll
                var assemblyFile = "AddonsCache/" + assemblyName + ".dll";
                using (var fs = new FileStream(assemblyFile, FileMode.Create))
                {
                    fs.Write(ms.GetBuffer(), 0, (int)ms.Length);
                }

                return(assemblyFile);
            }
        }
コード例 #21
0
        public void RollbackProcess_ShouldRollbackError_RollbackedCompilationShouldCompileWhenUriIsEmpty()
        {
            var syntaxTree  = CSharpSyntaxTree.ParseText(@"
using System;

namespace ExampleProject
{
    public class Query
    {
        public void Break()
        {
            if(System.Environment.GetEnvironmentVariable(""ActiveMutation"")==""1"")
            {
                string someQuery = ""test"";
                new Uri(new Uri(string.Empty), ""/API?"" - someQuery);
            }
            else
            {
                string someQuery = ""test"";
                new System.Uri(new System.Uri(string.Empty), ""/API?"" + someQuery);
            }
        }
    }
}");
            var ifStatement = syntaxTree.GetRoot()
                              .DescendantNodes()
                              .Where(x => x is IfStatementSyntax)
                              .First();
            var annotatedSyntaxTree = syntaxTree.GetRoot()
                                      .ReplaceNode(
                ifStatement,
                ifStatement.WithAdditionalAnnotations(new SyntaxAnnotation("MutationIf", "1"))
                ).SyntaxTree;

            var compiler = CSharpCompilation.Create("TestCompilation",
                                                    syntaxTrees: new Collection <SyntaxTree>()
            {
                annotatedSyntaxTree
            },
                                                    options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary),
                                                    references: new List <PortableExecutableReference>()
            {
                MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(Environment).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(Uri).Assembly.Location),
            });

            var target = new RollbackProcess();

            using (var ms = new MemoryStream())
            {
                var compileResult = compiler.Emit(ms);

                var fixedCompilation = target.Start(compiler, compileResult.Diagnostics);

                var rollbackedResult = fixedCompilation.Compilation.Emit(ms);

                rollbackedResult.Success.ShouldBeTrue();

                // validate that only one of the compile errors marked the mutation as rollbacked.
                fixedCompilation.RollbackedIds.ShouldBe(new Collection <int> {
                    1
                });
            }
        }
コード例 #22
0
        public Assembly CreateAssembly()
        {
            var references = new HashSet <Assembly>();

            references.UnionWith(from t in Translators
                                 from n in t.TypeNames
                                 select n.Key.Assembly);

            if (_options?.References != null)
            {
                references.UnionWith(_options.References);
            }
            references.Add(typeof(object).Assembly);

#if NETSTANDARD2_0
            references.Add(Assembly.Load(new AssemblyName("netstandard")));
            references.Add(Assembly.Load(new AssemblyName("System.Runtime")));
            references.Add(Assembly.Load(new AssemblyName("System.Collections")));
#endif

            var assemblyName = Path.GetRandomFileName();
            var symbolsName  = Path.ChangeExtension(assemblyName, "pdb");

            var metadataReferences        = references.Select(it => MetadataReference.CreateFromFile(it.Location));
            var isRelease                 = _options?.IsRelease ?? !Debugger.IsAttached;
            CSharpCompilation compilation = CSharpCompilation.Create(
                assemblyName,
                _codes,
                metadataReferences,
                new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, usings: new[] { "System" })
                .WithOptimizationLevel(isRelease ? OptimizationLevel.Release : OptimizationLevel.Debug)
                .WithPlatform(Platform.AnyCpu)
                );

            using var assemblyStream = new MemoryStream();
            using var symbolsStream  = new MemoryStream();
            var emitOptions = new EmitOptions(
                debugInformationFormat: DebugInformationFormat.PortablePdb,
                pdbFilePath: symbolsName);

            var embeddedTexts = _codes.Select(it => EmbeddedText.FromSource(it.FilePath, it.GetText()));

            EmitResult result = compilation.Emit(
                peStream: assemblyStream,
                pdbStream: symbolsStream,
                embeddedTexts: embeddedTexts,
                options: emitOptions);

            if (!result.Success)
            {
                var errors = new List <string>();

                IEnumerable <Diagnostic> failures = result.Diagnostics.Where(diagnostic =>
                                                                             diagnostic.IsWarningAsError ||
                                                                             diagnostic.Severity == DiagnosticSeverity.Error);

                foreach (Diagnostic diagnostic in failures)
                {
                    errors.Add($"{diagnostic.Id}: {diagnostic.GetMessage()}");
                }

                throw new InvalidOperationException(string.Join("\n", errors));
            }

            assemblyStream.Seek(0, SeekOrigin.Begin);
            symbolsStream.Seek(0, SeekOrigin.Begin);

#if NETSTANDARD2_0
            return(System.Runtime.Loader.AssemblyLoadContext.Default.LoadFromStream(assemblyStream, symbolsStream));
#else
            return(Assembly.Load(assemblyStream.ToArray(), symbolsStream.ToArray()));
#endif
        }
コード例 #23
0
        /// <summary>
        /// Compiles <paramref name="code"/> and creates script.
        /// </summary>
        /// <param name="options">Compilation options.</param>
        /// <param name="code">Code to be compiled.</param>
        /// <param name="builder">Assembly builder.</param>
        /// <param name="previousSubmissions">Enumeration of scripts that were evaluated within current context. New submission may reference them.</param>
        /// <returns>New script reepresenting the compiled code.</returns>
        public static Script Create(Context.ScriptOptions options, string code, PhpCompilationFactory builder, IEnumerable <Script> previousSubmissions)
        {
            // use the language version of the requesting context
            Version languageVersion = null;
            bool    shortOpenTags   = false;

            var language = options.Context.TargetPhpLanguage;

            if (language != null)
            {
                shortOpenTags = language.ShortOpenTag;
                Version.TryParse(language.LanguageVersion, out languageVersion);
            }

            // unique in-memory assembly name
            var name = builder.GetNewSubmissionName();

            // submission do not have the opening "<?php" script tag:
            var kind = options.IsSubmission ? SourceCodeKind.Script : SourceCodeKind.Regular;

            if (kind == SourceCodeKind.Script && options.EmitDebugInformation)
            {
                // since submission do not have the opening "<?php" tag,
                // add a comment with the opening tag, so source code editors don't get confused and colorize the code properly:
                code = $"#<?php\n{code}";
            }

            // parse the source code
            var tree = PhpSyntaxTree.ParseCode(
                SourceText.From(code, Encoding.UTF8, SourceHashAlgorithm.Sha256),
                new PhpParseOptions(
                    kind: kind,
                    languageVersion: languageVersion,
                    shortOpenTags: shortOpenTags),
                PhpParseOptions.Default,
                options.IsSubmission ? BuildSubmissionFileName(options.Location.Path, name.Name) : options.Location.Path
                );

            var diagnostics = tree.Diagnostics;

            if (!HasErrors(diagnostics))
            {
                // TODO: collect required types from {tree}, remember as a script dependencies
                // TODO: perform class autoload (now before compilation, and then always before invocation)

                // list of scripts that were eval'ed in the context already,
                // our compilation may depend on them
                var dependingSubmissions = previousSubmissions.Where(s => !s.Image.IsDefaultOrEmpty);
                IEnumerable <MetadataReference> metadatareferences
                    = dependingSubmissions.Select(s => MetadataReference.CreateFromImage(s.Image));

                if (options.AdditionalReferences != null)
                {
                    // add additional script references
                    metadatareferences
                        = metadatareferences.Concat(options.AdditionalReferences.Select(r => MetadataReference.CreateFromFile(r)));
                }

                // create the compilation object
                // TODO: add conditionally declared types into the compilation tables
                var compilation = (PhpCompilation)builder.CoreCompilation
                                  .WithAssemblyName(name.Name)
                                  .AddSyntaxTrees(tree)
                                  .AddReferences(metadatareferences);

                var emitOptions   = new EmitOptions();
                var embeddedTexts = default(IEnumerable <EmbeddedText>);

                if (options.EmitDebugInformation)
                {
                    compilation = compilation.WithPhpOptions(compilation.Options.WithOptimizationLevel(OptimizationLevel.Debug).WithDebugPlusMode(true));

                    if (options.IsSubmission)
                    {
                        emitOptions   = emitOptions.WithDebugInformationFormat(DebugInformationFormat.PortablePdb);
                        embeddedTexts = new[] { EmbeddedText.FromSource(tree.FilePath, tree.GetText()) };
                    }
                }
                else
                {
                    compilation = compilation.WithPhpOptions(compilation.Options.WithOptimizationLevel(OptimizationLevel.Release));
                }

                diagnostics = compilation.GetDeclarationDiagnostics();
                if (!HasErrors(diagnostics))
                {
                    var peStream  = new MemoryStream();
                    var pdbStream = options.EmitDebugInformation ? new MemoryStream() : null;

                    var result = compilation.Emit(peStream,
                                                  pdbStream: pdbStream,
                                                  options: emitOptions,
                                                  embeddedTexts: embeddedTexts
                                                  );

                    if (result.Success)
                    {
                        //if (pdbStream != null)
                        //{
                        //    // DEBUG DUMP
                        //    var fname = @"C:\Users\jmise\OneDrive\Desktop\" + Path.GetFileNameWithoutExtension(tree.FilePath);
                        //    File.WriteAllBytes(fname + ".dll", peStream.ToArray());
                        //    File.WriteAllBytes(fname + ".pdb", pdbStream.ToArray());
                        //}

                        return(new Script(name, peStream, pdbStream, builder, previousSubmissions));
                    }
                    else
                    {
                        diagnostics = result.Diagnostics;
                    }
                }
            }

            //
            return(CreateInvalid(diagnostics));
        }
コード例 #24
0
    // Public methods
    ///////////////////////////

    public void ReferenceAssembly(Assembly assembly)
    {
        references.Add(MetadataReference.CreateFromFile(assembly.Location));
    }
コード例 #25
0
ファイル: Form1.cs プロジェクト: 7UKECREAT0R/CSharpTester
        public void BuildTo(BuildType type)
        {
            SyntaxTree systree = CSharpSyntaxTree.ParseText(input.Text);

            Assembly[] assemblies = new List <Assembly> {
                typeof(object).GetTypeInfo().Assembly,
                typeof(Console).GetTypeInfo().Assembly,
                typeof(Enumerable).GetTypeInfo().Assembly,
                typeof(File).GetTypeInfo().Assembly,
                typeof(Assembly).GetTypeInfo().Assembly,
                typeof(CSharpArgumentInfo).GetTypeInfo().Assembly,
                typeof(CSharpSyntaxTree).GetTypeInfo().Assembly,
                typeof(System.Dynamic.CallInfo).GetTypeInfo().Assembly,
                typeof(RuntimeBinderException).GetTypeInfo().Assembly,
                typeof(System.Dynamic.ConvertBinder).GetTypeInfo().Assembly,
                typeof(ExpressionType).GetTypeInfo().Assembly,
                typeof(MessageBox).GetTypeInfo().Assembly,
                //Assembly.Load(new AssemblyName("Microsoft.CSharp")),
                //Assembly.Load(new AssemblyName("netstandard")),
                //Assembly.Load(new AssemblyName("System.Dynamic.Runtime")),
                //Assembly.Load(new AssemblyName("System.Runtime")),
                //Assembly.Load(new AssemblyName("mscorlib"))
            }.Distinct().ToArray();

            string[] assemblyPaths = new string[assemblies.Length];
            for (int x = 0; x < assemblies.Length; x++)
            {
                assemblyPaths[x] = assemblies[x].Location;
            }

            List <PortableExecutableReference> references = assemblyPaths.Select
                                                                (r => MetadataReference.CreateFromFile(r)).ToList();

            CSharpCompilationOptions options;

            if (type == BuildType.DLL)
            {
                options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary);
            }
            else
            {
                options = new CSharpCompilationOptions(OutputKind.ConsoleApplication);
            }
            CSharpCompilation compilation = CSharpCompilation.Create(
                "csharptestbuild",
                syntaxTrees: new[] { systree },
                references: references,
                options: options);

            using (var ms = new MemoryStream())
            {
                EmitResult result = compilation.Emit(ms);
                if (!result.Success)
                {
                    IEnumerable <Diagnostic> failures = result.Diagnostics.Where(diagnostic =>
                                                                                 diagnostic.IsWarningAsError ||
                                                                                 diagnostic.Severity == DiagnosticSeverity.Error);
                    DisplayErrors(failures);
                    return;
                }
                else
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    if (type == BuildType.DLL)
                    {
                        sfd.Filter = "Assembly (*.dll)|*.dll";
                    }
                    if (type == BuildType.EXE)
                    {
                        sfd.Filter = "Executable (*.exe)|*.exe";
                    }
                    if (sfd.ShowDialog() != DialogResult.OK)
                    {
                        return;
                    }
                    File.WriteAllBytes(sfd.FileName, ms.ToArray());
                }
            }
        }
コード例 #26
0
        public static IEnumerable <MetadataReference> GetMetadataReferences(bool needsCodeGenAssembly = true)
        {
            var result = DynamicCode.AssemblyTypes
                         .Select(type => MetadataReference.CreateFromFile(type.Assembly.Location))
                         .And(needsCodeGenAssembly ? DynamicCode.CodeGenAssemblyPath?.Let(s => MetadataReference.CreateFromFile(s)) : null)
                         .NotNull().ToList();

            return(result);
        }
コード例 #27
0
 public override ImmutableArray<PortableExecutableReference> ResolveReference(string reference, string baseFilePath, MetadataReferenceProperties properties)
     => ImmutableArray.Create(MetadataReference.CreateFromFile(reference, properties));
コード例 #28
0
        Assembly Compile()
        {
            Dictionary <string, Resource[]> resourcesByLanguage;
            Assembly assembly = null;

            try
            {
                Clear();

                if (!CompileResources(out resourcesByLanguage))
                {
                    return(null);
                }

                var manifestResources = CreateAssemblyResources(resourcesByLanguage);

                var compilation = CSharpCompilation.Create(AssemblyName)
                                  .WithOptions(CreateCompilationOptions())
                                  .AddSyntaxTrees(Files.Select(f => Parse(f)))
                                  .AddReferences(ReferenceAssemblies.Select(f => MetadataReference.CreateFromFile(f)));

                var emitResult          = (EmitResult)null;
                var assemblyFileName    = AssemblyName + ".dll";
                var assemblyPath        = Path.Combine(Config.TasksLocation, assemblyFileName);
                var assemblyPdbFileName = Path.Combine(Config.TasksLocation, AssemblyName + ".pdb");
                if (Debug)
                {
                    emitResult = compilation.Emit(assemblyPath, assemblyPdbFileName, manifestResources: manifestResources);
                }
                else
                {
                    emitResult = compilation.Emit(assemblyPath, manifestResources: manifestResources);
                }

                if (emitResult.Success)
                {
                    assembly = Assembly.LoadFrom(assemblyPath);
                    if (!BuildSatelliteAssemblies(resourcesByLanguage, assembly.GetName().Version.ToString()))
                    {
                        assembly = null;
                    }
                }
                else
                {
                    ShowErrors(emitResult);
                }
            }
            catch (Exception ex)
            {
                assembly = null;
                Log.Exception(ex);
            }
            finally
            {
                if (assembly == null)
                {
                    Clear();
                }
                foreach (var res in Resources.Where(res => (res.Compile && File.Exists(res.CompiledResource))))
                {
                    File.Delete(res.CompiledResource);
                }
            }
            return(assembly);
        }
        public async Task FollowTypeForwards_MultipleHops_Cache()
        {
            var source            = @"
public class C
{
    // A change
    public event System.EventHandler [|E|] { add { } remove { } }
}";
            var typeForwardSource = @"
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(C))]
";

            await RunTestAsync(async path =>
            {
                MarkupTestFile.GetSpan(source, out var metadataSource, out var expectedSpan);

                // Compile reference assembly
                var sourceText       = SourceText.From(metadataSource, encoding: Encoding.UTF8);
                var(project, symbol) = await CompileAndFindSymbolAsync(path, Location.Embedded, Location.Embedded, sourceText, c => c.GetMember("C.E"), buildReferenceAssembly: true);

                // Compile implementation assembly to a different DLL
                var dllFilePath    = Path.Combine(path, "implementation.dll");
                var sourceCodePath = Path.Combine(path, "implementation.cs");
                var pdbFilePath    = Path.Combine(path, "implementation.pdb");
                var assemblyName   = "implementation";

                var workspace = TestWorkspace.Create(@$ "
<Workspace>
    <Project Language=" "{LanguageNames.CSharp}" " CommonReferences=" "true" " ReferencesOnDisk=" "true" ">
    </Project>
</Workspace>", composition: GetTestComposition());

                var implProject = workspace.CurrentSolution.Projects.First();
                CompileTestSource(dllFilePath, sourceCodePath, pdbFilePath, assemblyName, sourceText, implProject, Location.Embedded, Location.Embedded, buildReferenceAssembly: false, windowsPdb: false);

                // Compile type forwarding implementation DLL
                var typeForwardDllFilePath = Path.Combine(path, "typeforward.dll");
                assemblyName = "typeforward";

                implProject = implProject.AddMetadataReference(MetadataReference.CreateFromFile(dllFilePath));
                sourceText  = SourceText.From(typeForwardSource, Encoding.UTF8);
                CompileTestSource(typeForwardDllFilePath, sourceCodePath, pdbFilePath, assemblyName, sourceText, implProject, Location.Embedded, Location.Embedded, buildReferenceAssembly: false, windowsPdb: false);

                var typeForward2DllFilePath = Path.Combine(path, "typeforward2.dll");
                assemblyName = "typeforward2";

                implProject = implProject.AddMetadataReference(MetadataReference.CreateFromFile(typeForwardDllFilePath));
                sourceText  = SourceText.From(typeForwardSource, Encoding.UTF8);
                CompileTestSource(typeForward2DllFilePath, sourceCodePath, pdbFilePath, assemblyName, sourceText, implProject, Location.Embedded, Location.Embedded, buildReferenceAssembly: false, windowsPdb: false);

                var service = workspace.GetService <IImplementationAssemblyLookupService>();

                Assert.Equal(dllFilePath, service.FollowTypeForwards(symbol, typeForward2DllFilePath, new NoDuplicatesLogger()));

                // We need the DLLs to exist, in order for some checks to pass correct, but to ensure
                // that the file isn't read, we just zero it out.
                File.WriteAllBytes(typeForwardDllFilePath, Array.Empty <byte>());
                File.WriteAllBytes(typeForward2DllFilePath, Array.Empty <byte>());
                File.WriteAllBytes(dllFilePath, Array.Empty <byte>());

                Assert.Equal(dllFilePath, service.FollowTypeForwards(symbol, typeForward2DllFilePath, new NoDuplicatesLogger()));
            });
        }
コード例 #30
0
        public void RollbackProcess_ShouldFailWhenLocalRollbackFailsAndInDevMode()
        {
            var syntaxTree = CSharpSyntaxTree.ParseText(@"using System;

namespace ExampleProject
{
    public class StringMagic
    {
        public string AddTwoStrings(string first, string second, out string third)
        {
            var dummy = """";
            if(System.Environment.GetEnvironmentVariable(""ActiveMutation"")==""8""){            
                while (first.Length > 2)
                {
                    dummy = first + second;
                } 
                while (first.Length < 2)
                {
                    dummy =  second - first;
                }
            }else{if(System.Environment.GetEnvironmentVariable(""ActiveMutation"")==""7""){            
                while (first.Length > 2)
                {
                    dummy =  first + second;
                } 
                while (first.Length < 2)
                {
                    dummy =  second - first;
                }
            }else{if(System.Environment.GetEnvironmentVariable(""ActiveMutation"")==""6""){            
                while (first.Length == 2)
                {
                    dummy =  first + second;
                } 
                while (first.Length < 2)
                {
                    dummy =  second + first;
                }
            }else{
                third = ""good"";
                while (first.Length == 2)
                {
                    dummy =  first + second;
                } 
                while (first.Length < 2)
                {
                    dummy =  second + first;
                }
            }}}
                return null;
        }
    }
}");
            var root       = syntaxTree.GetRoot();

            var mutantIf1 = root.DescendantNodes().OfType <IfStatementSyntax>().First();

            root = root.ReplaceNode(
                mutantIf1,
                mutantIf1.WithAdditionalAnnotations(new SyntaxAnnotation("MutationIf", "8"))
                );
            var mutantIf2 = root.DescendantNodes().OfType <IfStatementSyntax>().ToList()[1];

            root = root.ReplaceNode(
                mutantIf2,
                mutantIf2.WithAdditionalAnnotations(new SyntaxAnnotation("MutationIf", "7"))
                );
            var mutantIf3 = root.DescendantNodes().OfType <IfStatementSyntax>().ToList()[2];

            root = root.ReplaceNode(
                mutantIf3,
                mutantIf3.WithAdditionalAnnotations(new SyntaxAnnotation("MutationIf", "6"))
                );
            var annotatedSyntaxTree = root.SyntaxTree;

            var compiler = CSharpCompilation.Create("TestCompilation",
                                                    syntaxTrees: new Collection <SyntaxTree>()
            {
                annotatedSyntaxTree
            },
                                                    options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary),
                                                    references: new List <PortableExecutableReference>()
            {
                MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
                MetadataReference.CreateFromFile(typeof(Environment).Assembly.Location)
            });

            var target = new RollbackProcess();

            using (var ms = new MemoryStream())
            {
                var compileResult = compiler.Emit(ms);

                Should.Throw <ApplicationException>(() => { target.Start(compiler, compileResult.Diagnostics, true); });
            }
        }