Getinstanceobjgettypehaswritablepropertypropertyname

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Property(object obj, string propertyName, object propertyValue) { ReflectionInfo reflectionCache = ReflectionInfo.GetInstance(obj.GetType()); try { if (propertyName.IndexOf("[") > -1) { SetArrayProperty(obj, propertyName, propertyValue); } else { if (obj is IDictionary) { ((IDictionary) obj)[propertyName] = propertyValue; } else { PropertyInfo propertyInfo = reflectionCache.GetSetter(propertyName); if (propertyInfo == null) { throw new ProbeException("No Set method for property " + propertyName + " on instance of " + obj.GetType().Name); } try { propertyInfo.SetValue(obj, propertyValue, null); } catch (ArgumentException ae) { throw new ProbeException(ae); } catch (TargetException t) { throw new ProbeException(t); } catch (TargetParameterCountException tp) { throw new ProbeException(tp); } catch (MethodAccessException ma) { throw new ProbeException(ma); } } } } catch (ProbeException pe) { throw pe; } catch (Exception e) { throw new ProbeException("Could not Get property '" + propertyName + "' for " + obj.GetType().Name + ". Cause: " + e.Message, e); } } /// <summary> /// Return the specified property on an object. /// </summary> /// <param name="obj">The Object on which to invoke the specified property.</param> /// <param name="propertyName">The name of the property.</param> /// <returns>An Object representing the return value of the invoked property.</returns> public static object GetPropertyValue(object obj, string propertyName) { if (propertyName.IndexOf('.') > -1) { StringTokenizer parser = new StringTokenizer(propertyName, "."); IEnumerator enumerator = parser.GetEnumerator(); object value = obj; string token = null; while (enumerator.MoveNext()) { token = (string)enumerator.Current; value = GetProperty(value, token); if (value == null) { break; } } return value; } else { return GetProperty(obj, propertyName); } } /// <summary> /// Set the specified property on an object /// </summary> /// <param name="obj">The Object on wh...
View Full Document

This document was uploaded on 10/12/2012.

Ask a homework question - tutors are online