jeudi 16 novembre 2017

Is it possible to get the name of a property when accessing it off an instance of an object? [duplicate]

This question already has an answer here:

public class WzReferenceCountryValidator : AbstractValidator<IWzReferenceCountryFields>
{
    /// <summary>
    ///     Class constructor.
    /// </summary>
    public WzReferenceCountryValidator()
    {
        //Get an instance of the table model so we can get at the data annotations
        Reference_CountryTableModel tableModel = new Reference_CountryTableModel();

        RuleSet("Fields", () =>
                          {
                              RuleFor(aP => aP.SomeField).NotNull()
                                                    .NotEmpty()
                                                    .Length(tableModel.GetAttributeFrom<MinLengthAttribute>("SomeField").Length, tableModel.GetAttributeFrom<MaxLengthAttribute>("SomeField").Length);
                          });
    }
}

As you can see I am hard coding the "Name" in my call. Is there any way to actually get the name of the field from the instance. So say something lile

tableModel.Name.GetFieldName() ==> "SomeField"

so not the value of tableModel.SomeField but actually its name.





Aucun commentaire:

Enregistrer un commentaire