You are on page 1of 4

For more information please visit

http://awesomecsharp.blogspot.in/2013/08/cattribute-and-reflection-awesome-c.html
Attribute and reflection in awesome C sharp
By santosh Attribute : Attribute are the simple techniques for adding metadata ("metadata : -It is data about data or in simple words it is important information ") information and behavior of code within applications. Reflection : We use reflection techniques pick the attribute related information at runtime introducing Attribute Attributes : are a mechanism for adding metadata, such as compiler instructions and other data about your data, methods, and classes, to the program itself. Attributes are inserted into the metadata and are visible through

ILDasm and other metadata-reading tools.

Or in other words An attribute is an object that represents data you want to associate with an element in your program. The element to which you attach an attribute is referred to as the target of that attribute.

Attributes are of two types: intrinsic and custom. Intrinsic attributes are supplied as part of the Common Language Runtime (CLR), and they are integrated into .NET. Custom attributes are attributes you create for your own purposes. Let's clear CLR first common language runtime (CLR) The common language runtime (CLR) is major component in the .NET Framework and it is the execution engine for .NET Framework applications. It is responsible for proving the number of services, including the following: 1. Code management (loading and execution) 2. Verification of type safety 3. Conversion of Microsoft Intermediate Language (MSIL) to native code 4. Access to metadata (enhanced type information) 5. Managing memory for managed objects 6. Enforcement of code access security (See what is code access security posting soon) 7. Exception handling, including cross-language exceptions 8. Interoperation between managed code, COM objects, and pre-existing DLLs (unmanaged code and data) 9. Automation of object layout 10. Support for developer services (profiling, debugging, and so on). Most programmers will use only intrinsic attributes, though custom attributes can be a powerful tool when combined with reflection. attributes are applied to an assembly, others to a class or interface, and some, such as [WebMethod], to class members. These are called the attribute target. Now question comes in mind how to apply attribute in programming?

We can apply attributes to their targets by placing them in square brackets immediately before the target item. You can combine attributes, either by stacking one on top of another and You must place assembly attributes after all using statements and before any code.

[assembly: AssemblyDelaySign(false)] [assembly: AssemblyKeyFile(".\\awesome_C_sharp.snk")] we can create our own custom attribute Lets see code demo of and use them at runtime.

Code example is here


using System; class Car { [Obsolete] public void Accept() { Console.Title = "Awesome c sharp"; Console.WriteLine("hello i m Accept"); } [ObsoleteAttribute] public void Display() { Console.WriteLine("hello i m Display"); } [Obsolete("You shouldn't use this method anymore.")] public void Crazy() { Console.WriteLine("this is crazy method"); } // make the program thread safe for COM [STAThread] static void Main(string[] args) { Car obj = new Car(); obj.Accept(); obj.Display(); obj.Crazy(); Console.ReadLine(); }}

now let's see positional and named attribute parameter

code is here using System; [assembly: CLSCompliant(true)] public class car { public void Accept(int n) { Console.WriteLine("hello i m Accept function "+n); } [STAThread] static void Main(string[] args) { Console.Title = "Awesome c sharp"; int num = 0; car c = new car(); c.Accept(num); Console.Read(); }}

the STAThreadAttribute attribute. You'll often see this attribute applied to the Main() method, indicating that this C# program should communicate with unmanaged COM code using the Single Threading Apartment . It is generally safe to use this attribute all the time because you never know when a 3rd party library you're using is going to be communicating with COM. The following excerpt shows how to use the STAThreadAttribute attribute:

Reflection Reflection : is the process by which a program can read its own metadata. A program is said to reflect on itself, extracting metadata from its assembly and using that metadata either to inform the user or to modify its own behavior. Let's see technical demonstration

System.Reflection is the namespace used to performing reflection as i have told you that reflection with assemblies can lot beneficial try it out Code is here using System; using System.Reflection; public class Car { public static void Main() { Console.Title = "Awesome c sharp"; // examine a single object Type theType = Type.GetType( "System.Reflection.Assembly"); Console.WriteLine(theType); // get all the members MemberInfo[] mbrInfoArray = theType.GetMembers(); foreach (MemberInfo mbrInfo in mbrInfoArray) { Console.WriteLine(mbrInfo+" "+ mbrInfo.MemberType); } Console.Read(); }} Thanks guys i m finishing here today hope you will like the post. i will be waiting for your mails and txt chat request on wizpert.com .... Thanks guys one last thing Don't u forget that you are awesome... Once again HAPPY RAKHI... take care God bless u... For more information please visit

http://awesomecsharp.blogspot.in/2013/08/c-attributeand-reflection-awesome-c.html
====================================================== all right reserved it 2013 awesome c sharp brought to u by vardana solution ======================================================

You might also like