I want to get the StringLength attribute.
Class code :
var type1 = Type.GetType("MvcApplication4.Models.Sample.SampleMasterModel");
var metadata = ModelMetadataProviders.Current.GetMetadataForType(null, type1);
var properties = metadata.Properties;
var prop = properties.FirstOrDefault(p => p.PropertyName == "Remark");
?? Get StringLength attr?
Model Code:
public class SampleModel
{
[StringLength(50)]
public string Remark { get; set; }
}
Based on wudzik and Habib help. I modified the code.
Final Code:
PropertyInfo propertyInfo = type1.GetProperties().FirstOrDefault(p => p.Name == "Remark");
if (propertyInfo != null)
{
var attributes = propertyInfo.GetCustomAttributes(true);
var stringLengthAttrs =
propertyInfo.GetCustomAttributes(typeof (StringLengthAttribute), true).First();
var stringLength = stringLengthAttrs != null ? ((StringLengthAttribute)stringLengthAttrs).MaximumLength : 0;
}
You can get CustomAttributes through PropertyInfo like:
PropertyInfo propertyInfo = type1.GetProperties().FirstOrDefault(p=> p.Name == "Remark");
if (propertyInfo != null)
{
var attributes = propertyInfo.GetCustomAttributes(true);
}
/// <summary>
/// Returns the StringLengthAttribute for a property based on the property name passed in.
/// Use this method in the class or in a base class
/// </summary>
/// <param name="type">This type of the class where you need the property StringLengthAttribute.</param>
/// <param name="propertyName">This is the property name.</param>
/// <returns>
/// StringLengthAttribute of the propertyName passed in, for the Type passed in
/// </returns>
public static StringLengthAttribute GetStringLengthAttribute(Type type, string propertyName)
{
StringLengthAttribute output = null;
try
{
output = (StringLengthAttribute)type.GetProperty(propertyName).GetCustomAttribute(typeof(StringLengthAttribute));
}
catch (Exception ex)
{
//error handling
}
return output;
} //GetStringLengthAttribute
/// <summary>
/// Returns the StringLengthAttribute for a property based on the property name passed in.
/// Use this method in the class or in a base class
/// </summary>
/// <param name="propertyName">This is the property name.</param>
/// <returns>
/// StringLengthAttribute of the propertyName passed in, for the current class
/// </returns>
public StringLengthAttribute GetStringLengthAttribute(string propertyName)
{
StringLengthAttribute output = null;
try
{
output = (StringLengthAttribute)this.GetType().GetProperty(propertyName).GetCustomAttribute(typeof(StringLengthAttribute));
}
catch (Exception ex)
{
//error handling
}
return output;
} //GetStringLengthAttribute
}
Related
I am using Dapper ( and I couldn't be happier), I know how to access normal stored procedures as mentioned here, however, how do I pass on the the Npgsql refcursor name to the proc (using C#)? For example:
I have a proc that looks like:
FUNCTION xx.getData(
v_ref refcursor,
v_id integer)
RETURNS refcursor AS
...
How would I specify the parameters for xx.getData?
For example, if getData accepted just one parameter of type int, then I could call it like so:
var data = cnn.Query<myType>("xx.getData", new {Id = 1},
commandType: CommandType.StoredProcedure);
OR
var p = new DynamicParameters();
p.Add("#id", 11);
cnn.Execute("xx.getData", p, commandType: CommandType.StoredProcedure);
I can't find the correct type in System.DbType to pass on in the query.
Note that a refcursor corresponds to an active cursor that has already been opened in a previous call. In other words, it does not correspond to a stored procedure, but rather to a resultset (possibly but not necessarily returned from a stored procedure).
Just in case you really do need to send a refcursor, what you're looking for is NpgsqlDbType.Refcursor.
Bellow custom dynamic parameter compatible for Npgsql data types. It will work for with output refcursor parameters.
/// <summary>
/// Npgsql Dynamic Param for Dapper
/// </summary>
public class PgParam : SqlMapper.IDynamicParameters
{
private static readonly Dictionary<SqlMapper.Identity, Action<IDbCommand, object>> paramReaderCache = new Dictionary<SqlMapper.Identity, Action<IDbCommand, object>>();
private readonly Dictionary<string, ParamInfo> _parameters = new Dictionary<string, ParamInfo>();
private List<object> templates;
/// <summary>
/// construct a dynamic parameter bag
/// </summary>
public PgParam()
{
}
/// <summary>
/// construct a dynamic parameter bag
/// </summary>
/// <param name="template">can be an anonymous type or a DynamicParameters bag</param>
public PgParam(object template)
{
AddDynamicParams(template);
}
/// <summary>
/// All the names of the param in the bag, use Get to yank them out
/// </summary>
public IEnumerable<string> ParameterNames
{
get { return _parameters.Select(p => p.Key); }
}
void SqlMapper.IDynamicParameters.AddParameters(IDbCommand command, SqlMapper.Identity identity)
{
AddParameters(command, identity);
}
/// <summary>
/// Append a whole object full of params to the dynamic
/// EG: AddDynamicParams(new {A = 1, B = 2}) // will add property A and B to the dynamic
/// </summary>
/// <param name="param"></param>
public void AddDynamicParams(
dynamic param
)
{
if (param is object obj)
{
if (!(obj is PgParam subDynamic))
{
if (!(obj is IEnumerable<KeyValuePair<string, object>> dictionary))
{
templates = templates ?? new List<object>();
templates.Add(obj);
}
else
{
foreach (var kvp in dictionary)
{
Add(kvp.Key, kvp.Value);
}
}
}
else
{
if (subDynamic._parameters != null)
foreach (var kvp in subDynamic._parameters)
_parameters.Add(kvp.Key, kvp.Value);
if (subDynamic.templates != null)
{
templates = templates ?? new List<object>();
foreach (var t in subDynamic.templates) templates.Add(t);
}
}
}
}
/// <summary>
/// Add a parameter to this dynamic parameter list
/// </summary>
/// <param name="name"></param>
/// <param name="value"></param>
/// <param name="dbType"></param>
/// <param name="direction"></param>
/// <param name="size"></param>
public void Add(string name, object value = null, NpgsqlDbType? dbType = null, ParameterDirection? direction = null,int? size = null)
{
_parameters[name] = new ParamInfo
{
Name = name, Value = value, ParameterDirection = direction ?? ParameterDirection.Input, DbType = dbType,
Size = size
};
}
/// <summary>
/// Add all the parameters needed to the command just before it executes
/// </summary>
/// <param name="command">The raw command prior to execution</param>
/// <param name="identity">Information about the query</param>
protected void AddParameters(IDbCommand command, SqlMapper.Identity identity)
{
if (templates != null)
foreach (var template in templates)
{
var newIdent = identity.ForDynamicParameters(template.GetType());
Action<IDbCommand, object> appender;
lock (paramReaderCache)
{
if (!paramReaderCache.TryGetValue(newIdent, out appender))
{
appender = SqlMapper.CreateParamInfoGenerator(newIdent, false, true);
paramReaderCache[newIdent] = appender;
}
}
appender(command, template);
}
foreach (var param in _parameters.Values)
{
var add = !((NpgsqlCommand) command).Parameters.Contains(param.Name);
NpgsqlParameter p;
if (add)
{
p = ((NpgsqlCommand) command).CreateParameter();
p.ParameterName = param.Name;
}
else
{
p = ((NpgsqlCommand) command).Parameters[param.Name];
}
var val = param.Value;
p.Value = val ?? DBNull.Value;
p.Direction = param.ParameterDirection;
if (param.Size != null) p.Size = param.Size.Value;
if (param.DbType != null) p.NpgsqlDbType = param.DbType.Value;
if (add) command.Parameters.Add(p);
param.AttachedParam = p;
}
}
/// <summary>
/// Get the value of a parameter
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="name"></param>
/// <returns>The value, note DBNull.Value is not returned, instead the value is returned as null</returns>
public T Get<T>(string name)
{
var val = _parameters[name].AttachedParam.Value;
if (val == DBNull.Value)
{
if (default(T) != null)
throw new ApplicationException("Attempting to cast a DBNull to a non nullable type!");
return default(T);
}
return (T) val;
}
private class ParamInfo
{
public string Name { get; set; }
public object Value { get; set; }
public ParameterDirection ParameterDirection { get; set; }
public NpgsqlDbType? DbType { get; set; }
public int? Size { get; set; }
public IDbDataParameter AttachedParam { get; set; }
}
}
I'm working on a WPF application with MVVM pattern in which i use DataAnnotations for validation.
So i implemented a solution like that in this article.
Then i tried to add a property to my viewmodel - called "Age" - which shall only accept numbers and have a range between 1 and 100.
[RegularExpression(#"^[0-9]{1,3}$", ErrorMessage = "Please enter a valid number")]
[Range(1, 100, ErrorMessage = "Age should be between 1 to 100")]
/// <summary>
/// Gets or sets the age.
/// </summary>
public int Age
{
get { return GetValue(() => Age); }
set { SetValue(() => Age, value); }
}
And on my WPF window i got a textbox which is bound to Age:
<TextBox x:Name="tbx_age"
ToolTip="The age"
Text="{Binding Age, NotifyOnSourceUpdated=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}">
</TextBox>
As i'm starting the application the textbox is preallocated with zero ("0").
When i replace the zero in the textbox by "1a" i get the error message "The value '1a' cannot be converted".
This is no regular message from my code and i can't explain where it's coming from.
Have i made a mistake in the regular expression or something else?
I have uploaded my testproject to GitHub:
Repository
The project which i mean is named "Validation_DataAnnotations".
Thanks in advance!
This is the class PropertyChangedNotification which i use for notification and validation:
public abstract class PropertyChangedNotification : INotifyPropertyChanged, IDataErrorInfo
{
#region Fields
private readonly Dictionary<string, object> _values = new Dictionary<string, object>();
#endregion
#region Protected
/// <summary>
/// Sets the value of a property.
/// </summary>
/// <typeparam name="T">The type of the property value.</typeparam>
/// <param name="propertySelector">Expression tree contains the property definition.</param>
/// <param name="value">The property value.</param>
protected void SetValue<T>(Expression<Func<T>> propertySelector, T value)
{
string propertyName = GetPropertyName(propertySelector);
SetValue<T>(propertyName, value);
}
/// <summary>
/// Sets the value of a property.
/// </summary>
/// <typeparam name="T">The type of the property value.</typeparam>
/// <param name="propertyName">The name of the property.</param>
/// <param name="value">The property value.</param>
protected void SetValue<T>(string propertyName, T value)
{
if (string.IsNullOrEmpty(propertyName))
{
throw new ArgumentException("Invalid property name", propertyName);
}
_values[propertyName] = value;
NotifyPropertyChanged(propertyName);
}
/// <summary>
/// Gets the value of a property.
/// </summary>
/// <typeparam name="T">The type of the property value.</typeparam>
/// <param name="propertySelector">Expression tree contains the property definition.</param>
/// <returns>The value of the property or default value if not exist.</returns>
protected T GetValue<T>(Expression<Func<T>> propertySelector)
{
string propertyName = GetPropertyName(propertySelector);
return GetValue<T>(propertyName);
}
/// <summary>
/// Gets the value of a property.
/// </summary>
/// <typeparam name="T">The type of the property value.</typeparam>
/// <param name="propertyName">The name of the property.</param>
/// <returns>The value of the property or default value if not exist.</returns>
protected T GetValue<T>(string propertyName)
{
if (string.IsNullOrEmpty(propertyName))
{
throw new ArgumentException("Invalid property name", propertyName);
}
object value;
if (!_values.TryGetValue(propertyName, out value))
{
value = default(T);
_values.Add(propertyName, value);
}
return (T)value;
}
/// <summary>
/// Validates current instance properties using Data Annotations.
/// </summary>
/// <param name="propertyName">This instance property to validate.</param>
/// <returns>Relevant error string on validation failure or <see cref="System.String.Empty"/> on validation success.</returns>
protected virtual string OnValidate(string propertyName)
{
if (string.IsNullOrEmpty(propertyName))
{
throw new ArgumentException("Invalid property name", propertyName);
}
string error = string.Empty;
var value = GetValue(propertyName);
var results = new List<System.ComponentModel.DataAnnotations.ValidationResult>(1);
var result = Validator.TryValidateProperty(
value,
new ValidationContext(this, null, null)
{
MemberName = propertyName
},
results);
if (!result)
{
var validationResult = results.First();
error = validationResult.ErrorMessage;
}
return error;
}
#endregion
#region Change Notification
/// <summary>
/// Raised when a property on this object has a new value.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Raises this object's PropertyChanged event.
/// </summary>
/// <param name="propertyName">The property that has a new value.</param>
protected void NotifyPropertyChanged(string propertyName)
{
this.VerifyPropertyName(propertyName);
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
var e = new PropertyChangedEventArgs(propertyName);
handler(this, e);
}
}
protected void NotifyPropertyChanged<T>(Expression<Func<T>> propertySelector)
{
var propertyChanged = PropertyChanged;
if (propertyChanged != null)
{
string propertyName = GetPropertyName(propertySelector);
propertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion // INotifyPropertyChanged Members
#region Data Validation
string IDataErrorInfo.Error
{
get
{
throw new NotSupportedException("IDataErrorInfo.Error is not supported, use IDataErrorInfo.this[propertyName] instead.");
}
}
string IDataErrorInfo.this[string propertyName]
{
get
{
return OnValidate(propertyName);
}
}
#endregion
#region Privates
private string GetPropertyName(LambdaExpression expression)
{
var memberExpression = expression.Body as MemberExpression;
if (memberExpression == null)
{
throw new InvalidOperationException();
}
return memberExpression.Member.Name;
}
private object GetValue(string propertyName)
{
object value;
if (!_values.TryGetValue(propertyName, out value))
{
var propertyDescriptor = TypeDescriptor.GetProperties(GetType()).Find(propertyName, false);
if (propertyDescriptor == null)
{
throw new ArgumentException("Invalid property name", propertyName);
}
value = propertyDescriptor.GetValue(this);
_values.Add(propertyName, value);
}
return value;
}
#endregion
#region Debugging
/// <summary>
/// Warns the developer if this object does not have
/// a public property with the specified name. This
/// method does not exist in a Release build.
/// </summary>
[Conditional("DEBUG")]
[DebuggerStepThrough]
public void VerifyPropertyName(string propertyName)
{
// Verify that the property name matches a real,
// public, instance property on this object.
if (TypeDescriptor.GetProperties(this)[propertyName] == null)
{
string msg = "Invalid property name: " + propertyName;
if (this.ThrowOnInvalidPropertyName)
throw new Exception(msg);
else
Debug.Fail(msg);
}
}
/// <summary>
/// Returns whether an exception is thrown, or if a Debug.Fail() is used
/// when an invalid property name is passed to the VerifyPropertyName method.
/// The default value is false, but subclasses used by unit tests might
/// override this property's getter to return true.
/// </summary>
protected virtual bool ThrowOnInvalidPropertyName { get; private set; }
#endregion // Debugging Aides
}
As an int property can never be set to anything else than an int value so your property setter will never be invoked if you set the Text property of the TextBox to "1a". No regular expression or data annotation in the world will solve this.
What you can do to customize the error message that appears when the WPF runtime tries to convert the value "1a" to an int before the property is set is to use a ValidationRule:
<TextBox>
<TextBox.Text>
<Binding Path="Age" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<local:StringToIntValidationRule ValidationStep="RawProposedValue"/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
...
</TextBox>
public class StringToIntValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
int i;
if (int.TryParse(value.ToString(), out i))
return new ValidationResult(true, null);
return new ValidationResult(false, "Please enter a valid integer value.");
}
}
There is a full example and more information about this available in the following blog post: https://blog.magnusmontin.net/2013/08/26/data-validation-in-wpf/
Note that it is not the responsibility of the view model to verify that its Age property is set to an int value. This is the responsibility of the control or the view.
You are binding your TextBox.TextProperty against the int-Property 'Age', thus WPF will already need to convert the string to an integer, in order to assign the value to your property.
If you want to do all the conversion yourself, you can bind to a string-Property instead.
Alternatively, you could just set ValidatesOnDataErrors=False in your binding, then non-numeric values will be silently ignored, numeric values will change the bound property.
I am trying to get a static field info into metro app and I don't find a way to do that.
I have tried:
- type.GetRuntimeField
- typeInfo.GetDeclaredField in a loop to delve into every parent types
/// <summary>
/// Gets the field info from the specified name
/// </summary>
/// <param name="type">The source type</param>
/// <param name="fieldName">The name of the field</param>
/// <returns>The field info if found, null otherwise</returns>
public static FieldInfo GetField(this Type type, string fieldName)
{
var currentType = type;
FieldInfo result = null;
while (result == null && currentType != null)
{
var typeInfo = currentType.GetTypeInfo();
result = typeInfo.GetDeclaredField(fieldName);
currentType = typeInfo.BaseType;
}
return result;
}
... am I missing something or is there anyway to get a static field on a type using reflection in metro app?....
edit:
Well, I am so sorry for those who have waste time on this question, Dependency properties defined in the framework are actualy not readonly static fields, they are static properties... As I usualy declare my dps as field, I didn't consider the fact that form example FrameworkElement.Width could be a property...
So here is the code I used to get fields and property info:
public static class TypeExtensions
{
/// <summary>
/// Gets the field info from the specified name
/// </summary>
/// <param name="type">The source type</param>
/// <param name="fieldName">The name of the field</param>
/// <returns>The field info if found, null otherwise</returns>
public static FieldInfo GetField(this Type type, string fieldName)
{
var currentType = type;
FieldInfo result = null;
while (result == null && currentType != null)
{
var typeInfo = currentType.GetTypeInfo();
result = typeInfo.GetDeclaredField(fieldName);
currentType = typeInfo.BaseType;
}
return result;
}
/// <summary>
/// Gets the property info from the specified name
/// </summary>
/// <param name="type">The source type</param>
/// <param name="propertyName">The name of the property</param>
/// <returns>The field info if found, null otherwise</returns>
public static PropertyInfo GetProperty(this Type type, string propertyName)
{
var currentType = type;
PropertyInfo result = null;
while (result == null && currentType != null)
{
var typeInfo = currentType.GetTypeInfo();
result = typeInfo.GetDeclaredProperty(propertyName);
currentType = typeInfo.BaseType;
}
return result;
}
}
public static class DependencyObjectExtensions
{
public static DependencyProperty GetDependencyProperty(this DependencyObject dependencyObject, string propertyName)
{
var dependencyPropertyName = propertyName + "Property";
var type = dependencyObject.GetType();
var fieldInfo = type.GetField(dependencyPropertyName);
if (fieldInfo == null)
{
var propertyInfo = type.GetProperty(dependencyPropertyName);
if (propertyInfo != null)
{
return propertyInfo.GetValue(dependencyObject) as DependencyProperty;
}
}
else
{
var value = fieldInfo.GetValue(dependencyObject);
return value as DependencyProperty;
}
return null;
}
}
Thanks a lot
Regards,
Charles
The database I am working with currently has a varchar field, and in my code I want to map the potential values to a enumeration like:
public enum UserStatus
{
Anonymous,
Enrolled,
SuperUser
}
At the database level for this column, there is a constrain on it where the value has to be:
ANONYMOUS
ENROLLED
SUPERUSER
Is it possible for me to do:
UserStatus.SuperUser.ToString()
And have that value be SUPERUSER, and this be consistant and not screw up down the road?
A better solution may be to take advantage of the DescriptionAttribute:
public enum UserStatus
{
[Description("ANONYMOUS")]
Anonymous,
[Description("ENROLLED")]
Enrolled,
[Description("SUPERUSER")]
SuperUser
}
Then use something like:
/// <summary>
/// Class EnumExtenions
/// </summary>
public static class EnumExtenions
{
/// <summary>
/// Gets the description.
/// </summary>
/// <param name="e">The e.</param>
/// <returns>String.</returns>
public static String GetDescription(this Enum e)
{
String enumAsString = e.ToString();
Type type = e.GetType();
MemberInfo[] members = type.GetMember(enumAsString);
if (members != null && members.Length > 0)
{
Object[] attributes = members[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attributes != null && attributes.Length > 0)
{
enumAsString = ((DescriptionAttribute)attributes[0]).Description;
}
}
return enumAsString;
}
/// <summary>
/// Gets an enum from its description.
/// </summary>
/// <typeparam name="TEnum">The type of the T enum.</typeparam>
/// <param name="description">The description.</param>
/// <returns>Matching enum value.</returns>
/// <exception cref="System.InvalidOperationException"></exception>
public static TEnum GetFromDescription<TEnum>(String description)
where TEnum : struct, IConvertible // http://stackoverflow.com/a/79903/298053
{
if (!typeof(TEnum).IsEnum)
{
throw new InvalidOperationException();
}
foreach (FieldInfo field in typeof(TEnum).GetFields())
{
DescriptionAttribute attribute = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
if (attribute != null)
{
if (attribute.Description == description)
{
return (TEnum)field.GetValue(null);
}
}
else
{
if (field.Name == description)
{
return (TEnum)field.GetValue(null);
}
}
}
return default(TEnum);
}
}
So now you're referencing UserStatus.Anonymous.GetDescription().
Of course you could always make your own DatabaseMapAttribute (or what-have-you) and create your own extension methods. Then you can kill a reference to System.ComponentModel. Completely your call.
You can't override ToString for enums, instead you can create your own Extension Method like:
public static class MyExtensions
{
public static string ToUpperString(this UserStatus userStatus)
{
return userStatus.ToString().ToUpper();// OR .ToUpperInvariant
}
}
And then call it like:
string str = UserStatus.Anonymous.ToUpperString();
Enum.ToString supports 4 different formats. I'd go for:
UserStatus.SuperUser.ToString("G").ToUpper();
"G" ensures that it will try first to get the string representation of your enum.
I have a DataAnnotationValidator that I created. I am currently trying to test it with a Required Field attribute and I can't get the IsValid property to fail when my property is null. It does work correctly when I create a number with a Range attribute that is outside of the specified Range.
public class TestEntityWithDataAnnotations
{
public Guid Id { get; set; }
[Required(ErrorMessage = "Required")]
public string Name { get; set; }
}
[TestFixture]
public class DataAnnotationValidatorTest
{
[Test]
public void Validate_ReturnsFailure_WhenPropertyValidationIsNotValid()
{
var validator = new DataAnnotationValidator();
var invalidEntity = new TestEntityWithDataAnnotations
{
Id = Guid.NewGuid()
};
var validationResult = validator.Validate(invalidEntity);
Assert.IsFalse(validationResult.IsValid);
}
}
public class DataAnnotationValidator
{
public ValidationResult Validate(object obj)
{
Type objType = obj.GetType();
var typeDescriptor = GetTypeDescriptor(obj, objType);
var validationResult = new ValidationResult();
var classValidationResult = CheckClassIsValid(obj, typeDescriptor);
if (!classValidationResult.IsValid)
{
validationResult.AddErrors(classValidationResult.Errors);
}
foreach (PropertyDescriptor propertyDescriptor in typeDescriptor.GetProperties())
{
// Loop over all of the properties on our object that have Validation Attributes
var propValidationResult = CheckPropertyIsValid(obj, propertyDescriptor);
if(!propValidationResult.IsValid)
{
validationResult.AddErrors(propValidationResult.Errors);
}
}
return validationResult;
}
/// <summary>
/// Checks to see if there are any class level validation attributes and runs them
/// </summary>
/// <returns></returns>
private static ValidationResult CheckClassIsValid(object obj, ICustomTypeDescriptor typeDescriptor)
{
var errors = typeDescriptor.GetAttributes().OfType<ValidationAttribute>()
.Where(x => !x.IsValid(obj))
.Select(x => new ValidationError(typeDescriptor.GetClassName(), x.ErrorMessage));
return new ValidationResult(errors.ToList());
}
/// <summary>
/// Checks to see if a property has any DataAnnotations that it has violated
/// </summary>
private static ValidationResult CheckPropertyIsValid(object obj, PropertyDescriptor propertyDescriptor)
{
var errors = propertyDescriptor.Attributes.OfType<ValidationAttribute>()
.Where(x => !x.IsValid(obj))
.Select(x => new ValidationError(propertyDescriptor.Name, x.ErrorMessage));
return new ValidationResult(errors.ToList());
}
/// <summary>
/// Gets the model's type descriptor. In order to support the buddy class metadata model
/// for LINQ to SQL and Entity Framework, it uses
/// <see cref="AssociatedMetadataTypeTypeDescriptionProvider"/>.
/// </summary>
/// <param name="obj">The model object</param>
/// <param name="objType">The type of the model object</param>
/// <returns>The model's type descriptor</returns>
private static ICustomTypeDescriptor GetTypeDescriptor(object obj, Type objType)
{
var provider = new AssociatedMetadataTypeTypeDescriptionProvider(objType);
return provider.GetTypeDescriptor(objType, obj);
}
}
A bit of stupidity on my part. I needed to pass the value of the property into IsValid inside of CheckPropertyIsValid instead of the whole object.
private static ValidationResult CheckPropertyIsValid(object obj, PropertyDescriptor propertyDescriptor)
{
var errors = propertyDescriptor.Attributes.OfType<ValidationAttribute>()
.Where(x => !x.IsValid(propertyDescriptor.GetValue(obj)))
.Select(x => new ValidationError(propertyDescriptor.Name, x.ErrorMessage));
return new ValidationResult(errors.ToList());
}