Example #1
0
        public void Link(IRepositoryManager repositoryManager, TSource @object)
        {
            var destRepo         = repositoryManager.GetCommonRepository <TDestination>();
            var idValue          = _fkIdAccessor.GetValue(@object);
            var navPropertyValue = _navPropertyAccessor.GetValue(@object);
            var idHasValue       = idValue != null && idValue != 0;

            if (!idHasValue && navPropertyValue != null)
            {
                _fkIdAccessor.SetValue(@object, navPropertyValue.Id);
            }
            else if (idHasValue)
            {
                navPropertyValue = destRepo.FindById(idValue.Value);
                _navPropertyAccessor.SetValue(@object, navPropertyValue);
            }
            if (navPropertyValue != null && _reverseAccessor != null)
            {
                var collection = GetReverseCollection(navPropertyValue);
                if (CanAddToCollectionPredicate(collection)(@object))
                {
                    collection.Add(@object);
                }
            }
        }
Example #2
0
        private void CloneGenericCollection(IMemberAccessor accessor, object originalValue, object target)
        {
            // support readonly collections
            object targetValue = accessor.HasSetter
              ? CreateTargetValue(accessor, originalValue, target)
              : accessor.GetValue(target);

            if (targetValue == null)
            {
                return;
            }

            var sourceList = originalValue as IEnumerable;

            if (sourceList == null)
            {
                return;
            }

            // dynamic wrapper to call generic Add method
            dynamic targetList = new DynamicProxy(targetValue);

            foreach (var sourceItem in sourceList)
            {
                var cloneItem = CloneInstance(sourceItem);
                targetList.Add(cloneItem);
            }

            if (!accessor.HasSetter)
            {
                return;
            }

            accessor.SetValue(target, targetValue);
        }
Example #3
0
        /// <summary>
        /// Attempts to set the Property called <paramref name="name" /> to the <paramref name="value" /> specified.
        /// <remarks>
        /// Only properties that exist on <see cref="EntityType" /> can be set.
        /// If there is a type mismatch the request will fail.
        /// </remarks>
        /// </summary>
        /// <param name="name">The name of the Property</param>
        /// <param name="value">The new value of the Property</param>
        /// <param name="target">The target entity to set the value on</param>
        /// <returns>True if successful</returns>
        public bool TrySetPropertyValue(string name, object value, TEntityType target = null)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (!_propertiesThatExist.ContainsKey(name))
            {
                return(false);
            }

            IMemberAccessor cacheHit = _propertiesThatExist[name];

            if (value == null && !IsNullable(cacheHit.MemberType))
            {
                return(false);
            }

            if (value != null)
            {
                if (value is JToken)
                {
                    try {
                        value = JsonConvert.DeserializeObject(value.ToString(), cacheHit.MemberType);
                    } catch (Exception) {
                        return(false);
                    }
                }
                else
                {
                    bool isGuid  = cacheHit.MemberType == typeof(Guid) && value is string;
                    bool isEnum  = cacheHit.MemberType.IsEnum && value is Int64 && (long)value <= int.MaxValue;
                    bool isInt32 = cacheHit.MemberType == typeof(int) && value is Int64 && (long)value <= int.MaxValue;

                    if (!cacheHit.MemberType.IsPrimitive && !isGuid && !isEnum && !cacheHit.MemberType.IsInstanceOfType(value))
                    {
                        return(false);
                    }

                    if (isGuid)
                    {
                        value = new Guid((string)value);
                    }
                    if (isInt32)
                    {
                        value = (int)(long)value;
                    }
                    if (isEnum)
                    {
                        value = Enum.Parse(cacheHit.MemberType, value.ToString());
                    }
                }
            }

            //.Setter.Invoke(_entity, new object[] { value });
            cacheHit.SetValue(_entity ?? target, value);
            _changedProperties.Add(name);
            return(true);
        }
Example #4
0
        private void CloneArray(IMemberAccessor accessor, object originalValue, object target)
        {
            var valueType  = originalValue.GetType();
            var sourceList = originalValue as IList;

            if (sourceList == null)
            {
                return;
            }

            var targetValue = Activator.CreateInstance(valueType, sourceList.Count);
            var targetList  = targetValue as IList;

            if (targetList == null)
            {
                return;
            }

            for (var i = 0; i < sourceList.Count; i++)
            {
                targetList[i] = CloneInstance(sourceList[i]);
            }

            accessor.SetValue(target, targetList);
        }
