Sri Harsha
Sri Harsha

Reputation: 29

How to pass n no.of different generic parameters to a method

Currently I have below method which takes two linq expressions as parameters and do some process using them.

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;
    }

Instead restricting to just two parameters, can I pass n number of parameters which are of different type like above two?

Upvotes: 0

Views: 54

Answers (1)

Michael Mairegger
Michael Mairegger

Reputation: 7301

ou can take use of 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));
    // ...
}

Upvotes: 3

Related Questions