2009-10-08 10 views
9

मेरे पास निम्न कोड है:कैसे बताएं कि कोई PropertyInfo किसी विशेष enum प्रकार का है या नहीं?

public class DataReader<T> where T : class 
{ 
    public T getEntityFromReader(IDataReader reader, IDictionary<string, string> FieldMappings) 
    { 
     T entity = Activator.CreateInstance<T>(); 
     Type entityType = entity.GetType(); 
     PropertyInfo[] pi = entityType.GetProperties(); 
     string FieldName; 

     while (reader.Read()) 
     { 
      for (int t = 0; t < reader.FieldCount; t++) 
      { 
       foreach (PropertyInfo property in pi) 
       { 
        FieldMappings.TryGetValue(property.Name, out FieldName); 

        Type genericType = property.PropertyType; 

        if (!String.IsNullOrEmpty(FieldName)) 
         property.SetValue(entity, reader[FieldName], null); 
       } 
      } 
     } 

     return entity; 
    } 
} 

जब मैं Enum प्रकार के क्षेत्र में जाता हूं, या इस मामले में NameSpace.MyEnum, मैं कुछ विशेष करना चाहता हूं। मैं बस SetValue नहीं कर सकता क्योंकि डेटाबेस से आने वाला मान आइए "एम" कहें और Enum में मान "श्री" है। तो मुझे एक और विधि कॉल करने की जरूरत है। मुझे पता है! विरासत प्रणाली सही है?

तो मैं कैसे निर्धारित करूं कि PropertyInfo आइटम किसी विशेष गणना प्रकार का होता है?

तो उपरोक्त कोड में मैं पहले यह जांचना चाहता हूं कि PropertyInfo प्रकार एक विशिष्ट enum का है या नहीं और फिर यह मेरी विधि को कॉल करता है और यदि नहीं तो बस SetValue को चलाने की अनुमति दें।

+1

बजाय है के साथ उपयोग करते हैं, बस अपने जेनेरिक में "नई" बाधा जोड़ें: "जहां टी: कक्षा, नया()"। फिर बस "टी इकाई = नया टी()" का प्रयोग करें। इस तरह, आप संकलन समय पर पैरामीटर रहित कन्स्ट्रक्टर की आवश्यकता को लागू कर सकते हैं। – Brannon

+0

@ ब्रैनन, धन्यवाद कि यह एक महान टिप है। जब मैं काम में जाऊंगा तो करूँगा। धन्यवाद। – griegs

उत्तर

2
static void DoWork() 
{ 
    var myclass = typeof(MyClass); 
    var pi = myclass.GetProperty("Enum"); 
    var type = pi.PropertyType; 

    /* as itowlson points out you could just do ... 
     var isMyEnum = type == typeof(MyEnum) 
     ... becasue Enums can not be inherited 
    */ 
    var isMyEnum = type.IsAssignableFrom(typeof(MyEnum)); // true 
} 
public enum MyEnum { A, B, C, D } 
public class MyClass 
{ 
    public MyEnum Enum { get; set; } 
} 
+2

यह सिर्फ प्रकार == टाइपऑफ (MyEnum) परीक्षण करने के लिए स्पष्ट हो सकता है। IsAssignable कोई भी मूल्य नहीं जोड़ता है क्योंकि आपके पास MyEnum से प्राप्त होने वाला कोई अन्य प्रकार नहीं हो सकता है। – itowlson

+0

धन्यवाद @ मैथ्यू। एक खरीदा एक की तरह काम करता है। – griegs

3

अपने उपरोक्त कोड में,

bool isEnum = typeof(Enum).IsAssignableFrom(typeof(genericType)); 

या नहीं, वर्तमान प्रकार (से प्राप्त) है आप मिल जाएगा एक enum या नहीं।

+0

+1 आपके योगदान के लिए धन्यवाद @adrianbanks। – griegs

20

यहाँ है कि मैं क्या सफलता

property.PropertyType.IsEnum 
0

यह मैं कैसे संभाल जब मैं एक जोरदार टाइप किया सूची में एक डेटा तालिका में कनवर्ट Activator.CreateInstance () का उपयोग करने का

/// <summary> 
     /// Covert a data table to an entity wiht properties name same as the repective column name 
     /// </summary> 
     /// <typeparam name="T"></typeparam> 
     /// <param name="dt"></param> 
     /// <returns></returns> 
     public static List<T> ConvertDataTable<T>(this DataTable dt) 
     { 
      List<T> models = new List<T>(); 
      foreach (DataRow dr in dt.Rows) 
      { 
       T model = (T)Activator.CreateInstance(typeof(T)); 
       PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T)); 

       foreach (PropertyDescriptor prop in properties) 
       { 
        //get the property information based on the type 
        System.Reflection.PropertyInfo propertyInfo = model.GetType().GetProperties().Last(p => p.Name == prop.Name); 

        var ca = propertyInfo.GetCustomAttribute<PropertyDbParameterAttribute>(inherit: false); 
        string PropertyName = string.Empty; 
        if (ca != null && !String.IsNullOrWhiteSpace(ca.name) && dt.Columns.Contains(ca.name)) //Here giving more priority to explicit value 
         PropertyName = ca.name; 
        else if (dt.Columns.Contains(prop.Name)) 
         PropertyName = prop.Name; 

        if (!String.IsNullOrWhiteSpace(PropertyName)) 
        { 
         //Convert.ChangeType does not handle conversion to nullable types 
         //if the property type is nullable, we need to get the underlying type of the property 
         var targetType = IsNullableType(propertyInfo.PropertyType) ? Nullable.GetUnderlyingType(propertyInfo.PropertyType) : propertyInfo.PropertyType; 
         // var propertyVal = Convert.ChangeType(dr[prop.Name], targetType); 
         //Set the value of the property 
         try 
         { 
          if (propertyInfo.PropertyType.IsEnum) 
           prop.SetValue(model, dr[PropertyName] is DBNull ? (object)null : Enum.Parse(targetType, Convert.ToString(dr[PropertyName]))); 
          else 
           prop.SetValue(model, dr[PropertyName] is DBNull ? (object)null : Convert.ChangeType(dr[PropertyName], targetType)); 
         } 
         catch (Exception ex) 
         { 
          //Logging.CustomLogging(loggingAreasType: LoggingAreasType.Class, loggingType: LoggingType.Error, className: CurrentClassName, methodName: MethodBase.GetCurrentMethod().Name, stackTrace: "There's some problem in converting model property name: " + PropertyName + ", model property type: " + targetType.ToString() + ", data row value: " + (dr[PropertyName] is DBNull ? string.Empty : Convert.ToString(dr[PropertyName])) + " | " + ex.StackTrace); 
          throw; 
         } 
        } 
       } 
       models.Add(model); 
      } 
      return models; 
     } 
संबंधित मुद्दे