public void SetProperty(IDeserializerNode targetNode, PropertySpec property, object propertyValue) { if (targetNode.Operation == DeserializerNodeOperation.Default) { throw new InvalidOperationException("Invalid deserializer node operation default"); } if ((targetNode.Operation == DeserializerNodeOperation.Post && property.AccessMode.HasFlag(HttpMethod.Post)) || (targetNode.Operation == DeserializerNodeOperation.Patch && property.AccessMode.HasFlag(HttpMethod.Put))) { property.SetValue(targetNode.Value, propertyValue, targetNode.Context); } else { var propPath = string.IsNullOrEmpty(targetNode.ExpandPath) ? property.Name : targetNode.ExpandPath + "." + property.Name; throw new ResourceValidationException( $"Property {property.Name} of resource {targetNode.ValueType.Name} is not writable.", propPath, targetNode.ValueType.Name, null); } }
public void SetProperty(IDeserializerNode target, PropertySpec property, object propertyValue) { if (!property.IsWritable) { throw new InvalidOperationException("Unable to set property."); } if (typeof(IClientRepository).IsAssignableFrom(property.PropertyType)) { var repoImplementationType = this.clientRepositoryImplementationMap.GetOrAdd(property.PropertyType, t => t.Assembly.GetTypes() .First( x => !x.IsInterface && x.IsClass && t .IsAssignableFrom (x))); var listProxyValue = propertyValue as LazyCollectionProxy; object repo; if (listProxyValue != null) { repo = Activator.CreateInstance(repoImplementationType, this.client, listProxyValue.Uri, null, target.Value); } else { repo = Activator.CreateInstance(repoImplementationType, this.client, target.Uri + "/" + NameUtils.ConvertCamelCaseToUri(property.Name), propertyValue, target.Value); } property.SetValue(target.Value, repo); return; } property.SetValue(target.Value, propertyValue); }
public void SetValue_InstanceIsNull_ThrowTargetException() { // Setup var target = new ClassWithProperties(); var propertySpec = new PropertySpec(target.GetType().GetProperty("IntegerProperty")); // Call TestDelegate call = () => propertySpec.SetValue(null, 2); // Assert Assert.Throws <TargetException>(call); }
public void SetValue_ProperInstanceTypeAndValueType_PropertyIsUpdated() { // Setup var target = new ClassWithProperties(); var propertySpec = new PropertySpec(target.GetType().GetProperty("IntegerProperty")); // Call propertySpec.SetValue(target, 2); // Assert Assert.AreEqual(2, target.IntegerProperty); }
public void SetValue_SettingValueOfIncorrectType_ThrowArgumentException() { // Setup var target = new ClassWithProperties(); var propertySpec = new PropertySpec(target.GetType().GetProperty("IntegerProperty")); // Call TestDelegate call = () => propertySpec.SetValue(target, new object()); // Assert var exception = Assert.Throws <ArgumentException>(call); Assert.IsNull(exception.InnerException); }
public void SetValue_PropertyWithoutPublicSet_ThrowInvalidOperationException() { // Setup var target = new ClassWithProperties(); var propertySpec = new PropertySpec(target.GetType().GetProperty("DoublePropertyWithOnlyGetter")); // Call TestDelegate call = () => propertySpec.SetValue(target, 2); // Assert var exception = Assert.Throws <InvalidOperationException>(call); Assert.AreEqual("Property lacks public setter!", exception.Message); }
public void SetValue_SettingValueResultsInException_ThrowTargetInvocationException() { // Setup var target = new ClassWithProperties(); var propertySpec = new PropertySpec(target.GetType().GetProperty("ThrowsException")); // Call TestDelegate call = () => propertySpec.SetValue(target, ""); // Assert Exception innerException = Assert.Throws <TargetInvocationException>(call).InnerException; Assert.IsInstanceOf <ArgumentException>(innerException); }