Example #5
0
        private static void SetMemberValueToObject(MemberInfo mi, object graph, object data)
        {
            data = DecorateDate(data);

            IMemberAccessor accessor = graph as IMemberAccessor;

            if (accessor != null)
            {
                accessor.SetValue(graph, mi.Name, data);
            }
            else
            {
                switch (mi.MemberType)
                {
                case MemberTypes.Property:
                    PropertyInfo pi = (PropertyInfo)mi;
                    if (pi.CanWrite)
                    {
                        pi.SetValue(graph, data, null);
                    }
                    break;

                case MemberTypes.Field:
                    FieldInfo fi = (FieldInfo)mi;
                    fi.SetValue(graph, data);
                    break;

                default:
                    ThrowInvalidMemberInfoTypeException(mi);
                    break;
                }
            }
        }
Example #6
0
        /// <summary>
        /// Sets the property value with the specified name.
        /// </summary>
        /// <param name="target">The object whose property value will be set.</param>
        /// <param name="name">The name of the property to set.</param>
        /// <param name="value">The new value to be set.</param>
        public static void SetProperty(object target, string name, object value)
        {
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }
            Type            type       = target.GetType();
            Type            memberType = type;
            object          instance   = target;
            IMemberAccessor accessor2  = null;

            foreach (string str in name.Split(new char[] { '.' }))
            {
                if (accessor2 != null)
                {
                    instance   = accessor2.GetValue(instance);
                    memberType = accessor2.MemberType;
                }
                accessor2 = GetAccessor(memberType).FindProperty(str);
            }
            if (accessor2 == null)
            {
                throw new InvalidOperationException(string.Format("Could not find property '{0}' in type '{1}'.", name, type.Name));
            }
            accessor2.SetValue(instance, value);
        }
 private static void ShouldBehaveSimilar(IMemberAccessor<TestA, string> ma, TestA a1)
 {
     Assert.That(ma.GetValue(a1), Is.EqualTo(a1.PropString));
     ma.SetValue(a1, "updated " + a1.PropString);
     Assert.That(ma.GetValue(a1), Is.EqualTo(a1.PropString));
     a1.PropString = "new " + a1.PropString;
     Assert.That(ma.GetValue(a1), Is.EqualTo(a1.PropString));
 }
Example #8
0
 private static void ShouldBehaveSimilar(IMemberAccessor <TestA, string> ma, TestA a1)
 {
     Assert.That(ma.GetValue(a1), Is.EqualTo(a1.PropString));
     ma.SetValue(a1, "updated " + a1.PropString);
     Assert.That(ma.GetValue(a1), Is.EqualTo(a1.PropString));
     a1.PropString = "new " + a1.PropString;
     Assert.That(ma.GetValue(a1), Is.EqualTo(a1.PropString));
 }
Example #9
0
        private void SetValueWithCoercion(IMemberAccessor targetAccessor, object target, object value)
        {
            Type memberType = targetAccessor.MemberType;
            Type valueType  = value?.GetType().GetUnderlyingType();

            object v = ReflectionHelper.CoerceValue(memberType, valueType, value);

            targetAccessor.SetValue(target, v);
        }
Example #10
0
        private static void SetValueWithCoercion(IMemberAccessor sourceAccessor, object source, IMemberAccessor targetAccessor, object target)
        {
            Type sType = sourceAccessor.MemberType;
            Type tType = targetAccessor.MemberType;

            object value = sourceAccessor.GetValue(source);
            object v     = ReflectionHelper.CoerceValue(tType, sType, value);

            targetAccessor.SetValue(target, v);
        }
Example #11
0
        private void CloneObject(IMemberAccessor accessor, object originalValue, object target)
        {
            if (!accessor.HasSetter)
            {
                return;
            }

            var value = CloneInstance(originalValue);

            accessor.SetValue(target, value);
        }
Example #12
0
        private ICollection <TSource> GetReverseCollection(TDestination @object)
        {
            var collectionValue = _reverseAccessor.GetValue(@object);

            if (collectionValue == null || collectionValue.IsReadOnly)
            {
                collectionValue = collectionValue == null ? new List <TSource>() : new List <TSource>(collectionValue);
                _reverseAccessor.SetValue(@object, collectionValue);
            }
            return(collectionValue);
        }
Example #13
0
        private static void SetValueWithCoercion(IMemberAccessor targetAccessor, object target, object value)
        {
            Type pType = targetAccessor.MemberType;
            Type vType = value != null
                ? value.GetType().GetUnderlyingType()
                : null;

            object v = ReflectionHelper.CoerceValue(pType, vType, value);

            targetAccessor.SetValue(target, v);
        }
