Introduction of Reflection in .NET

have stated with the definition of .NET Reflection and its road map a list of mostly used classes the System.Reflection namespace provides and the importance of the Type class in .NET Reflection.
  • 2285

Introduction

I have stated with the definition of .NET Reflection and its road map a list of mostly used classes the System.Reflection namespace provides and the importance of the Type class in .NET Reflection. .NET Framework 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, 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. We can also access attribute information using Reflection.

.NET there is a powerful mechanism called .NET Reflection that not only allows you to introspect types but also raise methods on those types during runtime. Though the process of retrieving types information in .NET Reflection is slow compared to direct access of a method, property, or field, .NET Reflection provides dynamic execution of code and controls when used sparingly. All types with their methods are stored in assemblies.

Example : This is a simple demo how to used reflection in .NET.

Code

using System;

using System.Collections.Generic;

using System.Text;

using System.Reflection; 
 

namespace ReflectionTest

{

    class Program

    {

       private static int a = 5, b = 10, c = 20;

        static void Main(string[] args)

        {

            Console.WriteLine("a + b + c = " + (a + b + c));

            Console.WriteLine("Please enter the name of the variable that you wish to change:");

            string varName = Console.ReadLine();

            Type t = typeof(Program);

            FieldInfo fieldInfo = t.GetField(varName, BindingFlags.NonPublic | BindingFlags.Static);

            if (fieldInfo != null)

            {

                Console.WriteLine("The current value of " + fieldInfo.Name + " is " + fieldInfo.GetValue(null) + ". You may enter a new value now:");

          string newValue = Console.ReadLine();

          int newInt;

          if (int.TryParse(newValue, out newInt))

          {

            fieldInfo.SetValue(null, newInt);

            Console.WriteLine("a + b + c = " + (a + b + c));

             }

            Console.ReadKey();

            }

        }

    }

}

© 2019 DotNetHeaven. All rights reserved.