Validation Message shows Twice

Feb 15, 2010 at 9:12 AM
Edited Feb 15, 2010 at 10:26 AM

I am adding a [NotNullOrEmpty] attribute to a property of an inherited class.

On validation the message shows twice (The exception is thrown twice).

Does any one have an idea of why could this be happening? Has anyone seen this before?

 

UPDATE:

I looked into the source code to see if it is problem when using derived classes. I updated the TestNotNullOrEmptyAttribute to use a 'DerivedDummy', and subjected my validation test on that object. And yes, the message comes up twice. So this seems to be the issue. The validation function is called on the Derived as well as the Inherited class; which is why the properties in the inherited class are validated twice.

I am not a C# expert, so will take to debug into the source code and find a solution. Till then if any one has an update on this, please share.

Feb 15, 2010 at 11:46 AM
Edited Feb 15, 2010 at 11:47 AM

I seem to have solved the problem.

I have modified the TypeExtensions class to return only the properties of the class to be validated and not of the base classes. I ran through the tests and all worked fine except one; TestTypeExtensions. This was obvious as the function will now only return the properties of a single class.

This works for me because the ValidateObject loops through the base classes as well to validate.

Here is the code in case someone else wants it.

 

namespace ValidationAspects.Core
{
	public static class TypeExtensions
	{
		// this returns all properties including private properties in base classes, which FlattenHierachy does not
		public static IEnumerable<PropertyInfo> GetAllProperties(this Type type)
		{
			Enforce.IsNotNull(type, "type");

            //do
            //{
				foreach (PropertyInfo propertyInfo in
					type.GetProperties(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly))
				{
					yield return propertyInfo;
				}

            //    type = type.BaseType;            
            //} while (type != null);
		}
	}
}

 

Mike, can this change break some other functionality?