Καλώς ορίσατε στο dotNETZone.gr - Σύνδεση | Εγγραφή | Βοήθεια
σε

 

Αρχική σελίδα Ιστολόγια Συζητήσεις Εκθέσεις Φωτογραφιών Αρχειοθήκες

Auto-set Property values

Îåêßíçóå áðü ôï ìÝëïò anjelinio. Τελευταία δημοσίευση από το μέλος anjelinio στις 04-10-2005, 13:52. Υπάρχουν 0 απαντήσεις.
Ταξινόμηση Δημοσιεύσεων: Προηγούμενο Επόμενο
  •  04-10-2005, 13:52 5875

    Auto-set Property values

     .. πολύ καιρό ζήλευα τα JavaBeans, και την αυτόματη δυνατότητά τους να κάνουν initialize τα properties τους απο το Request, οπότε .. μοιράζομαι μια μικρή utility κλασσούλα που έγραψα για να κάνω κάτι παρόμοιο ...

    /// <summary>

           /// TypeUtils provides various little utilities regarding properties, methods and

           /// other Reflection-oriented stuff ...

           /// </summary>

           public class TypeUtils

           {

                  /// <summary>

                  /// Sets the value of the specified property to a parsed instance

                  /// of the propertyValue string (if what I wrote makes any sense anyway ... )

                  /// </summary>

                  /// <param name="propertyName">The property name</param>

                  /// <param name="propertyValue">The string representation of the property value</param>

                  /// <param name="_instance">The instance we're setting the property of</param>

                  public static void SetPropertyValue(string propertyName, string propertyValue, object _instance){

                         Type currentType = _instance.GetType();

                         // Get the property off the reflected type ... and set the value

                         PropertyInfo property = currentType.GetProperty(propertyName);

                         // Get the type of object for this property & attempt to locate a 'Parse' method :?

                         Type propertyType = property.PropertyType;

                         // ok, check out if it's a string property first ...

                         if(propertyType.Equals(typeof(string))){

                               property.SetValue(_instance, propertyValue, null);

                               return;

                         }

                         // .. else ... parse it baby ;]

                         MethodInfo parseMethod = propertyType.GetMethod("Parse", new Type[]{typeof(string)});

                         // Now, attempt to invoke the method and get the result ...

                         if(parseMethod!=null){

                               try {

                                      object objTypedValue = parseMethod.Invoke(null, new object[]{propertyValue});

                                      property.SetValue(_instance, objTypedValue, null);

                               }catch{

                                      return;

                               }

                         }                   

                  }

     

                  /// <summary>

                  /// Sets all the properties of the specified instance to the ascociated values in the

                  /// NameValueCollection. It will actually set as many of the properties it acn locate in the

                  /// collection, without throwing an error if it can't locate one.

                  /// </summary>

                  /// <param name="parameters"></param>

                  /// <param name="instance"></param>

                  public static void SetProperties(NameValueCollection parameters, object instance){

                         Type currentType = instance.GetType();

                         PropertyInfo[] properties = currentType.GetProperties();

                         foreach(PropertyInfo property in properties){

                               string propertyName = property.Name;

                               string propertyValue = parameters[propertyName];

     

                               if(propertyValue!=null)

                                      SetPropertyValue(propertyName, propertyValue, instance);

                         }

                  }

           }

    Χρησιμοποιώντας τη SetProperties(Request.Params, this) θα κάνει happily initialize όλα τα properties της κλάσσης σας, με το σωστό Data Type :)

    Όπως πάντα, οποιοδήποτε σχόλιο είναι κάτι παραπάνω απο δεκτό !

    Πολύ καλή μέρα μας ...


    Angel
    O:]
Προβολή Τροφοδοσίας RSS με μορφή XML
Με χρήση του Community Server (Commercial Edition), από την Telligent Systems