📘 C# Blog Series – Blog 26 **“C# Reflection — Code को Runtime पर Inspect और Execute कैसे करते हैं?

📘 C# Blog Series – Blog 26

**“C# Reflection — Code को Runtime पर Inspect और Execute कैसे करते हैं?





(Real-Life Example: GPS Packet Handler Auto-Execution using Reflection)”**

C# Reflection वो feature है जिससे आपका code run होने के समय ही खुद को पढ़ सकता, समझ सकता और modify कर सकता है

Reflection =
“Code that reads code at runtime.”

GPS Server जैसे dynamic systems में Reflection बहुत काम आता है, जैसे:

  • Packet handler को automatically call करना

  • Unknown device models को detect करना

  • Commands को text नाम से invoke करना

  • Custom attributes पढ़ना

  • Dynamic object creation

  • Plugins load करना

आज Reflection को सबसे आसान तरीके + आपके GPS project के real examples से समझेंगे।


🟦 1. Reflection क्या करता है?

Reflection आपको देता है:

✔ किसी object की properties पढ़ना
✔ Class की methods list पढ़ना
✔ किसी method को नाम से execute करना
✔ Class को runtime पर create करना
✔ Custom attributes detect करना


🟢 2. Property Names सीखना (Reflection से)

Type t = typeof(GPSDevice); foreach (var p in t.GetProperties()) { Console.WriteLine(p.Name); }

Output:

IMEI Latitude Longitude Battery

आपने class की सभी properties dynamically पढ़ लीं।


3. Methods खोजने के लिए Reflection

var methods = typeof(G17).GetMethods(); foreach (var m in methods) { Console.WriteLine(m.Name); }

GPS device class में कौन-कौन से functions हैं—
अब code खुद पढ़ सकता है।


🚀 4. सबसे Powerful Feature — Method को उसके नाम से Call करना

Imagine:
आपके server को packet मिला:

LOCATION,IMEI=...

और आपके पास method है:

void HandleLocation(string pkt)

Reflection से आप इस method को नाम से चला सकते हो:

var method = this.GetType().GetMethod("HandleLocation"); method.Invoke(this, new object[] { packet });

Program खुद method ढूंढेगा और execute करेगा!


🌟 5. Real-Life GPS Example: Auto Packet Handler System

Suppose आप packet type निकालते हो:

ALARM → "HandleAlarm" GPS"HandleGPS" LOGIN → "HandleLogin"

Reflection से call:

string handlerName = "Handle" + packetType; var method = this.GetType().GetMethod(handlerName); if (method != null) method.Invoke(this, new object[] { packet });

अब नया packet type जोड़ना super easy:

बस नया method बना दो:

void HandleSOS(string pkt) { }

Server खुद call कर देगा—no switch-case needed!


🟠 6. Real-Life: Custom Attribute + Reflection = Killer Combination

आपने पिछले blog में custom attribute देखा।

Example:

[Packet("LOCATION")] void HandleLocation(string pkt) { } [Packet("SOS")] void HandleSOS(string pkt) { }

अब Reflection से हर method scan करो:

var methods = this.GetType().GetMethods(); foreach (var m in methods) { var attr = m.GetCustomAttribute<PacketAttribute>(); if (attr != null && attr.Type == packetType) { m.Invoke(this, new object[] { packet }); } }

अब आपका GPS packet architecture automatically scalable हो गया।
कोई switch, if, else नहीं।
Just methods + attributes.


🔵 7. Dynamic Object Creation (Activator)

Reflection से class का object runtime पर बना सकते हो:

var obj = Activator.CreateInstance(typeof(G17));

Useful for:

  • Plugin-based architecture

  • Multi-device classes

  • Dynamic modules


🟣 8. Auto JSON Mapping (Advanced Use)

Reflection से property names + values पढ़कर JSON manually बना सकते हो।

foreach (var p in obj.GetType().GetProperties()) { var val = p.GetValue(obj); }

EntityFramework, Newtonsoft — अंदर इसी का use करते हैं।


🧠 9. Performance Warning (Important)

Reflection powerful है पर:

⚠ थोड़ा slow होता है
⚠ High-frequency loops में मत use करो
✔ Use for config-level logic
✔ Use for flexible packet parsers
✔ Use for plugin architecture

GPS server में heavy packets पर reflection avoid करो,
lightweight mapping पर use कर सकते हो।


🔚 Conclusion

Reflection code को super dynamic बना देता है:

✔ Methods को नाम से execute करना
✔ Classes inspect करना
✔ Custom attribute handling
✔ Dynamic packet routing
✔ Plugin-style architecture

GPS server, Web APIs, Billing, Game engines —
हर professional backend में Reflection एक hidden weapon है।

Post a Comment

0 Comments

Translate

Close Menu