Saturday 26 March 2011

Reflection Interview Questions

What is reflection?

All .NET compilers produce metadata about the types defined in the modules they produce. This metadata is packaged along with the module (modules in turn are packaged together in assemblies), and can be accessed by a mechanism called reflection.

Which namespace is used in Reflection?
System.Reflection

Which namespace is used to built new types at runtime?


System.Reflection.Emit

Advantages of Reflection?

1. Used to access attribute sin programs metadata.
2. Examine and instantiate types in an assembly.
3. To perform late binding, accessing methods on the types created at runtime.

What is attributes? Types of attributes?

Attributes are the declarative tags that convey information at runtime.
Some predefind attributes are Serializable, Deserializable

Types:

1. Standard: Predefined attributes in .Net framework

eg.

 class attributeparamsdemo
    {
        [DllImport("user32.DLL", EntryPoint = "MessageBox")]
        static extern int MessageDialog(int hwnd, string msg, string caption, int msgtype);
        static void Main(string[] args)
        {
            MessageDialog(0, "MessageDialog called !", "DllImport demo", 0);

        }
    }
This will display the messagebox as output with the message "MessageDialog called ! " , caption  "DllImport demo" and
Ok button as msgtype 0 contains ok button.

2. Custom: Attributes defined by user

eg.

namespace demo_customattribute
{
    [AttributeUsage(AttributeTargets.All,Inherited = false,AllowMultiple = true)]
    public class bookattribute : System.Attribute
    {
        private string name;

        public string version;

        public bookattribute(string str)
        {
            name = str;
            version = "1.5";
        }
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {

           book b = new book();
           Type t = b.GetType();
           Attribute[] ats = Attribute.GetCustomAttributes(t);
           foreach (Attribute a in ats)
           {
               if (a is bookattribute)
               {
                   bookattribute author = (bookattribute)a;
                   Console.WriteLine("AuthorName = " + author.Name);
                   Console.WriteLine("Version = " + author.version.ToString());
               }
           }
           Console.ReadLine();
        }
    }
        [bookattribute("balaguruswamy", version = "1.1")]
        [bookattribute("ABC", version = "1.2"), bookattribute("yashwant")]
        public class book
        {
            private string bkname;

            public string Bkname
            {
                get { return bkname; }
                set { bkname = value; }
            }
        }
               
    }
[AttributeUsage(AttributeTargets.All,Inherited = false,AllowMultiple = true)]
<b> AttributeTargets </b>indicates that this attribute can be applied to all program elements.
<b>Inherited</b> indicates whether the attribute can be inherited by classes that are derived from the classes to which attribute is applied.
<b>AllowMultiple</b>indicates whether multiple instances of attribute can exists on an element.




No comments:

Post a Comment