/// <summary> /// Checks to see if a Object has a writable property/field be a given name /// </summary> /// <param name="obj"> The object to check</param> /// <param name="propertyName">The property to check for</param> /// <returns>True if the property exists and is writable</returns> public static bool HasWritableProperty(object obj, string propertyName) { bool hasProperty = false; if (obj is IDictionary) { hasProperty = ((IDictionary)obj).Contains(propertyName); } else { if (propertyName.IndexOf('.') > -1) { StringTokenizer parser = new StringTokenizer(propertyName, "."); IEnumerator enumerator = parser.GetEnumerator(); Type type = obj.GetType(); while (enumerator.MoveNext()) { propertyName = (string)enumerator.Current; type = ReflectionInfo.GetInstance(type).GetGetterType(propertyName); hasProperty = ReflectionInfo.GetInstance(type).HasWritableMember(propertyName); } } else { hasProperty = ReflectionInfo.GetInstance(obj.GetType()).HasWritableMember(propertyName); } } return hasProperty; }
/// <summary> /// Sets the member value. /// </summary> /// <param name="obj">he Object on which to invoke the specified mmber.</param> /// <param name="memberName">Name of the member.</param> /// <param name="memberValue">The member value.</param> /// <param name="objectFactory">The object factory.</param> /// <param name="accessorFactory">The accessor factory.</param> public static void SetMemberValue(object obj, string memberName, object memberValue, IObjectFactory objectFactory, AccessorFactory accessorFactory) { if (memberName.IndexOf('.') > -1) { StringTokenizer parser = new StringTokenizer(memberName, "."); IEnumerator enumerator = parser.GetEnumerator(); enumerator.MoveNext(); string currentPropertyName = (string)enumerator.Current; object child = obj; while (enumerator.MoveNext()) { Type type = GetMemberTypeForSetter(child, currentPropertyName); object parent = child; child = GetMember(parent, currentPropertyName, accessorFactory); if (child == null) { try { IFactory factory = objectFactory.CreateFactory(type, Type.EmptyTypes); child = factory.CreateInstance(Type.EmptyTypes); SetMemberValue(parent, currentPropertyName, child, objectFactory, accessorFactory); } catch (Exception e) { throw new ProbeException("Cannot set value of property '" + memberName + "' because '" + currentPropertyName + "' is null and cannot be instantiated on instance of " + type.Name + ". Cause:" + e.Message, e); } } currentPropertyName = (string)enumerator.Current; } SetMember(child, currentPropertyName, memberValue, accessorFactory); } else { SetMember(obj, memberName, memberValue, accessorFactory); } }
/// <summary> /// Return the specified member on an object. /// </summary> /// <param name="obj">The Object on which to invoke the specified property.</param> /// <param name="memberName">Name of the member.</param> /// <param name="accessorFactory">The accessor factory.</param> /// <returns>An Object representing the return value of the invoked property.</returns> public static object GetMemberValue(object obj, string memberName, AccessorFactory accessorFactory) { if (memberName.IndexOf('.') > -1) { StringTokenizer parser = new StringTokenizer(memberName, "."); IEnumerator enumerator = parser.GetEnumerator(); object value = obj; string token = null; while (enumerator.MoveNext()) { token = (string)enumerator.Current; value = GetMember(value, token, accessorFactory); if (value == null) { break; } } return value; } else { return GetMember(obj, memberName, accessorFactory); } }
/// <summary> /// Returns the type that the set expects to receive as a parameter when /// setting a member value. /// </summary> /// <param name="type">The class type to check</param> /// <param name="memberName">The name of the member</param> /// <returns>The type of the member</returns> public static Type GetMemberTypeForSetter(Type type, string memberName) { Type memberType = type; if (memberName.IndexOf('.') > -1) { StringTokenizer parser = new StringTokenizer(memberName, "."); IEnumerator enumerator = parser.GetEnumerator(); while (enumerator.MoveNext()) { memberName = (string)enumerator.Current; memberType = ReflectionInfo.GetInstance(memberType).GetSetterType(memberName); } } else { memberType = ReflectionInfo.GetInstance(type).GetSetterType(memberName); } return memberType; }
/// <summary> /// Returns the type that the set expects to receive as a parameter when /// setting a member value. /// </summary> /// <param name="obj">The object to check</param> /// <param name="memberName">The name of the member</param> /// <returns>The type of the member</returns> public static Type GetMemberTypeForSetter(object obj, string memberName) { Type type = obj.GetType(); if (obj is IDictionary) { IDictionary map = (IDictionary)obj; object value = map[memberName]; if (value == null) { type = typeof(object); } else { type = value.GetType(); } } else { if (memberName.IndexOf('.') > -1) { StringTokenizer parser = new StringTokenizer(memberName, "."); IEnumerator enumerator = parser.GetEnumerator(); while (enumerator.MoveNext()) { memberName = (string)enumerator.Current; type = ReflectionInfo.GetInstance(type).GetSetterType(memberName); } } else { type = ReflectionInfo.GetInstance(type).GetSetterType(memberName); } } return type; }
/// <summary> /// Returns the MemberInfo of the set member on the specified type. /// </summary> /// <param name="type">The type to check</param> /// <param name="memberName">The name of the member</param> /// <returns>The type of the member</returns> public static MemberInfo GetMemberInfoForSetter(Type type, string memberName) { MemberInfo memberInfo = null; if (memberName.IndexOf('.') > -1) { StringTokenizer parser = new StringTokenizer(memberName, "."); IEnumerator enumerator = parser.GetEnumerator(); Type parentType = null; while (enumerator.MoveNext()) { memberName = (string)enumerator.Current; parentType = type; type = ReflectionInfo.GetInstance(type).GetSetterType(memberName); } memberInfo = ReflectionInfo.GetInstance(parentType).GetSetterMethod(memberName); } else { memberInfo = ReflectionInfo.GetInstance(type).GetSetterMethod(memberName); } return memberInfo; }
public StringTokenizerEnumerator(StringTokenizer stok) { _stokenizer = stok; }