Skip to content

Commit

Permalink
Improved property value validation, minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
bramborman committed Apr 22, 2017
1 parent abdfdf1 commit 0a0de1f
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 22 deletions.
14 changes: 0 additions & 14 deletions NotifyPropertyChangedBase/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,5 @@ internal static bool GetIsValueType(this Type type)
return type.IsValueType;
#endif
}

internal static bool GetIsSubclassOf(this Type type, Type baseClass)
{
#if WINDOWS_UWP
return type.GetTypeInfo().IsSubclassOf(baseClass);
#else
return type.IsSubclassOf(baseClass);
#endif
}

internal static bool GetIsNullableOfT(this Type type)
{
return type.Name == "Nullable`1";
}
}
}
9 changes: 3 additions & 6 deletions NotifyPropertyChangedBase/NotifyPropertyChanged.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
#if WINDOWS_UWP
using System.Reflection;
#endif
Expand Down Expand Up @@ -266,18 +265,16 @@ private void ValidateValueForType(object value, Type type)
{
if (value == null)
{
if (type.GetIsValueType() && !type.GetIsNullableOfT())
if (type.GetIsValueType() && Nullable.GetUnderlyingType(type) == null)
{
throw new ArgumentException($"The type '{type}' is not a nullable type.");
}
}
else
{
Type valueType = value.GetType();

if (valueType != type && !valueType.GetIsSubclassOf(type) && !(type.GetIsNullableOfT() && type.GetGenericArguments().Contains(valueType)))
if (!type.IsAssignableFrom(value.GetType()))
{
throw new ArgumentException($"Value of type {valueType} cannot be assigned to a property of type ({type})");
throw new ArgumentException($"The specified value cannot be assigned to a property of type ({type})");
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[![NuGet](https://img.shields.io/nuget/v/NotifyPropertyChangedBase.svg)](https://www.nuget.org/packages/NotifyPropertyChangedBase/)
[![Build status](https://ci.appveyor.com/api/projects/status/jc9gcr4gldjr8nq6/branch/master?svg=true)](https://ci.appveyor.com/project/bramborman/notifypropertychangedbase/branch/master)
[![Issues](https://img.shields.io/github/issues/bramborman/NotifyPropertyChangedBase.svg)](https://github.com/bramborman/NotifyPropertyChangedBase/issues)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](LICENSE.md)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/bramborman/NotifyPropertyChangedBase/blob/master/LICENSE.md)

NotifyPropertyChangedBase provides you an easy to use base class `NotifyPropertyChanged` that helps you implement the `INotifyPropertyChanged` interface which can help you in many cases, whether are you developing WPF, Console, UWP or any other app.

Expand Down
1 change: 0 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ branches:
only:
- master
- dev
- unit-tests-preparation
skip_tags: true
skip_commits:
files:
Expand Down

0 comments on commit 0a0de1f

Please sign in to comment.