C# Reflection

Introduction to C# Reflection

C# Reflection API आपको runtime के दौरान program elements को inspect और manipulate करने की ability provide करती है।

Reflection के द्वारा आप किसी assembly के बारे में सम्पूर्ण जानकारी प्राप्त कर सकते है। C# में assembly वह file होती है जो आपके project या application को represent करती है। यह file compiler द्वारा successful compilation पर automatically generate की जाती है।

Assembly file में ही आपके program के सभी Modules, namespaces, classes, methods आदि elements होते है। Reflection API आपको किसी assembly के metadata का access provide करती है। Reflection के द्वारा नयी assembly create और compile भी की जा सकती है।

Assembly file में available दूसरे program elements जैसे की class, interface, struct, delegate और methods आदि के बारे में भी Reflection द्वारा जानकारी प्राप्त की जा सकती है।

Reflection उन situations में महत्वपूर्ण है जब आप किसी ऐसी application के बारे में जानकारी प्राप्त करना चाहते हो जो आपने create नहीं की है या जिसके elements के बारे में आपको कोई भी जानकारी नहीं है। उदाहरण के लिए आप किसी दूसरे programmer द्वारा create की गयी किसी application को reflection द्वारा inspect कर सकते है।

System.Reflection

C# में reflection API System.Reflection namespace द्वारा represent की गयी है। इस namespace में ऐसी बहुत सी classes, methods और properties available है जिनके द्वारा आप किसी type के members को enumerate कर सकते है, नए objects instantiate कर सकते है, किसी class पर apply किये गए custom attributes को inspect कर सकते है और किसी object के members को भी execute कर सकते है।

System.Reflection namespace में available कुछ महत्वपूर्ण classes के बारे में निचे detail से बताया जा रहा है।

Assembly

यह class programmers द्वारा assemblies को load और explore करने के लिए उपयोग की जाती है। इस class का object create करके आप assembly में available program elements को देख सकते है।

किसी assembly को load करने के लिए इस class में Load() method provide किया गया है। इस method में assembly file का नाम pass किया जाता है। इस class के LoadFile() और LoadFrom() methods के द्वारा भी किसी assembly को load किया जा सकता है।

Assembly class का getTypes() method किसी assembly में available सभी types (class, struct, enum और delegate) आदि की list return करता है। किसी particular type को search करने के लिए getType() method use किया जा सकता है।

Module

एक Module portable executable file होती है। ये file .dll या exe extension के द्वारा defined होती है। एक module में कई namespaces हो सकते है। किसी assembly file में कई modules हो सकते है जिनमे अलग अलग namespaces हो सकते है।

Module class के getTypes(), getMethods() और getFields() आदि methods का प्रयोग करके आप Module में available types, method और fields के बारे में जानकारी extract कर सकते है।

MethodInfo

यह class किसी type के method को represent करती है। किसी object के methods की जानकारी प्राप्त करने के लिए आप MethodInfo class का object create करके available methods को use कर सकते है।

Methods के parameters और return type की information के लिए आप GetParameters() method और ReturnParameter, ReturnType आदि properties को उपयोग कर सकते है।

ConstructorInfo

यह class किसी constructor के attributes को discover करने या constructors को invoke करने के लिए प्रयोग की जाती है। Class में available constructors की list प्राप्त करने के लिए getConstructors() या getConstructor() method का प्रयोग किया जाता है।

MemberInfo

यह class किसी member के attributes और metadata के बारे में जानकारी access करने के लिए उपयोग की जाती है। यह एक abstract class होती है और दूसरी classes द्वारा class के सभी members (constructors, events, fields, methods, properties आदि) के बारे में जानकारी access करने के लिए use की जाती है।

इस class का object create करके आप सभी members के बारे में information extract कर सकते है।

ParameterInfo

किसी parameter का data type, default value आदि जानकारी प्राप्त करने के लिए आप ParameterInfo class का object use कर सकते है।

इस class का GetParameters() method किसी method के सभी parameters की list array के रूप में return करता है।

Type

Type class System.Reflection namespace में एक main functionality है। यह class meta data को access करने का primary way है। इस class को आप किसी type के members (constructors, methods, properties आदि) के type declaration के बारे में जानकरी प्राप्त करने के लिए प्रयोग कर सकते है।

Type एक abstract class है। इसलिए इसमें available methods को use करने के लिए आपको इसे inherit करने की आवश्यकता होती है। यह class निचे दिए जा रहे methods provide करती है।

  • GetConstructors()
  • GetProperties()
  • GetMethods()
  • GetEvents()
  • GetInterfaces()
  • GetMembers()
  • GetEvents()

FieldInfo

यह class field के attributes और meta data का access provide करती है। इस class का कोई public constructor नहीं होता है। इस class का object प्राप्त करने के लिए Type object पर getFields() या getField() method call किया जाता है।

Fields class में define किये गए variables होते है। FieldInfo class fields के बारे में metadata provide करने के साथ साथ fields के लिए dynamic set और get functionality provide करती है।

EventInfo

यह class event attributes और event metadata का access provide करती है। इस class को events को inspect करने और event handlers से जोड़ने के लिए use किया जा सकता है।

EventInfo class विभिन्न properties और methods provide करती है जिनकी मदद से आप events के बारे में सम्पूर्ण जानकारी प्राप्त कर सकते है। लेकिन एक बात आपको ध्यान रखनी चाहिए की इस class द्वारा आप events नहीं raise कर सकते है।

PropertyInfo

यह class property attributes और properties का metadata access करने के लिए प्रयोग की जाती है। यह एक abstract class है। जब आप इसे inherit करते है तो इसके GetValue(), SetValue(), GetAccessors() आदि methods को override करना आवश्यक होता है।

Example of C# Reflection

C# में reflection को निचे उदाहरण द्वारा समझाया जा रहा है।

using System;
using System.Reflection;

class myClass
{
    public string FirstName
    {
          get; set;
    }

    public string LastName
    {
          get; set;
    }

   static void Main(string[] args)
   {
         Type type = typeof(myClass);
         PropertyInfo[] propinfo = type.GetProperties();

         foreach(var item in propinfo)
         {
              Console.WriteLine(item);
         }
   }
}

ऊपर दिया गया उदाहरण निचे दिया गया output generate करता है।

System.String FirstName
System.String LastName