2010-03-02 11 views
5

मैं प्रतिबिंब का उपयोग करने की कोशिश कर रहा हूं यह जांचने के लिए कि किसी दिए गए वर्ग के गुणों को केवल पढ़ने योग्य विशेषता सेट है या नहीं। कक्षाएं मैं उपयोग कर रहा हूँ मेटाडाटा के लिए एक आंशिक "साथी" वर्ग का उपयोग कर MVC देखें मॉडल हैं (।सी # बडी क्लासेस/मेटा डेटा और प्रतिबिंब

public partial class AccountViewModel 
{ 
    public virtual Int32 ID { get; set; } 
    public virtual decimal Balance { get; set; }  

} 
[MetadataType(typeof(AccountViewModelMetaData))] 
public partial class AccountViewModel 
{ 
    class AccountViewModelMetaData 
    { 
     [DisplayName("ID")] 
     public virtual Int32 ID { get; set; } 

     [DisplayName("Balance")] 
     [DataType(DataType.Currency)] 
     [ReadOnly(true)] 
     public virtual decimal Balance { get; set; } 

    } 
} 

मैं अगर "शेष" ReadOnly संपत्ति की जांच करना चाहते हैं। अगर मैं संतुलन पर ReadOnly विशेषता निर्धारित AccountViewModel की संपत्ति, मैं इसे इस तरह से प्राप्त कर सकते हैं:।?

Type t = typeof(AccountViewModel); 
PropertyInfo pi = t.GetProperty("Balance"); 
bool isReadOnly = ReadOnlyAttribute.IsDefined(pi,typeof(ReadOnlyAttribute); 

मैं विशेषता की जानकारी अगर यह मेटा डेटा वर्ग पर है नहीं प्राप्त कर सकते हैं मैं कैसे जांच कर सकते हैं विशेषता मौजूद है मैं मेटा डेटा वर्गों को परिभाषित किया है मेरे सभी दृश्य मॉडल के लिए, और मेटा डेटा कक्षाओं पर विशेषताओं की जांच करने के लिए एक सामान्य तरीका की आवश्यकता है।

कोई सुझाव?

उत्तर

1

एक छोटा लेकिन काम करने वाला उदाहरण निम्नानुसार है, ध्यान दें कि मैंने बाहर के दृश्यमान होने के लिए नेस्टेड क्लास internal बनाया है।

public partial class AccountViewModel 
{ 
    internal class AccountViewModelMetaData 
    { 
     public virtual Int32 ID { get; set; } 
     [ReadOnlyAttribute(false)] 
     public virtual decimal Balance { get; set; } 
    } 

    public virtual Int32 ID { get; set; } 
    public virtual decimal Balance { get; set; } 
} 
class Program 
{ 
    public static void Main(string[] args) 
    { 
     Type metaClass = typeof(AccountViewModel.AccountViewModelMetaData); 

     bool hasReadOnlyAtt = HasReadOnlyAttribute(metaClass, "Balance"); 

     Console.WriteLine(hasReadOnlyAtt); 
    } 

    private static bool HasReadOnlyAttribute(Type type, string property) 
    { 
     PropertyInfo pi = type.GetProperty(property); 

     return ReadOnlyAttribute.IsDefined(pi, typeof(ReadOnlyAttribute)); 
    } 
} 

यह भी ध्यान रखें कि यह विशेषता के अस्तित्व के लिए जांच करता है। आप इस उदाहरण में विशेषता निर्दिष्ट कर सकते हैं लेकिन false का मान प्रदान कर सकते हैं। अगर आप यह जांचना चाहते हैं कि संपत्ति केवल पढ़ने के लिए है या नहीं, तो आप केवल ReadOnlyAttribute.IsDefined विधि का उपयोग नहीं कर सकते हैं।

7

समाधान GetCustomAttributes का उपयोग करने के लिए मेटाडेटा प्रकार प्राप्त करने के लिए और साथ ही उन पर गुण जाँच ...

Type t = typeof(MyClass); 
PropertyInfo pi = t.GetProperty(PropertyName); 
bool isReadOnly = ReadOnlyAttribute.IsDefined(pi, typeof(ReadOnlyAttribute)); 

if (!isReadOnly) 
{ 
    //check for meta data class. 
    MetadataTypeAttribute[] metaAttr = (MetadataTypeAttribute[])t.GetCustomAttributes(typeof(MetadataTypeAttribute),true); 

    if (metaAttr.Length > 0) 
    { 
     foreach (MetadataTypeAttribute attr in metaAttr) 
     { 
      t = attr.MetadataClassType; 
      pi = t.GetProperty(PropertyName); 

      if (pi != null) isReadOnly = ReadOnlyAttribute.IsDefined(pi, typeof(ReadOnlyAttribute)); 

      if (isReadOnly) break; 
     } 
    } 
} 
+1

खबरदार आप जाँच कर रहे हैं कि उस गुण मौजूद है और नहीं है कि यह परिभाषित किया गया है है 'सच है '। क्या आपने सोचा है कि क्या होता है यदि संपत्ति परिभाषित की गई है '[ReadOnlyAttribute (false)] '? –

+1

क्षमा करें, लेकिन आपके समाधान को समझ नहीं सका। :( – Rohit

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