Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an extension method be added to a class property to get the value of an attribute associated with the property?

I have several classes with attributes assigned to them. The one I'm mostly interested in is the FieldLength.MaxLength value.

/// <summary>
/// Users
/// </summary>
[Table(Schema = "dbo", Name = "users"), Serializable]
public partial class Users
{

    /// <summary>
    /// Last name
    /// </summary>
    [Column(Name = "last_name", SqlDbType = SqlDbType.VarChar)]
    private string _LastName;
    [FieldLength(MaxLength=25), FieldNullable(IsNullable=false)]
    public string LastName
    {
        set { _LastName = value; }
        get { return _LastName; }
    }

}

I need to know if it's possible to write some kind of extension method for the properties in my class to return the MaxLength value of the FieldLength attribute?

For instance. I'd like to be able to write something like the following…

Users user = new Users();
int lastNameMaxLength = user.LastName.MaxLength();
like image 890
Rob de Villiers Avatar asked Oct 10 '22 06:10

Rob de Villiers


1 Answers

No, this is not possible. You could add an extension method on Users though:

public static int LastNameMaxLength(this Users user) {
    // get by reflection, return
}
like image 83
jason Avatar answered Oct 12 '22 23:10

jason