Introduction
In this article, I have tried to explain the concept of Reflection in .NET. We shall also learn how to
get the type information using different ways. Use of properties and methods of
the
Type class in .NET
Reflection, with examples, are explained in this article.
What is .NET Reflection?
.NET Framework's Reflection API allows you to fetch assembly (type)
information at runtime programmatically. We can also achieve late binding by
using .NET Reflection. At runtime, the Reflection mechanism uses the PE file to
read information about the assembly. Reflection enables you to use code that is
not available at compile time. .NET Reflection allows an application to collect
information about itself and also to manipulate on itself. It can be used
effectively to find all types in an assembly and/or dynamically invoke methods
in an assembly. This includes information about the type, methods, properties, 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. We
can also access attribute information using Reflection.
Using Reflection, you can get any kind of information which you can see in a
class viewer; for example, information on the methods, properties, fields, and
events of an object.
The
The
System.Reflection
namespace and the System.Type
class plays a very important role in .NET Reflection. These two work together
and allow you to reflect over many other aspects of a type.
The System.Reflection Namespace
The
System.Reflection
namespace contains the classes and interfaces that provide a managed view of
loaded types, methods, and fields, with the ability to dynamically create and
invoke types; this process is known as Reflection in .NET framework. We will
take a look at some of the commonly used classed here:
Class
|
Description
|
Assembly |
Represents an assembly, which is a reusable, versionable,
and self-describing building block of a Common Language Runtime application.
This class contains a number of methods that allow you to load, investigate,
and manipulate an assembly.
|
Module |
Performs Reflection on a module. This class allows you to
access a given module within a multi-file assembly.
|
AssemblyName |
This class allows you to discover numerous details behind
an assembly's identity. An assembly's identity consists of the following:
|
EventInfo |
This class holds information for a given event. Use the
EventInfo class to inspect
events and to bind to event handlers. |
FieldInfo |
This class holds information for a given field. Fields are
variables defined in the class.
FieldInfo
provides access to the metadata for a field within a class, and provides
dynamic set and get functionality for the field. The class is not loaded into
memory until Invoke or get
is called on the object. |
MemberInfo |
The
MemberInfo
class is the abstract base class for classes used to obtain information about
all members of a class (constructors, events, fields, methods, and
properties). |
MethodInfo |
This class contains information for a given method.
|
ParameterInfo |
This class holds information for a given parameter.
|
PropertyInfo |
This class holds information for a given property.
|
Following example explain the concept of Reflection in .NET;
using System;
namespace Reflection
{
interface ICar
{
bool IsMoving();
}
}
using System;
namespace Reflection
{
internal class Car
{
//public
variables
public string Color;
//private
variables
//String
licensePlate; // e.g. "Lahore 888 999"
//double
maxSpeed; // in kilometers per hour
//int
startMiles; // Stating odometer reading
//int
endMiles; // Ending odometer reading
//double
gallons; // Gallons of gas used between the readings
//private
vaiables
private int _speed;
//Speed
- read-only property to return the speed
public int Speed
{
get { return _speed; }
}
//Accelerate
- add mph to the speed
public void Accelerate(int accelerateBy)
{
//Adjust
the speed
_speed += accelerateBy;
}
//IsMoving
- is the car moving?
public bool IsMoving()
{
//Is the
car's speed zero?
if (Speed == 0)
{
return false;
}
else
{
return true;
}
}
//Constructor
public Car()
{
//Set
the default values
Color = "Blue";
_speed = 0;
}
//Over
loaded constructor
public Car(string color, int speed)
{
Color = color;
_speed = speed;
}
//methods
public double calculateMPG(int startMiles, int endMiles, double gallons)
{
return (endMiles - startMiles) / gallons;
}
}
}
using System;
namespace Reflection
{
internal class SportsCar : Car
{
//Constructor
public SportsCar()
{
//Change
the default values
Color = "Red";
}
}
}
using System;
namespace Reflection
{
class Reflection
{
static void Main(string[] args)
{
Console.WriteLine("***********Output**********");
//
Obtain type information using the static Type.GetType() method.
//
(don't throw an exception if Car cannot be found and ignore case).
Type t = Type.GetType("Reflection.Car", false, true);
Console.WriteLine(t.FullName);
Console.ReadLine();
}
}
}
(Practice Makes a Man Perfect)
(Practice Makes a Man Perfect)