(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 से समझेंगे।
Reflection आपको देता है:
✔ किसी object की properties पढ़ना
✔ Class की methods list पढ़ना
✔ किसी method को नाम से execute करना
✔ Class को runtime पर create करना
✔ Custom attributes detect करना
Type t = typeof(GPSDevice);
foreach (var p in t.GetProperties())
{
Console.WriteLine(p.Name);
}
Output:
IMEI
Latitude
Longitude
Battery
आपने class की सभी properties dynamically पढ़ लीं।
var methods = typeof(G17).GetMethods();
foreach (var m in methods)
{
Console.WriteLine(m.Name);
}
GPS device class में कौन-कौन से functions हैं—
अब code खुद पढ़ सकता है।
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 करेगा!
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!
आपने पिछले 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.
Reflection से class का object runtime पर बना सकते हो:
var obj = Activator.CreateInstance(typeof(G17));
Useful for:
Plugin-based architecture
Multi-device classes
Dynamic modules
Reflection से property names + values पढ़कर JSON manually बना सकते हो।
foreach (var p in obj.GetType().GetProperties())
{
var val = p.GetValue(obj);
}
EntityFramework, Newtonsoft — अंदर इसी का use करते हैं।
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 कर सकते हो।
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 है।
0 Comments
Thanks for Commenting on our blogs, we will revert back with answer of your query.
EmojiThanks & Regards
Sonu Yadav