/// <summary> /// Gets the property's value. /// </summary> /// <typeparam name="T"> /// The property value type. This must be the same as the type returned by <see cref="PropertyAccessorData.PropertyType"/>: For simple value properties, /// this is the simple property type. For related objects, this is the related object's type. For related object collections, /// this is <see cref="ObjectList{T}"/>, where "T" is the related objects' type. /// </typeparam> /// <returns>The value of the encapsulated property. For simple value properties, /// this is the property value. For related objects, this is the related object. For related object collections, /// this is an <see cref="ObjectList{T}"/>, where "T" is the related objects' type.</returns> /// <exception cref="InvalidTypeException"> /// The type requested via <typeparamref name="T"/> is not the same as the property's type indicated by <see cref="PropertyAccessorData.PropertyType"/>. /// </exception> /// <exception cref="ClientTransactionsDifferException">The <see cref="DomainObject"/> cannot be used in the current <see cref="DomainObjects.ClientTransaction"/>.</exception> /// <exception cref="ObjectInvalidException">The object is invalid in the associated <see cref="ClientTransaction"/>.</exception> public T GetValue <T> () { CheckType(typeof(T)); object value = GetValueWithoutTypeCheck(); Assertion.DebugAssert( value != null || NullableTypeUtility.IsNullableType(PropertyData.PropertyType), "Property '{0}' is a value type but the DataContainer returned null.", PropertyData.PropertyIdentifier); try { return((T)value); } catch (InvalidCastException ex) { Assertion.IsNotNull(value, "Otherwise, the cast would have succeeded (ref type) or thrown a NullReferenceException (value type)."); var message = string.Format( "The property '{0}' was expected to hold an object of type '{1}', but it returned an object of type '{2}'.", PropertyData.PropertyIdentifier, PropertyData.PropertyType, value.GetType()); throw new InvalidTypeException(message, ex); } }
private void CheckConstructorArguments(ConstructorInfo constructor, object[] constructorArguments) { var parameters = constructor.GetParameters(); if (parameters.Length != constructorArguments.Length) { var message = string.Format("Expected {0} constructor argument(s), but was {1}.", parameters.Length, constructorArguments.Length); throw new ArgumentException(message, "constructorArguments"); } for (int i = 0; i < parameters.Length; i++) { var parameterType = parameters[i].ParameterType; var argument = constructorArguments[i]; if (argument == null) { if (!NullableTypeUtility.IsNullableType(parameterType)) { var message = string.Format("Constructor parameter at position {0} of type '{1}' cannot be null.", i, parameterType); throw new ArgumentException(message, "constructorArguments"); } } else if (!parameterType.IsInstanceOfType(argument)) { throw ArgumentUtility.CreateArgumentItemTypeException("constructorArguments", i, parameterType, argument.GetType()); } } }
private Type GetPropertyType(PropertyDefinition propertyDefinition) { var propertyType = propertyDefinition.IsObjectID ? propertyDefinition.PropertyInfo.PropertyType : propertyDefinition.PropertyType; if (NullableTypeUtility.IsNullableType(propertyType)) { propertyType = NullableTypeUtility.GetBasicType(propertyType); } return(propertyType); }
public void CollectPropertyType(PropertyDefinition propertyDefinition) { ArgumentUtility.CheckNotNull("propertyDefinition", propertyDefinition); var propertyType = propertyDefinition.PropertyType; if (NullableTypeUtility.IsNullableType(propertyType)) { propertyType = NullableTypeUtility.GetBasicType(propertyType); } if (propertyType.IsEnum) { _enumTypes.Add(propertyType); } }
public PropertyDefinition( ClassDefinition classDefinition, IPropertyInformation propertyInfo, string propertyName, bool isObjectID, bool isNullable, int?maxLength, StorageClass storageClass) { ArgumentUtility.CheckNotNull("classDefinition", classDefinition); ArgumentUtility.CheckNotNullOrEmpty("propertyName", propertyName); ArgumentUtility.CheckNotNull("propertyInfo", propertyInfo); _classDefinition = classDefinition; _propertyInfo = propertyInfo; _propertyType = isObjectID ? typeof(ObjectID) : propertyInfo.PropertyType; _propertyName = propertyName; _isObjectID = isObjectID; _isNullablePropertyType = NullableTypeUtility.IsNullableType(propertyInfo.PropertyType); _isNullable = isNullable; _maxLength = maxLength; _storageClass = storageClass; }
private bool IsNullSupported(Type dotNetType) { return(NullableTypeUtility.IsNullableType(dotNetType)); }
public void IsNullableType_WithNull_ThrowsArgumentNullException() { Assert.That( () => NullableTypeUtility.IsNullableType(null), Throws.TypeOf <ArgumentNullException>().With.Message.EndsWith("Parameter name: type")); }
public void IsNullableType_ReferenceType() { Assert.That(NullableTypeUtility.IsNullableType(typeof(object)), Is.True); Assert.That(NullableTypeUtility.IsNullableType(typeof(string)), Is.True); }
public void IsNullableType_NullableValueType() { Assert.That(NullableTypeUtility.IsNullableType(typeof(int?)), Is.True); Assert.That(NullableTypeUtility.IsNullableType(typeof(DateTime?)), Is.True); }
public void IsNullableType_ValueType() { Assert.That(NullableTypeUtility.IsNullableType(typeof(int)), Is.False); Assert.That(NullableTypeUtility.IsNullableType(typeof(DateTime)), Is.False); }