|
23 | 23 | using System.IO; |
24 | 24 | using System.Linq; |
25 | 25 | using System.Reflection; |
| 26 | +using System.Runtime.Versioning; |
26 | 27 | using System.Threading.Tasks; |
27 | 28 | using System.Xml.Linq; |
28 | 29 | using BigInteger = System.Numerics.BigInteger; |
@@ -146,11 +147,13 @@ public List<CompilationContext> CompileSources(CompilationSourceReferences refer |
146 | 147 | {string.Join(Environment.NewLine, references.Projects!.Select(u => $" <ProjectReference Include =\"{u}\"/>"))} |
147 | 148 | </ItemGroup>"; |
148 | 149 |
|
| 150 | + string targetFramework = GetTargetFrameworkMoniker(); |
| 151 | + |
149 | 152 | var csproj = $@" |
150 | 153 | <Project Sdk=""Microsoft.NET.Sdk""> |
151 | 154 |
|
152 | 155 | <PropertyGroup> |
153 | | - <TargetFramework>{AppContext.TargetFrameworkName!}</TargetFramework> |
| 156 | + <TargetFramework>{targetFramework}</TargetFramework> |
154 | 157 | <ImplicitUsings>enable</ImplicitUsings> |
155 | 158 | <Nullable>enable</Nullable> |
156 | 159 | </PropertyGroup> |
@@ -216,6 +219,30 @@ public List<CompilationContext> CompileProject(string csproj, List<INamedTypeSym |
216 | 219 | return targetContractName == null ? CompileProjectContractsWithPrepare(sortedClasses, classDependencies, allClassSymbols) : [CompileProjectContractWithPrepare(sortedClasses, classDependencies, allClassSymbols, targetContractName)]; |
217 | 220 | } |
218 | 221 |
|
| 222 | + private static string GetTargetFrameworkMoniker() |
| 223 | + { |
| 224 | + const string fallback = "net9.0"; |
| 225 | + string? tfm = AppContext.TargetFrameworkName; |
| 226 | + if (string.IsNullOrEmpty(tfm)) |
| 227 | + return fallback; |
| 228 | + |
| 229 | + try |
| 230 | + { |
| 231 | + FrameworkName framework = new(tfm); |
| 232 | + if (!string.Equals(framework.Identifier, ".NETCoreApp", StringComparison.OrdinalIgnoreCase)) |
| 233 | + return fallback; |
| 234 | + |
| 235 | + if (framework.Version.Major < 9) |
| 236 | + return fallback; |
| 237 | + |
| 238 | + return $"net{framework.Version.Major}.{framework.Version.Minor}"; |
| 239 | + } |
| 240 | + catch |
| 241 | + { |
| 242 | + return fallback; |
| 243 | + } |
| 244 | + } |
| 245 | + |
219 | 246 | public (List<INamedTypeSymbol> sortedClasses, Dictionary<INamedTypeSymbol, List<INamedTypeSymbol>> classDependencies, List<INamedTypeSymbol?> allClassSymbols) PrepareProjectContracts(string csproj) |
220 | 247 | { |
221 | 248 | Compilation ??= GetCompilation(csproj); |
|
0 commit comments