📘 C# Blog Series – Blog 25 **“C# Attributes — Classes और Methods पर Extra Metadata कैसे लगाते हैं?

📘 C# Blog Series – Blog 25

**“C# Attributes — Classes और Methods पर Extra Metadata कैसे लगाते हैं?





(Real-Life Example: API Models में JSON Property Names, Validation Attributes)”**

C# में Attributes वो फीचर है जो आपके code को
extra power, extra information और extra behaviour देता है —
बिना logic को बदले।

Attributes =
Code के ऊपर लगाया गया छोटा-सा tag
जो बताता है कि यह class/property/method का use कैसे होना चाहिए।

Real-life में attributes बहुत ज़रूरी हैं:

  • API models

  • JSON mapping

  • Validation rules

  • Database mapping (Entity Framework)

  • Logging

  • Reflection-based systems

  • GPS servers में packet mapping

आज attributes को सबसे आसान तरीके + real-life API & GPS examples से समझते हैं।


🟦 1. Attribute क्या है? (Simple Definition)

Attribute =
Code के बारे में extra जानकारी
जो runtime या compiler को बताती है कि इसे कैसे handle किया जाए।

Example:

[Obsolete]

इसका मतलब method पुरानी हो चुकी है।


🟢 2. Attribute कैसे लगाया जाता है?

[AttributeName] class MyClass { }

Attribute हमेशा [] brackets में होता है।


3. सबसे ज़्यादा इस्तेमाल होने वाले Attributes

[JsonPropertyName]
[JsonProperty] (Newtonsoft)
[Required]
[MaxLength]
[Table], [Key] (Entity Framework)
[Obsolete]
[Serializable]

इनसे आपका code ज्यादा smart बनता है।


🌟 4. Real-Life API Example: JSON Property Rename करना

Suppose API output में आपको:

{ "lat": 26.85, "lon": 80.94 }

नाम छोटा चाहिए लेकिन class में नाम meaningful चाहिए।

C# Model:

class GPSDevice { [JsonPropertyName("lat")] public double Latitude { get; set; } [JsonPropertyName("lon")] public double Longitude { get; set; } }

Output JSON:

{ "lat": 26.85, "lon": 80.94 }

Perfect clean mapping!


5. Newtonsoft JSON Attribute

अगर Newtonsoft use कर रहे हो:

[JsonProperty("lat")] public double Latitude { get; set; }

6. Real-Life GPS Example: Device Packet Model Mapping

GPS packet:

IMEI=861128068064267;BAT=80;SIG=4;

Class:

class GPSDeviceInfo { [JsonProperty("imei")] public string IMEI { get; set; } [JsonProperty("bat")] public int Battery { get; set; } [JsonProperty("sig")] public int Signal { get; set; } }

Now serialize → clean JSON mapping!


🟠 7. Validation Attributes — APIs में Most Important

Real-life APIs में user गलत data भेज सकता है।

Example Model:

class Student { [Required] public string Name { get; set; } [Range(1, 120)] public int Age { get; set; } [MaxLength(20)] public string City { get; set; } }

अगर गलत data भेजा →
API automatically error दे देगी।


🔵 8. Entity Framework Attributes — Database Mapping

class User { [Key] public int UserId { get; set; } [Required] [MaxLength(50)] public string Username { get; set; } [Table("UserInfo")] public class UserInfo { } }

Attributes से database tables linked हो जाते हैं।


🟣 9. [Obsolete] — पुरानी method पर Warning

[Obsolete("Use NewMethod instead")] void OldMethod() { }

अब developer जब OldMethod call करेगा →
warning आएगी।


📘 10. Custom Attribute (Advanced Level)

आप खुद भी attributes बना सकते हो:

[AttributeUsage(AttributeTargets.Method)] class LogAttribute : Attribute { public string Message { get; } public LogAttribute(string msg) { Message = msg; } }

Use:

[Log("This method does important work")] void Process() { }

GPS servers में custom attributes code को ज़्यादा structured बनाते हैं।


🌟 11. Real-Life GPS Example: Packet Handler Attribute (Advanced + Useful)

आप अलग-अलग packet types के लिए annotation लगा सकते हो:

[Packet("LOCATION")] void HandleLocation(string pkt) { } [Packet("ALARM")] void HandleAlarm(string pkt) { }

अब reflection से:

RunHandlerBasedOnPacketType("LOCATION");

Ultra-clean design!


🔚 Conclusion

C# Attributes code को बनाते हैं:

✔ Smart
✔ Clean
✔ Structured
✔ API-friendly
✔ Database-friendly
✔ Developer-friendly

GPS servers, APIs, billing software, ERPs —
हर जगह attributes powerful role निभाते हैं।

Post a Comment

0 Comments

Translate

Close Menu