Reflection
Reflection is the ability of inspecting an assemblie's metadata at runtime.
It is used to find all types in an assembly and/or dynamically invoke methods in an assembly.
This includes information about the type, properties, methods, and events of an object.
With Reflection, we can dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties.There are several uses of reflection.
1. When you drag and drop a button on a win forms or an asp.net application. The properties window uses reflection to show all the properties of the Button class. So,reflection is extensivley used by IDE or a UI designers.
2. Late binding can be achieved by using reflection. You can use reflection to dynamically create an instance of a type, about which we don't have any information at compile time. So, reflection enables you to use code that is not available at compile time.
Example:
using System;
using System.Reflection;
namespace Sample
{
public class MainClass
{
private static void Main()
{
// Get the Type Using GetType() static method
Type T = Type.GetType("Sample.Customer");
// Print the Type details
Console.WriteLine("Full Name = {0}",T.FullName);
Console.WriteLine("Just the Class Name = {0}",T.Name);
Console.WriteLine("Just the Namespace = {0}", T.Namespace);
Console.WriteLine();
// Print the list of Methods
Console.WriteLine("Methods in Customer Class");
MethodInfo[] methods = T.GetMethods();
foreach (MethodInfo method in methods)
{
// Print the Return type and the name of the method
Console.WriteLine(method.ReturnType.Name + " " + method.Name);
}
Console.WriteLine();
// Print the Properties
Console.WriteLine("Properties in Customer Class");
PropertyInfo[] properties = T.GetProperties();
foreach (PropertyInfo property in properties)
{
// Print the property type and the name of the property
Console.WriteLine(property.PropertyType.Name + " " + property.Name);
}
Console.WriteLine();
// Print the Constructors
Console.WriteLine("Constructors in Customer Class");
ConstructorInfo[] constructors = T.GetConstructors();
foreach (ConstructorInfo constructor in constructors)
{
Console.WriteLine(constructor.ToString());
}
}
}
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public Customer(int ID, string Name)
{
this.Id = ID;
this.Name = Name;
}
public Customer()
{
this.Id = -1;
this.Name = string.Empty;
}
public void PrintID()
{
Console.WriteLine("ID = {0}", this.Id);
}
public void PrintName()
{
Console.WriteLine("Name = {0}", this.Name);
}
}
}
Reflection is the ability of inspecting an assemblie's metadata at runtime.
It is used to find all types in an assembly and/or dynamically invoke methods in an assembly.
This includes information about the type, properties, methods, and events of an object.
With Reflection, we can dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and invoke its methods or access its fields and properties.There are several uses of reflection.
1. When you drag and drop a button on a win forms or an asp.net application. The properties window uses reflection to show all the properties of the Button class. So,reflection is extensivley used by IDE or a UI designers.
2. Late binding can be achieved by using reflection. You can use reflection to dynamically create an instance of a type, about which we don't have any information at compile time. So, reflection enables you to use code that is not available at compile time.
Example:
using System;
using System.Reflection;
namespace Sample
{
public class MainClass
{
private static void Main()
{
// Get the Type Using GetType() static method
Type T = Type.GetType("Sample.Customer");
// Print the Type details
Console.WriteLine("Full Name = {0}",T.FullName);
Console.WriteLine("Just the Class Name = {0}",T.Name);
Console.WriteLine("Just the Namespace = {0}", T.Namespace);
Console.WriteLine();
// Print the list of Methods
Console.WriteLine("Methods in Customer Class");
MethodInfo[] methods = T.GetMethods();
foreach (MethodInfo method in methods)
{
// Print the Return type and the name of the method
Console.WriteLine(method.ReturnType.Name + " " + method.Name);
}
Console.WriteLine();
// Print the Properties
Console.WriteLine("Properties in Customer Class");
PropertyInfo[] properties = T.GetProperties();
foreach (PropertyInfo property in properties)
{
// Print the property type and the name of the property
Console.WriteLine(property.PropertyType.Name + " " + property.Name);
}
Console.WriteLine();
// Print the Constructors
Console.WriteLine("Constructors in Customer Class");
ConstructorInfo[] constructors = T.GetConstructors();
foreach (ConstructorInfo constructor in constructors)
{
Console.WriteLine(constructor.ToString());
}
}
}
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public Customer(int ID, string Name)
{
this.Id = ID;
this.Name = Name;
}
public Customer()
{
this.Id = -1;
this.Name = string.Empty;
}
public void PrintID()
{
Console.WriteLine("ID = {0}", this.Id);
}
public void PrintName()
{
Console.WriteLine("Name = {0}", this.Name);
}
}
}
Comments
Post a Comment