Hi all,
i want to know detail about Reflection,its use in programming with simple example.
Thanks
Vikrant Jadhav
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Vikrant JadhavPosted Dec 22, 2011, 2:12 AM
Vikrant JadhavPosted Dec 22, 2011, 2:10 AM
Chintan RathodPosted Dec 22, 2011, 2:05 AM
Reflection is the process by which a computer program can observe and modify its own structure and behavior at runtime.
Reflection is most commonly used in high-level virtual machine programming languages like Smalltalk and scripting languages, and less commonly used in manifestly typed and/or statically typed programming languages such as Java, C, ML or Haskell.
In following examples, class is "Test" and it has simple method "hello" or "Hello".
Java Language Example
-----------------------------
// Without reflection
new Test().hello();
// With reflection
Class> cls = Class.forName("Test");
cls.getMethod("hello").invoke(cls.newInstance());
Visual Basic Example
---------------------------
'Without reflection
Dim test As New Test
test.Hello()
'Using variable syntax
Dim test As New Test
Microsoft.VisualBasic.CallByName(test, "Hello", CallType.Method)
Prasant JinagaPosted Dec 22, 2011, 1:53 AM
.NET Framework's Reflection API allows you to fetch type (assembly) 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.
Program that uses reflection in c#
using System;
using System.Reflection;
static class ReflectionTest
{
public static int Height;
public static int Width;
public static int Weight;
public static string Name;
public static void Write()
{
Type type = typeof(ReflectionTest);
// Get type pointer FieldInfo[] fields = type.GetFields();
// Obtain all fields foreach (var field in fields)
// Loop through fields
{
string name = field.Name;
// Get string name object temp = field.GetValue(null);
// Get value if (temp is int)
// See if it is an integer.
{
int value = (int)temp; Console.Write(name);
Console.Write(" (int) = ");
Console.WriteLine(value);
}
else if (temp is string)
// See if it is a string.
{
string value = temp as string;
Console.Write(name);
Console.Write(" (string) = ");
Console.WriteLine(value);
}
}
}
}
class Program
{ static void Main()
{ ReflectionTest.Height = 100;
// Set value ReflectionTest.Width = 50;
// Set value ReflectionTest.Weight = 300;
// Set value ReflectionTest.Name = "Perl";
// Set value ReflectionTest.Write();
// Invoke reflection methods
} }
OutputHeight (int) = 100
Width (int) = 50
Weight (int) = 300
Name (string) = Perl
This program defines two classes and two methods. The program begins in the Main entry point and the static fields Height, Width, Weight, and Name are assigned on the ReflectionTest static class. Next, the Write method is invoked, which internally calls the typeof operator, the GetFields parameterless instance method, and then accesses the Name and GetValue members on each individual FieldInfo.
Thanks,
Prasant