2012-03-01 22 views
5

Voglio passare attraverso tutte le proprietà di un tipo e voglio controllare se un tipo di proprietà non è una stringa, come posso farlo?Come verificare se un tipo è una stringa in C#?

La mia classe è:

public class MarkerInfo 
    { 
     public string Name { get; set; } 
     public byte[] Color { get; set; } 
     public TypeId Type { get; set; } 
     public bool IsGUIVisible { get; set; } 

     public MarkerInfo() 
     { 
      Color = new byte[4]; // A, R, G, B 
      IsGUIVisible = true; 
     } 
    } 

e il codice che sto usando per verificare la presenza di tipo è:

foreach (var property in typeof(MarkerInfo).GetProperties()) 
      {    

       if (property.PropertyType is typeof(string))    
      } 

Ma questo codice non funziona, qualsiasi idea di come farlo?

risposta

19
if (property.PropertyType == typeof(string)) 
2

uso == e non is o is String (lasciare il typeof)

2

Utilizza il seguente invece:

foreach (var property in typeof(MarkerInfo).GetProperties()) 
    {    
     if (property.PropertyType == typeof(string))    
    } 
Problemi correlati