Example #14
0
        private void CloneGenericDictionary(IMemberAccessor accessor, object originalValue, object target)
        {
            // support readonly dictionary
            object targetValue = accessor.HasSetter
              ? CreateTargetValue(accessor, originalValue, target)
              : accessor.GetValue(target);

            if (targetValue == null)
            {
                return;
            }

            // must support IEnumerable
            var sourceList = originalValue as IEnumerable;

            if (sourceList == null)
            {
                return;
            }

            // dynamic wrapper to call generic Add method
            dynamic targetDictionary = new DynamicProxy(targetValue);

            var e = sourceList.GetEnumerator();

            while (e.MoveNext())
            {
                // dynamic wrapper to get the key and value
                dynamic proxy      = new DynamicProxy(e.Current);
                var     keyProxy   = proxy.Key as IDynamicProxy;
                var     valueProxy = proxy.Value as IDynamicProxy;

                if (keyProxy == null)
                {
                    continue;
                }

                object key   = keyProxy.Wrapped;
                object value = valueProxy != null ? valueProxy.Wrapped : null;

                object cloneItem = CloneInstance(value);
                targetDictionary.Add(key, cloneItem);
            }

            if (!accessor.HasSetter)
            {
                return;
            }

            accessor.SetValue(target, targetValue);
        }
Example #15
0
        private static void SetValueWithCoercion(object target, IMemberAccessor accessor, object value)
        {
            if (value == null)
            {
                return;
            }

            var pType = accessor.MemberType;
            var vType = value.GetType().GetUnderlyingType();
            var v     = ReflectionHelper.CoerceValue(pType, vType, value);

            if (v != null)
            {
                accessor.SetValue(target, v);
            }
        }
Example #16
0
        private void CloneArray(IMemberAccessor accessor, object originalValue, object target)
        {
            var valueType = originalValue.GetType();
              var sourceList = originalValue as IList;
              if (sourceList == null)
            return;

              var targetValue = Activator.CreateInstance(valueType, sourceList.Count);
              var targetList = targetValue as IList;
              if (targetList == null)
            return;

              for (int i = 0; i < sourceList.Count; i++)
            targetList[i] = CloneInstance(sourceList[i]);

              accessor.SetValue(target, targetList);
        }
Example #17
0
        public void ShouldWorkForUnknownTypeAndUnknownMember()
        {
            var             a1 = CreateTestObject();
            IMemberAccessor ma = TypeAccessor.GetMemberAccessor(typeof(TestA), "PropInt");

            Assert.That(ma.GetValue(a1), Is.EqualTo(a1.PropInt));
            ma.SetValue(a1, a1.PropInt + 10);
            Assert.That(ma.GetValue(a1), Is.EqualTo(a1.PropInt));

            IObjectMemberAccessor oma = a1.GetObjectMemberAccessor("Field1");

            Assert.That(oma.GetValue(), Is.EqualTo(a1.Field1));
            oma.SetValue(12.56);
            Assert.That(oma.GetValue(), Is.EqualTo(a1.Field1));
            a1.Field1 -= 345;
            Assert.That(oma.GetValue(), Is.EqualTo(a1.Field1));
        }
Example #18
0
        /// <summary>
        /// Sets the field value with the specified name.
        /// </summary>
        /// <param name="target">The object whose field value will be set.</param>
        /// <param name="name">The name of the field to set.</param>
        /// <param name="value">The new value to be set.</param>
        public static void SetField(object target, string name, object value)
        {
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }
            Type            type     = target.GetType();
            IMemberAccessor accessor = FindField(type, name);

            if (accessor == null)
            {
                throw new InvalidOperationException(string.Format("Could not find field '{0}' in type '{1}'.", name, type.Name));
            }
            accessor.SetValue(target, value);
        }
Example #19
0
        private void CloneDictionary(IMemberAccessor accessor, object originalValue, object target)
        {
            // support readonly dictionary
            var targetValue = accessor.HasSetter
                ? CreateTargetValue(accessor, originalValue, target)
                : accessor.GetValue(target);

            if (targetValue == null)
            {
                return;
            }

            // must support IDictionary
            var sourceList = originalValue as IDictionary;

            if (sourceList == null)
            {
                return;
            }

            var targetList = targetValue as IDictionary;

            if (targetList == null)
            {
                return;
            }

            var e = sourceList.GetEnumerator();

            while (e.MoveNext())
            {
                var cloneItem = CloneInstance(e.Value);
                targetList.Add(e.Key, cloneItem);
            }

            if (!accessor.HasSetter)
            {
                return;
            }

            accessor.SetValue(target, targetValue);
        }
