简体   繁体   中英

How to dynamically set an expression type within a method

I have this view model:

    public class BankAccountViewModel : IValidator<BankAccountViewModel>
    {
      public BankAccount BankAccount { get; set; }

public void ConfigureValidation(ValidationConfiguration<StockOnHandViewModel> config)
        {
            config.For(m => m.BankAccount.AccountHolder);

        }
    }

so the class is:

public class BankAccount
{
  public string AccountHolder { get; set; }
  public List<Transfer> Transfers { get; set; }
}

public class Transfer
{
  public int Amount { get; set; }
}

Then I have some extensions for validation

public static class ValidationExtensions
    {
  public static PropertyRuleSet<TModel, TProperty> For<TModel, TProperty>(
            this ValidationConfiguration<TModel> validationConfiguration,
            Expression<Func<TModel, TProperty>> accessor)
        {
            return validationConfiguration.Add(accessor);
        }
}

So I was able to call the For method for AccountHolder config.For(m => m.BankAccount.AccountHolder);

So that's cool. It sends through the expression as expected.

It becomes a little more difficult for the list items Transfers.

In transfers I might want to send through the expression for the amount of each transfer. So if I had two transfers I would want to send:

m => m.BankAccount.Transfers[0].Amount
m => m.BankAccount.Transfers[1].Amount

I know I could do it this way:

for(int i=0; i < BankAccount.Transfers.Count; i++)
            {
                config.For(m => m.BankAccount.Transfers[i].Amount);
            }

However I don't want to be doing this for list items all the time.

I really want to maybe have a different For method for list items that I could some how call and it would do this for me.

I was thinking maybe of something like:

public static PropertyRuleSet<TModel, TProperty> For<TModel, TProperty>(
            this ValidationConfiguration<TModel> validationConfiguration,
            Expression<Func<TModel, TProperty>> accessor, int count)
        {
...
}

where you maybe call it like:

config.For(m => m.BankAccount.Transfers[i].Amount, BankAccount.Transfers.Count);

However this won't work because I'm not sure how to send the expression part without the i and then populate it for each list item later. Also not sure what to do in the method

Anyone know what to do here?

Having it return a list seems linqy:

public static PropertyRuleSet<TModel, TProperty> For<TModel, TProperty>(
            this ValidationConfiguration<TModel> validationConfiguration,
            Expression<Func<TModel,IEnumerator<TProperty>>> accessor)
        {
...
}

and then this:

config.For(m => m.BankAccount.Transfers.Select(t => t.Amount));

I have not tested it so it might have a typo.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM