[C #] Analyzing AssemblyInfo. cs,
Analysis of AssemblyInfo. cs-learn about common Attribute
[Blogger] Anti-bone Aberdeen [original] http://www.cnblogs.com/liqingwen/p/5944391.html
Collation
Last time, we used "C # knowledge Review-feature Attribute" to learn how to create and use feature attributes. This time, let's take a look at the AssemblyInfo file that comes with every time VS is used to create a project. cs.
Directory
Core code
Show the code in the diagram and check the Arrow
Using System. Reflection; using System. Runtime. InteropServices; // The general information about the assembly is controlled through the following feature set. Modify these feature values to modify // information associated with the Assembly. [Assembly: AssemblyTitle ("MusicStore")] [assembly: AssemblyDescription ("")] [assembly: AssemblyConfiguration ("")] [assembly: AssemblyCompany ("Microsoft")] [assembly: AssemblyProduct ("MusicStore")] [assembly: AssemblyCopyright ("Copyright©Microsoft 2016 ")] [assembly: AssemblyTrademark (" ")] [assembly: AssemblyCulture (" ")] // setting ComVisible to false will make the type in this Assembly invisible to the COM component. If you need to // access a type in this Assembly from COM, set the ComVisible attribute to true for this type. [Assembly: ComVisible (false)] // if this item is made public to COM, the following GUID is used for the ID of typelib [assembly: Guid ("a9ef3281-9049-4a52-a2f1-2061d442200e")] // The Assembly version information consists of the following four values: // main version/secondary version/internal version/revision version // All values can be specified, you can also use the default values of revision number and internal version number to use "*": [assembly: AssemblyVersion ("1.0.0.0")] [assembly: assemblyFileVersion ("1.0.0.0")]
I. Global attributes
Most features apply to specific language elements (such as classes or methods), but some attributes apply globally to the entire assembly or module. For example, the AssemblyVersionAttribute can be used to embed version information into the assembly.
Global features appear in any top-levelusingCommand and any type, module, or namespace declaration. The global feature can be displayed in multiple source files, but files must be compiled during a single compilation. In the C # project, they are in the AssemblyInfo. cs file.
The Assembly feature is the value that provides information about the assembly. They are divided into the following categories:
① Assembly identity feature
② Information Features
③ Assembly list features
④ Strong name features
1. Assembly identity features
The three features (Strong names, if applicable) determine the Assembly identifier: name, version, and culture. When cited in code, these features constitute the complete name of the Assembly. You can set the version and culture of an assembly. However, the name value is set by the compiler in Visual Studio IDE of the "assembly information" dialog box. After an assembly is created, the name value is based on the file that contains the Assembly List. The assemblyflagsattri attribute specifies whether multiple copies of an assembly can coexist.
Figure-"assembly information" dialog box
Figure- ing between nouns and AssemblyInfo. cs files in the "assembly information" dialog box
2. Information Features
You can use the information feature to provide other company or product information for the Assembly.
3. assembly list features
You can use the Assembly list feature to provide information in the assembly list. This includes the title, description, default alias, and configuration.
Ii. Outdated featuresObsoleteProperty indicates that a program entity is marked as one that is not recommended for use. Every time an object is marked as obsolete, a warning or error will be generated subsequently to configure attributes.
1 /// <summary> 2 // old class 3 /// </summary> 4 [Obsolete ("Please use" + nameof (NewClass)] 5 class OldClass 6 {7 public void Method () {} 8} 9 10 /// <summary> 11 // New class 12 // </summary> 13 class NewClass14 {15 [Obsolete ("Please use" + nameof (newMethod ), true)] 16 public void OldMethod () {} 17 18 public void NewMethod () {} 19}
1 class Program 2 {3 static void Main (string [] args) 4 {5 var oldClass = new OldClass (); // warning 6 7 var newClass = new NewClass (); 8 newClass. oldMethod (); // error 9} 10}
Condition features
ConditionalThe attribute execution method depends on the pre-processing identifier.ConditionalThe attribute is the alias of ConditionalAttribute and can be applied to methods or attribute classes.
In this example,ConditionalApply to methods to enable or disable Program-specific diagnostic information:
1 class Debug 2 { 3 [Conditional("DEBUG")] 4 public static void Output(string msg) 5 { 6 Console.WriteLine(msg); 7 } 8 } 9 10 class Program11 {12 static void Main(string[] args)13 {14 Debug.Output("This is Debug!");15 Console.WriteLine("Done!");16 17 Console.Read();18 }19 }
ConditionalAttributes are usually used inDEBUGIdentifier indicates the debug version of the feature that enables tracing and records, but is not in the release version.
Class Debug {public static void Output (string msg) {Console. writeLine (msg) ;}} class Program {static void Main (string [] args) {# if DEBUG. output ("This is Debug! "); # Endif Console. WriteLine (" Done! "); Console. Read ();}}
UseConditionalMore clean, of course, you can also put less commonly used or error-prone methods in#if…#endifInternal block ID.
4. The caller information attributes use the Caller information attributes to obtain information about the caller and pass it to the method. You can obtain the file path of the source code, line number in the source code, and member name of the caller.
To obtain information about the member caller, use the attributes that apply to optional parameters. Specify the default value for each optional parameter.
[Reference] Microsoft official documentation