Example #20
0
        private object CreateTargetValue(IMemberAccessor accessor, object originalValue, object target)
        {
            var    valueType = originalValue.GetType();
            object targetValue;

            // check if this object has already been cloned
            // using RuntimeHelpers.GetHashCode to get object identity
            int hashCode = RuntimeHelpers.GetHashCode(originalValue);

            if (_objectReferences.TryGetValue(hashCode, out targetValue))
            {
                accessor.SetValue(target, targetValue);
                return(null);
            }

            targetValue = LateBinder.CreateInstance(valueType);
            // keep track of cloned instances
            _objectReferences.Add(hashCode, targetValue);
            return(targetValue);
        }
Example #21
0
        /// <summary>
        /// Sets the property value with the specified name.
        /// </summary>
        /// <param name="target">The object whose property value will be set.</param>
        /// <param name="name">The name of the property to set.</param>
        /// <param name="value">The new value to be set.</param>
        /// <remarks>This method supports nested property names. An exmample name would be 'Person.Address.ZipCode'.</remarks>
        /// <param name="flags">A bitmask comprised of one or more <see cref="BindingFlags"/> that specify how the search is conducted.</param>
        public static void SetProperty(object target, string name, object value, BindingFlags flags)
        {
            if (target == null)
            {
                throw new ArgumentNullException("target");
            }
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name");
            }

            Type   rootType      = target.GetType();
            Type   currentType   = rootType;
            object currentTarget = target;

            TypeAccessor    typeAccessor;
            IMemberAccessor memberAccessor = null;

            // support nested property
            var parts = name.Split('.');

            foreach (var part in parts)
            {
                if (memberAccessor != null)
                {
                    currentTarget = memberAccessor.GetValue(currentTarget);
                    currentType   = memberAccessor.MemberType;
                }

                typeAccessor   = TypeAccessor.GetAccessor(currentType);
                memberAccessor = typeAccessor.FindProperty(part, flags);
            }

            if (memberAccessor == null)
            {
                throw new InvalidOperationException(string.Format(
                                                        "Could not find property '{0}' in type '{1}'.", name, rootType.Name));
            }

            memberAccessor.SetValue(currentTarget, value);
        }
Example #22
0
        private void CloneCollection(IMemberAccessor accessor, object originalValue, object target)
        {
            // support readonly collections
            var targetValue = accessor.HasSetter
                ? CreateTargetValue(accessor, originalValue, target)
                : accessor.GetValue(target);

            if (targetValue == null)
            {
                return;
            }

            var sourceList = originalValue as IEnumerable;

            if (sourceList == null)
            {
                return;
            }

            // must support IList
            var targetList = targetValue as IList;

            if (targetList == null)
            {
                return;
            }

            foreach (var sourceItem in sourceList)
            {
                var cloneItem = CloneInstance(sourceItem);
                targetList.Add(cloneItem);
            }

            if (!accessor.HasSetter)
            {
                return;
            }

            accessor.SetValue(target, targetValue);
        }
