As we know .Net compiler provides the information about all types (classes, variables,….etc) by generating the metadata. .Net provides one more way to add additional information through attributes. Attributes are nothing more than the code annotations that can be applied to a given type like Class, Interface, Structure, method, property,……etc.
Basically attributes in .Net are class types and these extend the abstract class System.Attribute. .Net provides various predefined attributes and as a developer we can create our own custom attribute by inheriting the System.Attribute class.
There are several predefined attributes in .Net and we can use these attributes for different purpose. Today we discuss about different predefined attributes.
CLSCompliant: This attribute enforces any item to follow the Common Language Specifications (CLS). We can apply CLSCompliant attribute over the class as shown below.
[CLSCompliant(true)]
public class Employee
{
public string GetEmpName()
{
return "Raj";
}
}
As shown above we are applying the attribute between [ and ] brackets. If we provides the true for CLSCompliant attribute, that particular type has to follow all CLS rules. As shown above example Employee class has to follow CLS rules.
DllImport: This attribute allows us to import unmanaged C- or C++-based code library, including the API of the underlying operating system and [DllImport] is not used to communicate with COM assemblies.
Obsolete: If you want to make any type obsolete, apply obsolete attribute on that type as shown below.
public class Employee
{
[Obsolete("Another method is used to get the Employee Information",false)]
public string GetEmpName()
{
return "Raj";
}
}
Here we apply the Obsolete attribute on GetEmpName() method and whenever client calls this method, it displays Warning message as “Another method is used to get the Employee Information”. If you want to display error message instead of warning make Boolean value as true as shown below.
public class Employee
{
[Obsolete("Another method is used to get the Employee Information",true)]
public string GetEmpName()
{
return "Raj";
}
}
Whenever any client calls the above code, .Net compiler generates error message as “Another method is used to get the Employee Information”. Obsolete attribute is very useful whenever we want force the existing client to use different method.
Serializable & NonSerialized: We can apply Serializable attribute to force any type to serialize and NonSerialized attribute is used to avoid the serialization as shown below.
[Serializable]
public class Employee
{
[NonSerialized]
public string GetEmpName()
{
return "Raj";
}
public double GetSal()
{
return 1000.10;
}
}
In the above code we apply the Serializable class over the Employee class, so all types within Employee class are serializable unless if we applies the NonSerialized attribute. That means all Employee members are serialized other than GetEmpName() member because NonSerialized attribute is applied on GetEmpName() method.
WebMethod: WebMethod attribute makes the method invokable via HTTP requests and instructs the CLR to serialize the method return value as XML.
public class EmployeeService : System.Web.Services.WebService
{
[System.Web.Services.WebMethod(BufferResponse = false)]
public string GetEmpName()
{
//implementation code
}
}
As shown above GetEmpName() method marked with WebMethod attribute, so client can invoke this method over HTTP.