[C #] Analyzing AssemblyInfo. cs,
Profiling AssemblyInfo. cs-understanding common feature attributes
[Blogger] Anti-bone Aberdeen [original] http://www.cnblogs.com/liqingwen/p/5944391.html
Collation
Previously, 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
- Global features
- Outdated features:
Obsolete
- Condition features:
Conditional
- Caller information features
AssemblyInfo. cs
Select an AssemblyInfo. cs file randomly. Open 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 features
Most features apply to specific language elements (such as classes or methods), but some features apply globally to the entire assembly or module. For example, AssemblyVersionAttribute can be used to embed version information into a set of programs.
Global features appear in any top-levelusing
Command 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. Feature: You can set the version, culture, and name value of the Assembly by the compiler in Visual Studio IDE of the "assembly information" dialog box. After creating the assembly, according to 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 features:Obsolete
Obsolete
Feature 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 accordingly.
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:Conditional
Conditional
The feature execution method depends on the preprocessing identifier.Conditional
The attribute is the alias of ConditionalAttribute and can be applied to methods or attribute classes.
In this example,Conditional
Apply 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 }
Conditional
Features are usually used inDEBUG
Identifier 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 ();}}
UseConditional
More clean, of course, you can also put less commonly used or error-prone methods in#if…#endif
Internal block ID.
4. The caller information feature uses the Caller information attribute 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.
1 internal class Program 2 {3 private static void Main (string [] args) 4 {5 CallerMethod (); 6 Console. read (); 7} 8 9 public static void CallerMethod () 10 {11 TraceMessage ("I am the caller"); 12} 13 14 public static void TraceMessage (string msg, 15 [CallerMemberName] string name = "", 16 [CallerFilePath] string filePath = "", 17 [CallerLineNumber] int lineNumer = 0) 18 {19 Trace. writeLine ($ "{nameof (msg) }:{ msg}"); 20 Trace. writeLine ($ "{nameof (name) }:{ name}"); 21 Trace. writeLine ($ "{nameof (filePath) }:{ filePath}"); 22 Trace. writeLine ($ "{nameof (lineNumer) }:{ lineNumer}"); 23} 24}
1. Remarks
You must specify an explicit default value for each optional parameter. The caller information feature cannot be applied to unspecified parameters.
The caller information feature does not make the parameter an optional parameter. On the contrary, they will affect the default value when this parameter is ignored.
During compilation, the caller information value is passed into the intermediate language (IL) as the text ). Unlike the result of the exception StackTrace feature, these results are not affected by fuzzy processing.
You can explicitly provide optional parameters to control Caller information or hide Caller information.
Portal
C # knowledge Review-feature Attribute
C # knowledge Review-serialization
C # knowledge Review-Expression Tree Expression Trees
[Reference] Microsoft official documentation