Example #23
0
    /// <summary>
    /// Sets the property value with the specified name.
    /// </summary>
    /// <param name="target">The object whose property value will be set.</param>
    /// <param name="name">The name of the property to set.</param>
    /// <param name="value">The new value to be set.</param>
    public static void SetProperty(object target, string name, object value)
    {
        if (target == null)
        {
            throw new ArgumentNullException(nameof(target));
        }
        if (String.IsNullOrEmpty(name))
        {
            throw new ArgumentNullException(nameof(name));
        }

        var    rootType      = target.GetType();
        var    currentType   = rootType;
        object currentTarget = target;

        TypeAccessor    typeAccessor;
        IMemberAccessor memberAccessor = null;

        // support nested property
        string[] parts = name.Split('.');
        foreach (string part in parts)
        {
            if (memberAccessor != null)
            {
                currentTarget = memberAccessor.GetValue(currentTarget);
                currentType   = memberAccessor.MemberType;
            }

            typeAccessor   = GetAccessor(currentType);
            memberAccessor = typeAccessor.FindProperty(part);
        }

        if (memberAccessor == null)
        {
            throw new InvalidOperationException($"Could not find property '{name}' in type '{rootType.Name}'.");
        }

        memberAccessor.SetValue(currentTarget, value);
    }
    private void CloneDictionary(IMemberAccessor accessor, object originalValue, object target)
    {
      // support readonly dictionary
      object targetValue = accessor.HasSetter
        ? CreateTargetValue(accessor, originalValue, target)
        : accessor.GetValue(target);

      if (targetValue == null)
        return;

      // must support IDictionary
      var sourceList = originalValue as IDictionary;
      if (sourceList == null)
        return;

      var targetList = targetValue as IDictionary;
      if (targetList == null)
        return;

      var e = sourceList.GetEnumerator();
      while (e.MoveNext())
      {
        var cloneItem = CloneInstance(e.Value);
        targetList.Add(e.Key, cloneItem);
      }

      if (!accessor.HasSetter)
        return;

      accessor.SetValue(target, targetValue);
    }
    private void CloneCollection(IMemberAccessor accessor, object originalValue, object target)
    {
      // support readonly collections
      object targetValue = accessor.HasSetter
        ? CreateTargetValue(accessor, originalValue, target)
        : accessor.GetValue(target);

      if (targetValue == null)
        return;

      var sourceList = originalValue as IEnumerable;
      if (sourceList == null)
        return;

      // must support IList
      var targetList = targetValue as IList;
      if (targetList == null)
        return;

      foreach (var sourceItem in sourceList)
      {
        var cloneItem = CloneInstance(sourceItem);
        targetList.Add(cloneItem);
      }

      if (!accessor.HasSetter)
        return;

      accessor.SetValue(target, targetValue);
    }
Example #26
0
 public void SetValue(TMember value)
 {
     _rtAccessor.SetValue(_object, value);
 }
 public void SetValue(object destination, object valueToSet)
 {
     _memberAccessor.SetValue(destination, valueToSet);
 }
 public void should_be_able_to_set_a_value_to_the_property()
 {
     _result.SetValue(_project, "newid");
 }
    private void CloneObject(IMemberAccessor accessor, object originalValue, object target)
    {
      if (!accessor.HasSetter)
        return;

      object value = CloneInstance(originalValue);
      accessor.SetValue(target, value);
    }
 public void should_be_able_to_set_a_value()
 {
     _result.SetValue(_project, "new project manager");
 }
        private void StartDeserialize(object instance, IMemberAccessor memberAccessor)
        {
            var properties = instance.GetType().GetProperties();

            foreach (PropertyInfo property in properties)
            {
                var type = property.PropertyType;
                if (type.Equals(typeof(Boolean)))
                {
                    memberAccessor.SetValue(instance, property.Name, this.ReadBoolean());
                }
                else if (type.Equals(typeof(Boolean[])))
                {
                    memberAccessor.SetValue(instance, property.Name, this.ReadArray(ReadBoolean));
                }
                else if (type.Equals(typeof(Byte)))
                {
                    memberAccessor.SetValue(instance, property.Name, this.ReadByte());
                }
                else if (type.Equals(typeof(Byte[])))
                {
                    memberAccessor.SetValue(instance, property.Name, this.ReadBytes());
                }
                else if (type.Equals(typeof(Int16)))
                {
                    memberAccessor.SetValue(instance, property.Name, this.ReadInt16());
                }
                else if (type.Equals(typeof(Int16[])))
                {
                    memberAccessor.SetValue(instance, property.Name, this.ReadArray(ReadInt16));
                }
                else if (type.Equals(typeof(Int32)))
                {
                    memberAccessor.SetValue(instance, property.Name, this.ReadInt32());
                }
                else if (type.Equals(typeof(Int32[])))
                {
                    memberAccessor.SetValue(instance, property.Name, this.ReadArray(ReadInt32));
                }
                else if (type.Equals(typeof(Int64)))
                {
                    memberAccessor.SetValue(instance, property.Name, this.ReadInt64());
                }
                else if (type.Equals(typeof(Int64[])))
                {
                    memberAccessor.SetValue(instance, property.Name, this.ReadArray(ReadInt64));
                }
                else if (type.Equals(typeof(String)))
                {
                    memberAccessor.SetValue(instance, property.Name, this.ReadString());
                }
                else if (type.Equals(typeof(String[])))
                {
                    memberAccessor.SetValue(instance, property.Name, this.ReadArray(ReadString));
                }
                else if (type.Equals(typeof(Single)))
                {
                    memberAccessor.SetValue(instance, property.Name, this.ReadFloat());
                }
                else if (type.Equals(typeof(Single[])))
                {
                    memberAccessor.SetValue(instance, property.Name, this.ReadArray(ReadFloat));
                }
                else if (type.Equals(typeof(Double)))
                {
                    memberAccessor.SetValue(instance, property.Name, this.ReadDouble());
                }
                else if (type.Equals(typeof(Double[])))
                {
                    memberAccessor.SetValue(instance, property.Name, this.ReadArray(ReadDouble));
                }
                else if (type.Equals(typeof(DateTime)))
                {
                    memberAccessor.SetValue(instance, property.Name, this.ReadDateTime());
                }
                else if (type.BaseType.Equals(typeof(Enum)))
                {
                    memberAccessor.SetValue(instance, property.Name, this.ReadEnum(property.PropertyType));
                }
                else if (type.IsArray)
                {
                    if (type.GetElementType().IsValueType)
                    {
                        throw new Exception("not supported this value array!");
                    }

                    var elementType         = type.GetElementType();
                    var arrayMemberAccessor = memberAccessor.Type.Equals(elementType) ? memberAccessor : DynamicMethodMemberAccessor.FindClassAccessor(elementType);
                    memberAccessor.SetValue(instance, property.Name, this.ReadArray(property.PropertyType, arrayMemberAccessor));
                }
                else
                {
                    if (this.ReadInt32() == 1)
                    {
                        var childerInstanceMemberAccessor = memberAccessor.Type.Equals(property.PropertyType) ? memberAccessor : DynamicMethodMemberAccessor.FindClassAccessor(property.PropertyType);
                        var childerInstance = Activator.CreateInstance(property.PropertyType);
                        this.StartDeserialize(childerInstance, childerInstanceMemberAccessor);
                        childerInstanceMemberAccessor.SetValue(instance, property.Name, childerInstance);
                    }
                }
            }
        }
    private object CreateTargetValue(IMemberAccessor accessor, object originalValue, object target)
    {
      var valueType = originalValue.GetType();
      object targetValue;

      // check if this object has already been cloned 
      // using RuntimeHelpers.GetHashCode to get object identity
      int hashCode = RuntimeHelpers.GetHashCode(originalValue);
      if (_objectReferences.TryGetValue(hashCode, out targetValue))
      {
        accessor.SetValue(target, targetValue);
        return null;
      }

      targetValue = LateBinder.CreateInstance(valueType);
      // keep track of cloned instances
      _objectReferences.Add(hashCode, targetValue);
      return targetValue;
    }
