AutoMapper: How to Initialize BindingFlags?

2019-09-04 03:24发布

问题:

When I asked this question about AutoMapper and protected properties, I received a response from Jimmy Bogard (AutoMapper co-creator). He suggested that we can initialize the BindingFlags using the code that I pasted below. The problem is, I get the following error and don't know how to fix it:

"AutoMapper.IConfiguration does not contain a definition for 'BindingFlags' and no extension method..."

Here's the code:

Mapper.Initialize(cfg =>
            {
                cfg.BindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
                cfg.CreateMap<PolicyDetail, Policy>();
            });

UPDATE:

I didn't have AutoMapper 3.3.0. Once I updated, this code worked. Thanks to Gert Arnold.

回答1:

Seems like it should work, looking at the source code, IConfiguration inherits from IProfileExpression, which inherits from IMappingOptions, which contains a public property with the following definition:

BindingFlags BindingFlags { get; set; }

See AutoMapper > src > UnitTests > BindingFlagsConfiguration.cs for an example to compare to what you have.



标签: c# AutoMapper