I have a custom class (call i Field) that that implements several properties. One of the properties is MaximumLength that spcifies the maximum length that the value can be. The Value property is an object so i can be set to string, int, double, etc. Then I have a class that has multiple properties of type Field in it. All the Field properties are initialized in the constructor and only the Field.Value property can be written to. What I want to do is throw an error if the an attempt is made to set the Field.Value to a value that is too long for the field and implement INotifyPropertyChanged. My problem is the Value property is a member of the generic Field class and I do not know how to get the name of the property inside that class.
an example:
public class Customer
{
private Field _firstName = new Field(typeof(string), 20);
public Field FirstName
{
get
{
return _firstName;
}
}
}
public class Field
{
private Type _type;
private int _maximumLength;
object _value;
public Field(Type type, int maximumLength)
{
_type = type;
_maximumLength = maximumLength;
}
public Object Value
{
get
{
return _value;
}
set
{
if (value.ToString().Length > _maximumLength)
{
throw(string.Format("{0} cannot exceed {1} in length.", property name, _maximumValue);
}
else
{
_value = value;
OnPropertyChanged(property name);
}
}
}
}
Hopefully that is clear enough.