Attribute Conditional marks a conditional method whose execution depends on a designated preprocessor identifier.
It causes conditional compilation of method calls, depending on the given value, such as Debug or Trace . For example, it displays the value of variables while debugging a code.
The syntax for determining this attribute in C # is as follows:
[ Conditional ( conditionalSymbol )]
For example:
[ Conditional ( "DEBUG" )]
Here is an example of Conditional illustration in C #:
#define DEBUG using System ; using System . Diagnostics ; public class QuanTriMangCom { [ Conditional ( "DEBUG" )] public static void Message ( string msg ) { Console . WriteLine ( msg ); } } class Test { static void function1 () { QuanTriMangCom . Message ( "Trong hàm 1." ); function2 (); } static void function2 () { QuanTriMangCom . Message ( "Trong hàm 2." ); } public static void Main () { QuanTriMangCom . Message ( "Trong hàm Main." ); function1 (); Console . ReadKey (); } }
Use the Console.ReadKey command (); to see the results more clearly.
Compiling and running the above C # program will produce the following results:
In the Main function
In function 1.
In function 2.
In C #, Obsolote marks a program entity that should not be used. It tells you to tell the compiler to remove a specific target element. For example, when a new method is being used in a class and if you still want to retain the old method in this class, you can mark it as obsolete (obsolete) by displaying a message as method. New mode will be used, instead of the old one.
The syntax to define this attribute in C # is as follows:
[ Obsolete ( message )] [ Obsolete ( message , iserror )]
Inside:
The message is a string describing why the item is obsolete and what is used instead.
iserror is a Boolean value. If its value is true, the compiler will treat the use of this item as an error. The default value is false (ie the compiler will generate a warning).
The following example illustrates obsolete in C #:
using System ; public class QuanTriMangCom { [ Obsolete ( "Đừng sử dụng QTM1, hãy sử dụng QTM2" , true )] static void QTM1 () { Console . WriteLine ( "Đây là phương thức cũ" ); } static void QTM2 () { Console . WriteLine ( "Đây là phương thức mới" ); } public static void Main () { QTM1 (); } }
When you try to compile the above program, the compiler will issue an error message:
Compilation failed: 1 error(s), 0 warnings
main.cs(13,7): error CS0619: `QuanTriMangCom.QTM1()' is obsolete: `Đừng sử dụng QTM1, hãy sử dụng QTM2'
Also called custom attributes or user-defined attributes. The .Net Framework allows creating Custom Attributes that can be used to store declaration information and can be retrieved at runtime. This information may relate to any target element depending on the design criteria and application requirements.
Creating and using Custom Attribute in C # includes 4 steps:
The final step involves writing a simple program to read through metadata to find different notations. Metadata is data or information used to describe other data. This program should use reflection to access attributes at runtime. We will talk more about this in the next article.
Step 1: Declare a Custom Attribute in C #
A new Custom Attribute should be derived from the System.Attribute class in C #. For example:
//Thuộc tính tùy chính BugFix được gán cho lớp và thành viên của nó. [ AttributeUsage (
AttributeTargets . Class | AttributeTargets . Constructor | AttributeTargets . Field | AttributeTargets . Method | AttributeTargets . Property , AllowMultiple = true )] public class DeBugInfo : System . Attribute
In the above code, we have declared a Custom Attribute as DeBugInfo.
Step 2: Build Custom Attribute in C #
We build a Custom Attribute named DeBugInfo, which stores the information obtained by debugging any program. It will store the following information:
The DeBugInfo class has 3 private properties to store the first 3 information and a public property to store the message. Therefore, the error code, developer name, and review date are the corresponding positional_parameter of DeBugInfo class and notice the name_parameter.
Each attribute must have at least one constructor. The corresponding positional_parameter should be transmitted through that constructor. The following example illustrates the DeBugInfo class on:
//Custom attribute BugFix được gán cho các lớp và thành viên của nó.
[ AttributeUsage ( AttributeTargets . Class | AttributeTargets . Constructor | AttributeTargets . Field | AttributeTargets . Method | AttributeTargets . Property , AllowMultiple = true )] public class DeBugInfo : System . Attribute { private int bugNo ; private string developer ; private string lastReview ; public string message ; public DeBugInfo ( int bg , string dev , string d ) { this . bugNo = bg ; this . developer = dev ; this . lastReview = d ; } public int BugNo { get { return bugNo ; } } public string Developer { get { return developer ; } } public string LastReview { get { return lastReview ; } } public string Message { get { return message ; } set { message = value ; } } }
Step 3: Apply Custom Attribute in C #
The Custom Attribute in C # is applied by placing it right before its target:
[ DeBugInfo ( 45 , "Nguyễn Huy" , "03/09/2017" , Message = "Kiểu trả về không hợp lệ" )] [ DeBugInfo ( 49 , "Hà Minh" , "09/11/2017" , Message = "Biến chưa được sử dụng" )] class Rectangle { //các biến thành viên protected double dai ; protected double rong ; public Rectangle ( double d , double r ) { dai = d ; rong = r ; } [ DeBugInfo ( 55 , "Nguyễn Huy" , "03/09/2017" , Message = "Kiểu trả về không hợp lệ" ) ] public double tinhS () { return dai * rong ; } [ DeBugInfo ( 56 , "Hà Minh" , "09/11/2017" ) ] public void Display () { Console . WriteLine ( "Chiều dài: {0}" , dai ); Console . WriteLine ( "Chiều rộng: {0}" , rong ); Console . WriteLine ( "Diện tích: {0}" , tinhS ()); } }
In the next chapter, we retrieve information Attribute using a Reflection class object in C #.
Follow tutorialspoint
Previous article: File I / O in C #
Next article: Reflection in C #