About Me

My photo
Life is good...Life is wonderful...Life is love...Oh yeah yeah yeah... It all part of that game...It all good That's the way...oh yeah yeah yeah... Thodi si meethi hai, Zara si 'MIRCHI' hai... Sau gram zindagi yeh, Sambhaal ke kharchi hai... Asli hai, jhooti hai, Khaalis hai, farzi hai...oh yeah yeah yeah... ~ Dream, Achieve and Win ~

Friday, June 8, 2012

Attribute and Reflection in C#


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Csharp
{
    //DEFINING CUSTOM ATTRIBUTE
    [AttributeUsage(AttributeTargets.Class |
        AttributeTargets.Method|
        AttributeTargets.Field|
        AttributeTargets.Property|
        AttributeTargets.Constructor , AllowMultiple =true)]
   
    //CONSTRUCTING AND NAMING NEW CUSTOM ATTRIBUTE 
    public class ClassAttributes : System.Attribute
    {
       
        //POSITIONAL PARAMETERS-specify essential information about attributes
        private int sno;
        private string msg;
       
        //MUST HAVE ATLEAST ONE CONSTRUCTOR.
        //constructor of ClassAttribute Class for positional parameters
        public ClassAttributes(int s, string m)
        {
            this.sno=s;
            this.msg=m;
        }
           
        public int getSno
        {
         get{   return sno;}
        }
        public string getMsg
        {
            get { return msg; }
        }
    }

   
    //CLASS LEVEL attribute set----APPLYING
    [ClassAttributes(1,"Amrit Setting Class Level Attributes")]
    [ClassAttributes(2, "i am Attribute of class -> AttributeExample")]
    class AttributeExample
    {
        //METHOD LEVEL attribute set---APPLYING
         [ClassAttributes(3, "I am Attribute of method DisplayClassMetaData()")]
        //display metadeta using reflecftion
        public void DisplayClassMetaData()
        {

            Type type = typeof(AttributeExample);
           
            //display "Class LEVEL" attributes...
            Console.WriteLine("\nCLASS LEVEL ATTRIBUTES");
            foreach (Object a in type.GetCustomAttributes(typeof(ClassAttributes), false))
            { // create object
                ClassAttributes obj = (ClassAttributes)a;
                if (null != obj)
                {
                    Console.WriteLine("\nSNo={0} \n Message={1}\n", obj.getSno, obj.getMsg);
                }

            }
         }
       

 //again METHOD LEVEL attribute set---APPLYING
[ClassAttributes(4, "I am Attribute of method  DisplayMethodMetaData ()")]

 public void DisplayMethodMetaData()
         {
             Type type = typeof(AttributeExample);
             //display "Method LEVEL attributes....
             Console.WriteLine("\nMETHOD LEVEL ATTRIBUTES");
             foreach (MethodInfo method in type.GetMethods())
            {  foreach(Attribute a in method.GetCustomAttributes(true))
                {
                    try
                    {
                        ClassAttributes obj = (ClassAttributes)a;
                        if (null != obj)
                        {
                            Console.WriteLine("\nSNo={0} \n Message={1}\n", obj.getSno, obj.getMsg);
                        }
                    }
                    catch (Exception e)
                    {
                    }
                    finally
                    {
                        Console.WriteLine("***********");
                    }
                       
                }
            }

        }
        public static void Main()
        {
            AttributeExample o = new AttributeExample();
            o.DisplayClassMetaData();
            Console.ReadLine();
            o.DisplayMethodMetaData();
            Console.ReadLine();

        }

    }
}

Monday, June 4, 2012

Returning More Than One Values


class CallingFunc
    {
        void Input()
        {
            int a;
            int b;
            int c,d,e,f;
            a = Convert.ToInt32(Console.ReadLine());
            b = Convert.ToInt32(Console.ReadLine());
            Console.ReadLine();
            System.Console.WriteLine("Before Increment Inside Input \na=" + a + " \nb =" + b);
            Console.ReadLine();
            Increment(a, b, out c, out d, out e, out f);
            Console.ReadLine();
            System.Console.WriteLine("After Increment Inside Input \n a=" + a + " \n b =" + b);
            System.Console.WriteLine(c);
            System.Console.WriteLine(d);
            System.Console.WriteLine(e);
            System.Console.WriteLine(f);
        }
        void Increment( int a,int b, out int c, out int d, out int e, out int f)
        {
            a = a * 100;
            b = b * 500;
            Console.ReadLine();
            System.Console.WriteLine("Inside Increment \n a=" + a + " \n b =" + b);
             c=a+b;
            d=a-b;
            e=a*b;
            f=a/b;
       
        }
        public static void Main()
        {
            CallingFunc  o = new CallingFunc ();
            o.Input();
            Console.ReadLine();
        }
    }

Pass By Reference


class PassByReference
    {
        void Input()
        {
            int a;
            int b;
            a = Convert.ToInt32(Console.ReadLine());
            b = Convert.ToInt32(Console.ReadLine());
            System.Console.WriteLine("Before Increment Inside Input \n a=" + a + " \n b =" + b);
            Increment(ref a,ref  b);
            System.Console.WriteLine("After Increment Inside Input \n a=" + a + " \n b =" + b);
        }
        void Increment(ref int a, ref int b)
        {
            a = a * 100;
            b = b * 500;
            System.Console.WriteLine("Inside Increment \n a=" + a + " \n b =" + b);

        }
        public static void Main()
        {
            PassByReference o = new PassByReference();
            o.Input();
            Console.ReadLine();
        }
    }

Pass By Value



class PassByValue
    {

        void Input()
        {
            int a;
            int b;
            a = Convert.ToInt32(Console.ReadLine());
            b = Convert.ToInt32(Console.ReadLine());
            System.Console.WriteLine("Inside Input Before Increment\n a=" + a + " \n b =" + b);
            Increment(a,b);
            System.Console.WriteLine("Inside Input After Increment \n a=" + a + " \n b =" + b);
        }
        void Increment(int a, int b)
        {
            a = a * 100;
            b = b * 500;
            System.Console.WriteLine("Inside Increment \n a=" + a + " \n b =" + b);

        }
        public static void Main()
        {
            PassByValue o = new PassByValue();
            o.Input();
            Console.ReadLine();
        }
    }