/// <summary> /// Create a new factory instance for a given type /// </summary> /// <param name="typeToCreate">The type instance to build</param> /// <param name="types">The types of the constructor arguments</param> /// <returns>Returns a new instance factory</returns> public IFactory CreateFactory(Type typeToCreate, Type[] types) { if (_logger.IsDebugEnabled) { return(new FactoryLogAdapter(typeToCreate, types, _objectFactory.CreateFactory(typeToCreate, types))); } return(_objectFactory.CreateFactory(typeToCreate, types)); }
public static void SetMemberValue(object obj, string memberName, object memberValue, IObjectFactory objectFactory, AccessorFactory accessorFactory) { if (memberName.IndexOf('.') > -1) { IEnumerator enumerator = new StringTokenizer(memberName, ".").GetEnumerator(); enumerator.MoveNext(); string current = (string)enumerator.Current; object obj2 = obj; while (enumerator.MoveNext()) { Type memberTypeForSetter = GetMemberTypeForSetter(obj2, current); object obj3 = obj2; obj2 = GetMember(obj3, current, accessorFactory); if (obj2 == null) { try { obj2 = objectFactory.CreateFactory(memberTypeForSetter, Type.EmptyTypes).CreateInstance(Type.EmptyTypes); SetMemberValue(obj3, current, obj2, objectFactory, accessorFactory); } catch (Exception exception) { throw new ProbeException("Cannot set value of property '" + memberName + "' because '" + current + "' is null and cannot be instantiated on instance of " + memberTypeForSetter.Name + ". Cause:" + exception.Message, exception); } } current = (string)enumerator.Current; } SetMember(obj2, current, memberValue, accessorFactory); } else { SetMember(obj, memberName, memberValue, accessorFactory); } }
/// <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> /// Create a new factory instance for a given type /// </summary> /// <param name="typeToCreate">The type instance to build</param> /// <param name="types">The types of the constructor arguments</param> /// <returns>Returns a new instance factory</returns> public IFactory CreateFactory(Type typeToCreate, Type[] types) { { return(_objectFactory.CreateFactory(typeToCreate, types)); } }