As part of a larger project I wrote this sample code:
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UnitTestLibrary
{
public class temp
{
private System.Int32 _someNumber;
[Range(1,5)]
public System.Int32 somenumber
{
get
{
return _someNumber;
}
set
{
if(_someNumber != value)
{
_someNumber = value;
}
}
}
public bool ValidateProperty()
{
//do validation of a single property
List<ValidationResult> validationResults = new List<ValidationResult>();
//works as expected
bool result = Validator.TryValidateObject(this, new ValidationContext(this), validationResults);
//throws System.ArgumentException: The value for property 'somenumber' must be of type System.Int32
ValidationContext validationContext = new ValidationContext(this, null)
{
MemberName = "somenumber"
};
result = Validator.TryValidateProperty(this, validationContext, validationResults);
return result;
}
}
[TestClass]
public class temptest
{
[TestMethod]
public void testmethod()
{
temp tempa = new temp();
tempa.somenumber = (System.Int32)3;
Assert.IsTrue(tempa.ValidateProperty());
}
}
}
why do I get an exception on the type of the property?
Maybe important: I use this in the context of Windows RT unit test project using Visual Studio 2013 express for Windows (store apps).
thanks