Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8846739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:04:52+00:00 2026-06-14T12:04:52+00:00

I have a set of classes I would like to map to with Automapper.

  • 0

I have a set of classes I would like to map to with Automapper. However, every class has a constructor parameter. This parameter is of the same type for all members, but I do not know the value to supply until the point I want to do my mapping.

I have found the ConstructUsing method, but that requires me to specify the parameter value at time of configuration. I would prefer to do it at time of Mapping to avoid needing to create a separate MappingEngine for every instance of the parameter. I have found the Map overload that maps to an already created destination object. This is helpful, but it does not work for lists or object graphs.

Essentially, I am looking for something like this resolve method from Autofac, only applied to Automapper’s Map method.

Resolve<IFoo>( new TypedParameter( typeof( IService ), m_service) );
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:04:53+00:00Added an answer on June 14, 2026 at 12:04 pm

    Reading through the Automapper source code, I found a workable solution which I described below.

    First, you need to specify you want to use a Service Locator for construction.

    IConfiguration configuration = ...;
    configuration.CreateMap<Data.Entity.Address, Address>().ConstructUsingServiceLocator();
    

    Then when calling map, you specify a specific service locator using the opts parameter

    // Use DI container or manually construct function
    // that provides construction using the parameter value specified in question.
    // 
    // This implementation is somewhat Primitive, 
    // but will work if parameter specified is always the only parameter
    Func<Type, object> constructingFunction =
        type => return Activator.CreateInstance( type, new object[] { s_service } );
    
    mappingEngine.Map<Data.Entity.Address, Address>(
        source, opts: options => options.ConstructServicesUsing( constructingFunction );
    

    The “ServiceLocator” indicated by constructingFunction above takes precedent over the function provided to IConfiguration.ConstructServicesUsing(...)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following classes which I would like to map using NHibernate class
I would like to have a mapped class named Message. This class should include
I have a class which I would like to map as a component onto
I have two classes set up as follows: class Point { protected: double coords[3];
I have classes which have automatic properties only like public customerName {get; set;}. They
I have two classes: public class MultilingualString { public int Id { get; set;
I would like to deserialise Scala case classes that have been serialised using lift-json.
I have the following model class MyClass { id someRandomString } I would like
I have the following classes: public class Folder{ private Set<Documents> documents; private Set<Clip> clips;
I have two classes: class Foo{ public int FooId { get; set; } ...

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.