views:

347

answers:

1

I'm trying to create a custom mapping with AutoMapper, but I can't use 3.0 syntax with lambdas. How would one convert this 3.0 code into 2.0 ?

Mapper.CreateMap<MyClass, MyDto>()
 .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.CompanyName))

Edit:

Since there was no better solution, we are now using vs2008 on one workstation to make these mappings and build a dll. I hope it won't be long until we upgrade to vs2010.

+2  A: 

EDIT: Jimmy has mentioned in the comments that AutoMapper uses expression trees. That doesn't immediately rule out the possibility of using .NET 2.0 (you can use Mono's implementation of System.Core), but if you can't even using C# 3 you'll have to construct all the expression trees manually, which it going to be a huge pain.

Are you absolutely prohibited from using .NET 3.5 and C# 3?

Jon Skeet
I'm working with Visual Studio 2005
Morri
It used to be possible to use LINQ in VS2005 - see http://www.c-sharpcorner.com/UploadFile/nsatheeshk/Linq06302006030119AM/Linq.aspx (not sure if this is still supported though)
Steve Haigh
I'm getting stuck on delegate(Bar opt) { return opt.MapFrom(... What is Bar, and howcome it has .MapFrom method?
Morri
@Morri: Bar would be whatever the type of "opt" is in the original lambda expression.
Jon Skeet
Morri
@Morri: I suggest you get the code working in VS2008 (e.g. in the free Express version) which will let you experiment with it - and hover over the parameter to see its type.
Jon Skeet
@JonAutoMapper uses expressions for strongly-typed reflection, so you can't go the anonymous delegate route. The ForMember method accepts an Expression<Func<TSource, TMember>>, not Func<TSource, TMember>.
Jimmy Bogard
@Jimmy: Ah. That's going to make life considerably harder...
Jon Skeet