Lesson 6 : Reflection, attrib,preprocessor



 

 

monodevelop

imagecredit:monodevelop

Reflection

In order to make you grasp the idea of reflection.Let us first discuss some key concepts

Metadata

The data about other data is known as meta data.The logical collection of information is known as data.We can see that our program consists of data in the form of variables,methods,classes,properties etc.

Managed Code

Code that is executed by the CLR.
Code that targets the common language runtime, the foundation of the .NET Framework, is known as managed code.
Managed code supplies the metadata necessary for the CLR to provide services such as memory management, cross-language integration, code access security, and automatic lifetime control of objects. All code based on IL(Intermediate Language) executes as managed code.
Code that executes under the CLI execution environment.

Taken from Answer 9 of the mentioned link

http://stackoverflow.com/questions/334326/what-is-managed-unmanaged-code-in-c

Now my own words:

Managed code is used in Visual Studio environment since the era when .Net framework introduced in early 2000’s.In the .Net framework support by Visual Studio’s provide the facility that each and every language supported by the framework such as C#,C++,Vb.Net etc run under the Common Language Runtime or CLR. The CLR provides the JIT (Just in time) compilation facility provides the feature to translate the user written code (irrespective of the language used) in to the machine readable code so that the compiler understands and executes it swiftly.
There had been the problem in older versions before .Net that the components written in C++ were difficult to use in vb at that time.Now we can write a component in C# and use it in Vb.Net code.
CLR also manages memory quite effectively.Exception handling (will be discusses later),garbage collection and thread management are the best features available.The standard which CLR follows for converting the same machine code of different programming languages is know as CLI (Common Language Specification)

Let’s talk about reflection

Reflection

Let’s say if we want our managed code to look for assemblies,modules and types information at runtime,we have to make it look for its own metadata through reflection.The code reflects back its own features.

 

Example1using System;
using System.Reflection;public class MyClass
{

public virtual int MultiplyNumbers(int a,int b)
{

return (a*b);

}

}

class Program
{
public static void Main()
{
Console.WriteLine(“—-Reflection Example—-“);
MyClass Obj = new MyClass();

Type ObjType = Obj.GetType();

MethodInfo ClassMethodInfo = ObjType.GetMethod(“MultiplyNumbers”);

Console.WriteLine(“Method information:{0} n”, ClassMethodInfo.ToString());

Console.WriteLine(“Class Name:{0} n”,ObjType.ToString() );

Console.WriteLine(“Is Class abstract ?{0} n”, ObjType.IsAbstract);

Console.WriteLine(“Is Class public ?{0} n”, ObjType.IsPublic);

Type ClassType = typeof(MyClass);

//Create an object instance
object ObjInstance = Activator.CreateInstance(ClassType);

// passing parameters
object[] Parameters = new object[] { 5,5 };
// Display the Invoked method.
Console.Write(“n Method- ” + ClassMethodInfo.Name+”()” + ” returns ” +
ClassMethodInfo.Invoke(Obj, Parameters) + “n”);

}
}

 

Output

Attributes

Attributes are associated or attached with classes,methods,properties,structures,interfaces,constructors,modules etc.The main objective of using an attribute is to tell the compiler about what to do with a specific block of code.The attributes can be accessed both at runtime and compile time.There are two types of attributes , pre-defined and user-defined.First we will check the predefined attributes already present.
Don’t worry about the complexity.It is not so complex to understand.Your worries are mine.Let us go through the examples.If you are interested to skim through the pre-defined attributes then microsoft has everything to offer you in this regard.Click the button and see them 🙂

The attribute is declared as follows

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Constructor | AttributeTargets.Field | AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = true)]

Click to see the list of predefined attributes

Examples

Example1

Look at it and get the exact idea

 

using System;
using System.Reflection;//Attribute targets Classes,Constructors…Properties etc

[AttributeUsage(AttributeTargets.Class |
AttributeTargets.Constructor |
AttributeTargets.Field |
AttributeTargets.Method |
AttributeTargets.Property,
AllowMultiple = true)] /*AllowMultiplte allows multiple instances of the same attribute
can be used */

//The class has to be deriven from the Attribute class to make it work as an attribute
public class InventoryMovement : System.Attribute
{
private int itemcode;
private string itemtype;
private string shipmentDate;

public InventoryMovement(int _code, string _type,string _date)
{
this.itemcode = _code;
this.itemtype = _type;
this.shipmentDate = _date;
}

public int Itemcode
{
get
{
return itemcode;
}
}
public string Itemtype
{
get
{
return itemtype;
}
}
public string ShipmentDate
{
get
{
return shipmentDate;
}
}

}

// attribute for class
//using first instance of the attribute
[InventoryMovement(1155, “Laptops”, “14/11/2013”)]
//using second instance of the attribute

[InventoryMovement(1223, “Printers”, “01/11/2013”)]

class CheckShipment
{

//attribute for method
//using first instance of the attribute
[InventoryMovement(1155, “Laptops”, “14/11/2013”)]
//using second instance of the attribute

[InventoryMovement(1223, “Printers”, “01/11/2013”)]
public int ShipmentStatus(int _itemcode,string _itemtype)
{

if (_itemcode == 1155 && _itemtype == “Laptops”)
{

return 0;
}

else if (_itemcode == 1223 && _itemtype == “Printers”)
{

return 1;
}

else
{

Console.WriteLine(“Sorry! no such item found”);
return 2;

}
}

static void Main()
{
Console.WriteLine(“n Enter Item Code:”);
int itemcode = int.Parse(Console.ReadLine());

Console.WriteLine(“n Enter Item Type:”);
string itemtype = Console.ReadLine().ToString();

CheckShipment shipment = new CheckShipment();
int i=shipment.ShipmentStatus(itemcode,itemtype);
if (i == 1)
{

Console.WriteLine(“nItem with code:{0} and type:{1} has been shipped”,itemcode,itemtype);

}

else if (i == 0)
{

Console.WriteLine(“nItem with code:{0} and type:{1} has not been shipped”, itemcode, itemtype);

}

}

}

Outputs

Pre-processor Directives

After doing some heavy lifting by myself,I expect from you guys that you should explore these directives by yourselves.No complexity or anything quite different is needed.Ask me through your precious comments if you have any problem.Follow the msdn link

Click here!

[If you don’t have the visual studio IDE right now.Just type ” Online C# Compilers” in google.They have the best compilers online for you absolutely free]

Lesson 6 completed……

 

Copyrighted.com Registered & Protected 
W81O-7XOF-UC7A-OUQV


Syed Adeel Ahmed
Syed Adeel Ahmed
Analyst, Programmer, Educationist and Blogger at Technofranchise
Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.

Published by

Syed Adeel Ahmed

Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.