如何将 n no.of 个不同的通用参数传递给一个方法
How to pass n no.of different generic parameters to a method
目前我有以下方法,它将两个 linq 表达式作为参数并使用它们进行一些处理。
public RecordConfiguration<TStage, TKey> EnsureUnique<TProperty1, TProperty2>(Expression<Func<TStage, TProperty1>> propertyAccessor1, Expression<Func<TStage, TProperty2>> propertyAccessor2)
{
var columnSet = new ColumnSet<TStage>();
columnSet.AddAt(0, propertyAccessor1);
columnSet.AddAt(1, propertyAccessor2);
Expression<Func<IEntitySetCollection, short, IValidator>> uniquenessValidatorCreator = (entitySetCollection, stagedEntitySetId) =>
new UniquenessValidator<TStage, TKey>(entitySetCollection,stagedEntitySetId, columnSet);
RecordValidatorCreators.Add(uniquenessValidatorCreator);
return this;
}
我可以传递 n 个不同类型的参数,而不是只限制两个参数吗?
你可以使用 params operator
public RecordConfiguration<TStage, TKey> EnsureUnique>(params Expression<Func<TStage, object>>[] propertyAccessors)
{
// ...
properyAccessors.Select((val,index)=>new{ val,index})
.ForEach(i =>columnSet.AddAt(i.index, i.val));
// ...
}
目前我有以下方法,它将两个 linq 表达式作为参数并使用它们进行一些处理。
public RecordConfiguration<TStage, TKey> EnsureUnique<TProperty1, TProperty2>(Expression<Func<TStage, TProperty1>> propertyAccessor1, Expression<Func<TStage, TProperty2>> propertyAccessor2)
{
var columnSet = new ColumnSet<TStage>();
columnSet.AddAt(0, propertyAccessor1);
columnSet.AddAt(1, propertyAccessor2);
Expression<Func<IEntitySetCollection, short, IValidator>> uniquenessValidatorCreator = (entitySetCollection, stagedEntitySetId) =>
new UniquenessValidator<TStage, TKey>(entitySetCollection,stagedEntitySetId, columnSet);
RecordValidatorCreators.Add(uniquenessValidatorCreator);
return this;
}
我可以传递 n 个不同类型的参数,而不是只限制两个参数吗?
你可以使用 params operator
public RecordConfiguration<TStage, TKey> EnsureUnique>(params Expression<Func<TStage, object>>[] propertyAccessors)
{
// ...
properyAccessors.Select((val,index)=>new{ val,index})
.ForEach(i =>columnSet.AddAt(i.index, i.val));
// ...
}