|
18 | 18 |
|
19 | 19 | using System; |
20 | 20 | using System.Collections.Generic; |
21 | | -using System.Diagnostics; |
22 | 21 | using System.IO; |
23 | 22 | using System.Linq; |
24 | 23 | using System.Reflection.PortableExecutable; |
@@ -330,6 +329,45 @@ static string GetTargetFrameworkAttributeSnippetFile() |
330 | 329 | return tempFile; |
331 | 330 | } |
332 | 331 |
|
| 332 | + const string nonEmbeddedAttributesSnippet = @" |
| 333 | +using System; |
| 334 | +
|
| 335 | +#if !NET60 |
| 336 | +namespace System.Runtime.CompilerServices |
| 337 | +{ |
| 338 | + [AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = false)] |
| 339 | + internal sealed class CompilerFeatureRequiredAttribute : Attribute |
| 340 | + { |
| 341 | + public CompilerFeatureRequiredAttribute(string featureName) |
| 342 | + { |
| 343 | + } |
| 344 | + } |
| 345 | +
|
| 346 | + internal class IsExternalInit |
| 347 | + { |
| 348 | + } |
| 349 | +#endif |
| 350 | +#if !NET70 |
| 351 | + [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false, Inherited = false)] |
| 352 | + internal sealed class RequiredMemberAttribute : Attribute |
| 353 | + { |
| 354 | + } |
| 355 | +#endif |
| 356 | +#if !NET60 |
| 357 | +} |
| 358 | +#endif |
| 359 | +"; |
| 360 | + |
| 361 | + static readonly Lazy<string> nonEmbeddedAttributesSnippetFile = new Lazy<string>(GetNonEmbeddedAttributesSnippetFile); |
| 362 | + |
| 363 | + static string GetNonEmbeddedAttributesSnippetFile() |
| 364 | + { |
| 365 | + // Note: this leaks a temporary file, we're not attempting to delete it, because it is only one. |
| 366 | + var tempFile = Path.GetTempFileName(); |
| 367 | + File.WriteAllText(tempFile, nonEmbeddedAttributesSnippet); |
| 368 | + return tempFile; |
| 369 | + } |
| 370 | + |
333 | 371 | public static List<string> GetPreprocessorSymbols(CompilerOptions flags) |
334 | 372 | { |
335 | 373 | var preprocessorSymbols = new List<string>(); |
@@ -419,6 +457,11 @@ public static async Task<CompilerResults> CompileCSharp(string sourceFileName, C |
419 | 457 | sourceFileNames.Add(targetFrameworkAttributeSnippetFile.Value); |
420 | 458 | } |
421 | 459 |
|
| 460 | + if (targetNet40) |
| 461 | + { |
| 462 | + sourceFileNames.Add(nonEmbeddedAttributesSnippetFile.Value); |
| 463 | + } |
| 464 | + |
422 | 465 | var preprocessorSymbols = GetPreprocessorSymbols(flags); |
423 | 466 |
|
424 | 467 | if ((flags & CompilerOptions.UseMcsMask) == 0) |
|
0 commit comments