Reflection is the ability of inspecting an assemblies' metadata at runtime
it is used to find all types in an assembly and/or dynamically invoke methods in an assembly
using System;
using System.Reflection;
namespace Reflection
{
public class Program
{
private static void Main(string[] args)
{
//Type T = Type.GetType("Reflection.Customer");
//Type T = typeof(Customer);
Customer C1 = new Customer();
Type T = C1.GetType();
Console.WriteLine("FUll Name = {0}", T.FullName);
Console.WriteLine("Just the Name = {0}", T.Name);
Console.WriteLine("Just the Namespace = {0}", T.Namespace);
Console.WriteLine();
Console.WriteLine("Properties in Customers");
PropertyInfo[] properties = T.GetProperties();
foreach (PropertyInfo property in properties)
{
Console.WriteLine(property.PropertyType.Name +" "+ property.Name);
}
Console.WriteLine();
Console.WriteLine("Methods in Customers");
MethodInfo[] methods = T.GetMethods();
foreach (MethodInfo method in methods)
{
Console.WriteLine(method.ReturnType.Name + " " + method.Name);
}
Console.WriteLine();
Console.WriteLine("Constructors in Customers");
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);
}
}
}