मैंने एक विशेषता बनाई है, MyAttribute को कॉल किया है, जो कुछ सुरक्षा कर रहा है और किसी कारण से कन्स्ट्रक्टर को निकाल दिया नहीं जा रहा है, किसी भी कारण से?विशेषता वर्ग कन्स्ट्रक्टर को कॉल नहीं कर रहा है
public class Driver
{
// Entry point of the program
public static void Main(string[] Args)
{
Console.WriteLine(SayHello1("Hello to Me 1"));
Console.WriteLine(SayHello2("Hello to Me 2"));
Console.ReadLine();
}
[MyAttribute("hello")]
public static string SayHello1(string str)
{
return str;
}
[MyAttribute("Wrong Key, should fail")]
public static string SayHello2(string str)
{
return str;
}
}
[AttributeUsage(AttributeTargets.Method)]
public class MyAttribute : Attribute
{
public MyAttribute(string VRegKey)
{
if (VRegKey == "hello")
{
Console.WriteLine("Aha! You're Registered");
}
else
{
throw new Exception("Oho! You're not Registered");
};
}
}
तो अब आप अपना कोड अपवाद फेंकने के लिए प्राप्त कर सकते हैं। लेकिन क्या यह आपको विधि को कॉल करने से रोकता है? –
मैं मानता हूं कि गुणों में कोई व्यवहार करना गलत है। लेकिन सवाल यह था कि उपर्युक्त कोड में अपवाद क्यों नहीं होता है और उत्तर है - क्योंकि जब आप इसे एक्सेस करने का प्रयास करते हैं तो विशेषता वर्ग का उदाहरण बनाया जाता है। –