0
我有此視圖模型內的表達類型:如何動態地設置的方法
public class BankAccountViewModel : IValidator<BankAccountViewModel>
{
public BankAccount BankAccount { get; set; }
public void ConfigureValidation(ValidationConfiguration<StockOnHandViewModel> config)
{
config.For(m => m.BankAccount.AccountHolder);
}
}
所以類是:
public class BankAccount
{
public string AccountHolder { get; set; }
public List<Transfer> Transfers { get; set; }
}
public class Transfer
{
public int Amount { get; set; }
}
然後,我有用於驗證的一些擴展
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);
}
}
所以我能夠爲AccountHolder調用For方法config.For(m => m.BankAccount.AccountHolder);
這很酷。它按預期發送表達式。
對於列表項轉移變得更加困難。
在傳輸中,我可能希望通過表達式發送每次傳輸的金額。所以,如果我有兩個轉變我想送:
m => m.BankAccount.Transfers[0].Amount
m => m.BankAccount.Transfers[1].Amount
我知道我能做到這一點是這樣的:
for(int i=0; i < BankAccount.Transfers.Count; i++)
{
config.For(m => m.BankAccount.Transfers[i].Amount);
}
但是我不想做這個列表項的所有時間。
我真的想或許有一個不同的For
方法列表項目,我可以如何調用,它會爲我做這個。
我想也許的是這樣的:
public static PropertyRuleSet<TModel, TProperty> For<TModel, TProperty>(
this ValidationConfiguration<TModel> validationConfiguration,
Expression<Func<TModel, TProperty>> accessor, int count)
{
...
}
下,您也許把它想:
config.For(m => m.BankAccount.Transfers[i].Amount, BankAccount.Transfers.Count);
然而,這不會起作用,因爲我不知道如何發送表達部分沒有我,然後填充它爲每個列表項目後。也不知道該怎麼辦在方法
任何人都知道該怎麼辦?
不知道你將如何去選擇關閉BankAccount,我想你會做轉移? – AnonyMouse 2012-08-14 23:13:02
@AnonyMouse你是對的...修復。 – Hogan 2012-08-14 23:16:59