Introduction
- using System;
- namespace attributes
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Attributes sample");
- TestMethod();
- Console.ReadKey();
- }
- [Obsolete("Deprecated Method",false)]
- public static void TestMethod()
- {
- Console.WriteLine("Hello world");
- }
- }
- }

|
Attributes
|
Description
|
|
[Serialization]
|
By marking these attributes, a class is able to persist its current state into the stream.
|
|
[NonSerialization]
|
It specifies that a given class or field should not be persisted during the serialization process.
|
|
[Obsolete]
|
It is used to mark a member or type as deprecated. If they are attempted to be used somewhere else then compiler issues a warning message.
|
|
[DllImport]
|
This allows .NET code to make calls to an unmanaged C or C++ library.
|
|
[WebMethod]
|
This is used to build XML web services and the marked method is being invoked by an HTTP request.
|
|
[CLSCompliant]
|
Enforce the annotated items to conform to the semantics of CLS.
|
Attributes Description
To illustrate the predefined attributes in action, let's create a console based application to apply the implementation of them.
[Serialization] and [NonSerialization]
Here, assume that you have built a test class that can be persisted in a binary format using the [Serialization] attribute.
- [Serializable]
- public class test
- {
- public test() { }
- string name;
- string coutnry;
- [NonSerialized]
- int salary;
- }

- [WebService(Namespace = "http://tempuri.org/")]
- [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
- // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
- // [System.Web.Script.Services.ScriptService]
- public class UtilityWebService : System.Web.Services.WebService {
- public UtilityWebService () {
- //Uncomment the following line if using designed components
- //InitializeComponent();
- }
- [WebMethod]
- public string HelloWorld() {
- return "Hello World";
- }
- [WebMethod]
- public int addition(int a,int b)
- {
- return a+b;
- }
- }
- using System;
- using System.Runtime.InteropServices;
- namespace attributes
- {
- public class test
- {
- [DllImport("user32.dll", EntryPoint = "MessageBox")]
- public static extern int ShowMessageBox(int hWnd,string text, string caption,uint type);
- }
- class Program
- {
- static void Main(string[] args)
- {
- string caption = "Hello World";
- string text = "Sample Article on DLLImport Attribute";
- test.ShowMessageBox(0, text, caption, 0);
- Console.ReadKey();
- }
- }
- }


- The custom attribute class should be derived from System.Attribute.
- The Attribute name should have the Attribute suffix.
- Set the probable targets with the AttributeUsage attribute.
- Implement the class constructor and write-accessible properties.
The first step in building a custom attribute is to create a new class FunWith with an Attribute suffix that is derived from the System.Attribute class. Then define the class constructor and write an accessible property, Company.
- [AttributeUsage(AttributeTargets.Class)]
- public class FunwithAttribute : Attribute
- {
- public FunwithAttribute(string s)
- {
- this.Company = s;
- }
- public string Company { get; set; }
- }
- [Funwith("HCL Technology")]
- public class test
- {
- public test(string name, string country)
- {
- this.EmpName = name;
- this.Country = country;
- }
- public string FullDetails()
- {
- string str = EmpName + "-" + Country;
- return str;
- }
- private string EmpName;
- private string Country;
- }
- class Program
- {
- static void Main(string[] args)
- {
- test obj = new test("Ajay","India");
- Console.WriteLine("Result:{0}",obj.FullDetails());
- Console.ReadKey();
- }
- }

- static void Main(string[] args)
- {
- MemberInfo info = typeof(test);
- object[] attrib = info.GetCustomAttributes(typeof(FunwithAttribute), false);
- foreach (Object attribute in attrib)
- {
- FunwithAttribute a = (FunwithAttribute)attribute;
- Console.WriteLine("Company: {0}", a.Company);
- }
- }
- using System;
- using System.Reflection;
- public class CheckStatus : Attribute
- {
- private bool Val = false;
- public bool status
- {
- get { return Val; }
- }
- public CheckStatus(bool val)
- {
- Val = val;
- }
- }
- [CheckStatus(false)]
- public class test
- {
- private string EmpName;
- private string Country;
- public test(string name, string country)
- {
- this.EmpName = name;
- this.Country = country;
- }
- public string FullDetails()
- {
- string str = null;
- Type type = this.GetType();
- CheckStatus[] attrib = (CheckStatus[])type.GetCustomAttributes(typeof(CheckStatus), false);
- if (attrib[0].status == true)
- {
- str = EmpName + "-" + Country;
- }
- else
- {
- str = "Hi " + EmpName;
- }
- return str;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- test obj = new test("Ajay","India");
- Console.WriteLine("Result:{0}",obj.FullDetails());
- Console.ReadKey();
- }
- }

- using System.Reflection;
- using System.Runtime.CompilerServices;
- using System.Runtime.InteropServices;
- [assembly: AssemblyTitle("CustomAttribute")]
- [assembly: AssemblyDescription("")]
- [assembly: AssemblyConfiguration("")]
- [assembly: AssemblyCompany("")]
- [assembly: AssemblyProduct("CustomAttribute")]
- [assembly: AssemblyCopyright("Copyright © 2013")]
- [assembly: AssemblyTrademark("")]
- [assembly: AssemblyCulture("")]
- [assembly: ComVisible(false)]
- [assembly: Guid("ce5fc30b-e670-4115-aa64-4be10e7b6ea9")]
- [assembly: AssemblyVersion("1.0.0.0")]
- [assembly: AssemblyFileVersion("1.0.0.0")]

Anil KumarPosted Feb 11, 2015, 11:50 PM
Hey, you made me your fond of :) I do eagerly wait for your articles! Keep rocking man!