2010-03-18 19 views
5

मैंने एक विशेषता बनाई है, 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"); 
     }; 
    } 
} 

उत्तर

1

वास्तव में यह विफल रहता है, लेकिन केवल तभी जब आप विशेषता गुण प्राप्त करने का प्रयास कर रहे हैं। यहां एक उदाहरण दिया गया है जो विफल रहता है:

using System; 

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")); 

     Func<string, string> action1 = SayHello1; 
     Func<string, string> action2 = SayHello2; 

     MyAttribute myAttribute1 = (MyAttribute)Attribute.GetCustomAttribute(action1.Method, typeof(MyAttribute)); 
     MyAttribute myAttribute2 = (MyAttribute)Attribute.GetCustomAttribute(action2.Method, typeof(MyAttribute)); 

     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 string MyProperty 
    { 
     get; set; 
    } 

    public string MyProperty2 
    { 
     get; 
     set; 
    } 

    public MyAttribute(string VRegKey) 
    { 
     MyProperty = VRegKey; 
     if (VRegKey == "hello") 
     { 
      Console.WriteLine("Aha! You're Registered"); 
     } 
     else 
     { 
      throw new Exception("Oho! You're not Registered"); 
     }; 

     MyProperty2 = VRegKey; 
    } 
} 
+0

तो अब आप अपना कोड अपवाद फेंकने के लिए प्राप्त कर सकते हैं। लेकिन क्या यह आपको विधि को कॉल करने से रोकता है? –

+0

मैं मानता हूं कि गुणों में कोई व्यवहार करना गलत है। लेकिन सवाल यह था कि उपर्युक्त कोड में अपवाद क्यों नहीं होता है और उत्तर है - क्योंकि जब आप इसे एक्सेस करने का प्रयास करते हैं तो विशेषता वर्ग का उदाहरण बनाया जाता है। –

8

गुण संकलन समय पर लागू होते हैं, और कंस्ट्रक्टर गुण को भरने के लिए ही प्रयोग किया जाता। गुण मेटाडेटा हैं, और केवल रनटाइम पर ही जांच की जा सकती है।

वास्तव में, गुणों में कोई व्यवहार नहीं होना चाहिए।

+0

यदि ऐसा है, तो किसी विधि की सुरक्षा कैसे सेट कर सकते हैं? – Coppermill

+0

यह एक बिल्कुल अलग विषय है, लेकिन आप कोड एक्सेस सुरक्षा पर एक नज़र डालना चाहेंगे। –

+0

मैं सीएएस की सिफारिश नहीं करता क्योंकि यह सही होने के लिए बहुत जटिल है और नेट 4.0 में बहिष्कृत किया गया है। – adrianbanks

संबंधित मुद्दे