Example #33
0
        private void CloneGenericCollection(IMemberAccessor accessor, object originalValue, object target)
        {
            // support readonly collections
            object targetValue = accessor.HasSetter
              ? CreateTargetValue(accessor, originalValue, target)
              : accessor.GetValue(target);

            if (targetValue == null)
                return;

            var sourceList = originalValue as IEnumerable;
            if (sourceList == null)
                return;

            // dynamic wrapper to call generic Add method
            dynamic targetList = new DynamicProxy(targetValue);
            
            foreach (var sourceItem in sourceList)
            {
                var cloneItem = CloneInstance(sourceItem);
                targetList.Add(cloneItem);
            }

            if (!accessor.HasSetter)
                return;

            accessor.SetValue(target, targetValue);
        }
Example #34
0
        private void CloneGenericDictionary(IMemberAccessor accessor, object originalValue, object target)
        {
            // support readonly dictionary
            object targetValue = accessor.HasSetter
              ? CreateTargetValue(accessor, originalValue, target)
              : accessor.GetValue(target);

            if (targetValue == null)
                return;

            // must support IEnumerable
            var sourceList = originalValue as IEnumerable;
            if (sourceList == null)
                return;

            // dynamic wrapper to call generic Add method
            dynamic targetDictionary = new DynamicProxy(targetValue);
            
            var e = sourceList.GetEnumerator();
            while (e.MoveNext())
            {
                // dynamic wrapper to get the key and value
                dynamic proxy = new DynamicProxy(e.Current);
                var keyProxy = proxy.Key as IDynamicProxy;
                var valueProxy = proxy.Value as IDynamicProxy;

                if (keyProxy == null)
                    continue;

                object key = keyProxy.Wrapped;
                object value = valueProxy != null ? valueProxy.Wrapped : null;

                object cloneItem = CloneInstance(value);
                targetDictionary.Add(key, cloneItem);
            }

            if (!accessor.HasSetter)
                return;

            accessor.SetValue(target, targetValue);
        }
 public void should_throw_an_exception_when_setting_a_value_to_the_property()
 {
     The.Action(() => _result.SetValue(_project, "new project manager")).ShouldThrowAn <MemberAccessException>();
 }