yoyobroccoli
yoyobroccoli

Reputation: 33

IMemberConfigurationExpression failing when upgrade AutoMapper from v3 to v6

I'm trying to update our code from aotumapper v3 to v6 and having trouble with one helper method that is using IMemberConfigurationExpression.

    private void TreatEmptyStringsAsNull<TSource>(IMemberConfigurationExpression<TSource> expression)
    {

        expression.Condition(ctx => ctx.SourceType != typeof(string) || (string)ctx.SourceValue != string.Empty); 
    }

And this method is called by: config.CreateMap().ForAllMembers(TreatEmptyStringsAsNull);

The error message I received is that "using generic type IMemberConfigurationExpression requires three arguments.

My attempted fix:

 private void TreatEmptyStringsAsNull<TSource, TDestination, TMember>(IMemberConfigurationExpression<TSource, TDestination, TMember> expression)
    {

        expression.Condition(ctx => ctx.SourceType != typeof(string) || (string)ctx.SourceValue != string.Empty); 
    }

But then I got a new error "TSource does not contain a definition for 'SourceType' and no extension method 'SourceType' accepting a first argument of type 'TSource' could be found."

How should I update this helper method to make it work?

#Update: I did the following changes which no longer cause any error:

    private static void TreatEmptyStringsAsNull<TSource, TDestination, TMember>(IMemberConfigurationExpression<TSource,TDestination, TMember> expression)
    {
        expression.Condition(ctx => ctx.GetType() != typeof(string) || ctx.ToString() != string.Empty); 
    }

Upvotes: 1

Views: 1635

Answers (1)

yoyobroccoli
yoyobroccoli

Reputation: 33

private static void TreatEmptyStringsAsNull<TSource, TDestination, TMember>(IMemberConfigurationExpression<TSource,TDestination, TMember> expression)
{
    expression.Condition(ctx => ctx.GetType() != typeof(string) || ctx.ToString() != string.Empty); 
}

This worked.

Upvotes: 1

Related Questions