chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//            Copyright Keysight Technologies 2012-2019
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Reflection;
namespace OpenTap.Engine.UnitTests
{
    using SF = SyntaxFactory;
 
    class CodeGen
    {
        public class Result
        {
            public byte[] Bytes;
            public bool Success;
 
            public string Log;
 
            Assembly assembly;
            public Assembly GetAssembly(){
                if(assembly == null){
                    assembly = Assembly.Load(Bytes);
                }
                return assembly;
            }
        }
 
        public static Result BuildCode(string code, string moduleName, string strongNameKeyFile = null){
            
            var metadataref = new List<MetadataReference> { };
            // Detect the file location for the library that defines the object type
            var systemRefLocation=typeof(object).GetTypeInfo().Assembly.Location;
            // Create a reference to the library
            var systemReference = MetadataReference.CreateFromFile(systemRefLocation);
            var md = new HashSet<Assembly> { typeof(int).Assembly, typeof(ITestStep).Assembly};
            try
            {
                var asm = Assembly.Load("netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51");
                md.Add(asm);
            }
            catch { }
            try
            {
                var asm = Assembly.Load("mscorlib");
                md.Add(asm);
            }
            catch { }
            try
            {
                var asm = Assembly.Load("System");
                md.Add(asm);
            }
            catch { }
            try
            {
                var asm = Assembly.Load("System.Core");
                md.Add(asm);
            }
            catch { }
            try
            {
                var asm = Assembly.Load("System.Runtime");
                md.Add(asm);
            }
            catch { }
            try
            {
                var asm = Assembly.Load("System.Collections");
                md.Add(asm);
            }
            catch { }
            try
            {
                var asm = Assembly.Load("System.ComponentModel.TypeConverter");
                md.Add(asm);
            }
            catch { }
            try
            {
                var asm = Assembly.Load("System.ObjectModel");
                md.Add(asm);
            }
            catch { }
            try
            {
                var asm = Assembly.Load("Microsoft.CSharp");
                md.Add(asm);
            }
            catch { }
            md.Add(Assembly.GetEntryAssembly());
            foreach (var path in md)
            {
                if(path == null) continue;
                var r = MetadataReference.CreateFromFile(path.Location);
                metadataref.Add(r);
            }
 
            var options = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary, true, platform: Platform.AnyCpu, assemblyIdentityComparer: DesktopAssemblyIdentityComparer.Default)
               .WithReportSuppressedDiagnostics(true);
 
            if (strongNameKeyFile != null)
            {
                options = options.WithStrongNameProvider(new DesktopStrongNameProvider(ImmutableArray.Create(Path.GetDirectoryName(Path.GetFullPath(strongNameKeyFile)))))
                       .WithCryptoKeyFile(Path.GetFileName(strongNameKeyFile));
            }
 
            CSharpCompilation compilation = CSharpCompilation.Create(moduleName,
                    syntaxTrees: new[] { SF.ParseSyntaxTree(code) },
                    references: metadataref,
                    options: options
                );
            
            using(var ms = new MemoryStream()){
                var result= compilation.Emit(ms);
                return new Result{Success = result.Success, Bytes = ms.ToArray(), Log = string.Join("\n", result.Diagnostics)};
            }
        }
    }
 
}