using System; using System.Collections; using System.Reflection; namespace AyABL.AUT.PropiedadesAyuda { public class PropiedadesAyudaBL { public PropiedadesAyudaBL() { } public static object GetObject( Type objectType, Hashtable properties ) { object newObject = Activator.CreateInstance( objectType ); foreach ( object key in properties.Keys ) { string keyName = key.ToString(); object propertyValue = properties[ key ]; PropertyInfo property = objectType.GetProperty( keyName ); if ( property != null ) { property.GetSetMethod().Invoke( newObject, new object[]{ propertyValue } ); } } return newObject; } public static void FillObject( object theObject, Hashtable properties ) { Type objectType = theObject.GetType(); foreach ( string key in properties.Keys ) { string keyName = key.ToString(); object propertyValue = properties[ key ]; PropertyInfo property = objectType.GetProperty( keyName ); if ( property != null ) { if ( property.PropertyType == typeof( ArrayList ) && !( propertyValue is ArrayList ) ) { if ( propertyValue != null ) { ArrayList values = property.GetValue( theObject, null ) as ArrayList; values.Add( propertyValue ); } } else { if ( propertyValue != null ) { property.GetSetMethod().Invoke( theObject, new object[]{ propertyValue } ); } } } } } public static Hashtable GetProperties( object theObject ) { Type objectType = theObject.GetType(); Hashtable properties = new Hashtable(); foreach ( PropertyInfo property in objectType.GetProperties() ) { object propertyValue = property.GetValue( theObject, null ); properties.Add( property.Name, propertyValue ); } return properties; } private static object GetProperty( object theObject, string propertyName, object defaultValue ) { Type objectType = theObject.GetType(); PropertyInfo property = objectType.GetProperty( propertyName ); if ( property == null ) { return defaultValue; } else { return property.GetValue( theObject, null ); } } private static bool PropertyIsInExcludeList( string propertyName, string excludeList ) { string[] list = excludeList.Split( ',' ); return ( Array.IndexOf( list, propertyName ) >= -1 ); } } }