0

In a Windows C# project created using Visual Studio 2019 I see the file:

.NETFramework,Version=v4.7.2.AssemblyAttributes.cs

added to the solution with the following content:

// <autogenerated />
using System;
using System.Reflection;
[assembly: global::System.Runtime.Versioning.TargetFrameworkAttribute(".NETFramework,Version=v4.7.2", FrameworkDisplayName = ".NET Framework 4.7.2")]

It's located under obj\Debug folder.

How does this C# file exist by itself? It has no namespace specification and does not seem to be part of a class. Is it pulled into the build by some sort of special convention?

NOTE: I know about the following question:

Why does Visual Studio generates the following file .NETFramework,Version=v4.5.AssemblyAttributes.cs in the temp folder

The above question does not answer my question of how the file is pulled into the build.

Sabuncu
  • 5,095
  • 5
  • 55
  • 89
  • https://learn.microsoft.com/en-us/archive/blogs/kirillosenkov/msbuild-unnecessary-rebuilds-because-of-generated-assemblyattributes-cs – stuartd Apr 23 '21 at 17:04
  • @stuartd Please see my edit, that question does not answer my question of how the file is pulled into the build. – Sabuncu Apr 23 '21 at 17:08
  • That file is auto-generated by MSBuild. Since it also generates the C# compile command, it has no trouble including that file. Switch the build verbosity to Detailed to see it getting done, ought to give a hint why its location is wrong. – Hans Passant Apr 23 '21 at 17:56
  • @HansPassant Thank you. I am just trying to understand how the file stands on its own as .cs file, given that it has no namespace and class definitions. – Sabuncu Apr 23 '21 at 17:59
  • 1
    Hmm, a namespace is useful for type declarations. It doesn't have any, only the attribute matters. The C# compiler does not know what .net version you target, so msbuild has to help. – Hans Passant Apr 23 '21 at 18:15
  • @HansPassant Thank you Hans. If this question wasn't closed I could accept your answer. If you'd like I can post new question and you can answer there. Many thanks again, few people here like yourself. – Sabuncu Apr 23 '21 at 18:21
  • This question was reopened. – Peter O. Apr 23 '21 at 23:33
  • 1
    @Sabuncu I have reopened the question as you requested. The duplicate was concerning why the file was created in the temp folder, however your file was created in the Debug folder, so I thought that technically it wasn't a duplicate. But if you're asking _"what is this file for and how does it work?"_ then that's still off-topic: only the MSBuild designers could answer that. I suspect the answer is - it's their build process and they can do it however they like. – stuartd Apr 23 '21 at 23:35
  • @stuartd Thank you Stuart. My question is this: **This is a .cs file but its content is unlike that of a typical C# file: it contains no namespace, no class definition. It only contains using directives and a single attribute. Does this not break rules as a standalone .cs file? Apparently it does not, so what are the special exceptions that allow this sort of content.** Thanks again. – Sabuncu Apr 24 '21 at 05:23
  • @HansPassant This question has been reopened. I wonder if you'd consider posting your comment as an answer? Regards. – Sabuncu Apr 24 '21 at 05:26

0 Answers0