Thursday, March 31, 2011

Understanding the Reflection in console Application:

CLR executable modules contain information that is called metadata, which describes the executable code that they contain. Reflection is the technology that enables a program to interact with both this metadata and the executable code.
 Other words, Computer programs consist of instructions that tell the computer how to process data. In most circumstances, the instructions from a program are not considered as part of the data that the program processes. Sometimes, however, a program may benefit from the ability to examine itself at run time, and to alter its behavior or even its structure depending on what it finds. The technology that enables a program to use this powerful ability is called reflection.

The Microsoft .NET Framework version 2.0 provides a comprehensive set of classes in the System.Reflection namespace that model the structure of a common language runtime (CLR) program. These classes enable you to examine the structure of a program, dynamically load instructions (in the form of assemblies) into your program while it runs, and create new program instructions that can be executed or saved as a new assembly.

Loading an Assembly:
The assembly Class:-

•    Provides methods to load an assembly by specifying a fully qualified assembly name, an AssemblyName instance, or a path name.
•    Supports loading for reflection only (no code execution).
•    Use the CLR assembly loader (Fusion) to locate assemblies and apply version-binding redirection policies.
•    Does not always load the assembly that you expect.
•    Provides method to access the assembly’s type.
Load, LoadFrom, ReflectionOnlyLoad, ReflectionOnlyLoadFrom:

How the CLR Loads an Assembly:
When the just-in-time (JIT) compiler compiles the IL for a method, it checks the IL for references to types from other assemblies. If there are any references to other types, the JIT compiler uses the TypeRef and AssemblyRef metadata tables to locate the assembly that defines the referenced type. The AssemblyRef table holds identity information about the target assembly, which consists of the assembly’s name, version, culture, and public key token for a strongly named assembly or just the assembly’s name for a weakly named assembly. Internally, the CLR passes the identity information of the assembly to one of the static Load overload methods in the Assembly class to load the assembly into the process.

For example Loading an Assembly:-
using System;
using System.Reflection;
using System.Runtime.Remoting;

[assembly: AssemblyTitle ("Reflection1")]

namespace Reflection
{
        
    class Program
    {

        static void Main(string[] args)
        {
            string s = "hello";

            //load method
              Loadmethod();

             }

        public void add()
        {
            Console.WriteLine("add method ");
        }


        public static void Loadmethod()
        {
            string assemblyId = "System.Drawing, version=2.0.0.0, " +
"culture=neutral, PublicKeyToken=b03f5f7f11d50a3a";
            Assembly a = Assembly.Load(assemblyId);
            foreach (Type type in a.GetTypes())
            {
                Console.WriteLine(type.FullName);
            }
        }
    }
 }

Output is display below:-

No comments:

Recent Posts