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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Prasant JinagaRank #2,383 · 594 reputationPosted Dec 21, 2011, 8:53 PM EST
.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
Chintan RathodRank #516 · 4.8K reputationPosted Dec 21, 2011, 9:05 PM EST
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)
Vikrant Jadhav64 reputationPosted Dec 21, 2011, 9:10 PM EST
Vikrant Jadhav64 reputationPosted Dec 21, 2011, 9:12 PM EST