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 5956263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:13:55+00:00 2026-05-22T18:13:55+00:00

I have a custom type converter that converts UTC DateTime properties to a company’s

  • 0

I have a custom type converter that converts UTC DateTime properties to a company’s local time (talked about here: Globally apply value resolver with AutoMapper).

I’d now like to only have this converter do its thing if the property on the view model is tagged with a custom DisplayInLocalTime attribute.

Inside the type converter, if I implement the raw ITypeConvert<TSource, TDestination> interface, I can check if the destination view model property being converted has the attribute:

public class LocalizedDateTimeConverter : ITypeConverter<DateTime, DateTime> 
{
    public DateTime Convert(ResolutionContext context)
    {
        var shouldConvert = context.Parent.DestinationType
                                .GetProperty(context.MemberName)
                                .GetCustomAttributes(false)[0].GetType() == typeof(DisplayInLocalTimeAttribute);
        if (shouldConvert) {
            // rest of the conversion logic...
        }
    }
}

So this code works just fine (obviously there’s more error checking and variables in there for readability).

My questions:

  1. Is this the correct way to go about this? I haven’t found anything Googling around or spelunking through the AutoMapper code base.
  2. How would I unit test this? I can set the parent destination type on the ResolutionContext being passed in with a bit of funkiness, but can’t set the member name as all implementors of IMemberAccessor are internal to AutoMapper. This, and the fact that it’s super ugly to setup, makes me this isn’t really supported or I’m going about it all wrong.

I’m using the latest TeamCity build of AutoMapper, BTW.

  • 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-05-22T18:13:56+00:00Added an answer on May 22, 2026 at 6:13 pm

    Don’t unit test this, use an integration test. Just write a mapping test that actually calls AutoMapper, verifying that whatever use case this type converter is there to support works from the outside.

    As a general rule, unit tests on extension points of someone else’s API don’t have as much value to me. Instead, I try to go through the front door and make sure that I’ve configured the extension point correctly as well.

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

Sidebar

Related Questions

Using NH 2.0, I have a custom type. It is composed of four properties,
I have a JUnit test that tests adding Strings to a Dictionary custom type.
I have a custom converter that has a DefaultText property. All my converter does
I have a custom type that I created, which should be persisted in a
Is it possible to have some kind of type converter that allows Sync Framework
I have a DateTime object that I need to print in a custom gridlike
I have a VGroup in my application that contains several custom components of type
I have some custom type: [RdfSerializable] public class Item { [RdfProperty(true)] public string Name
I use NHibernate and have a custom type for encrypting passwords in the database
I have a custom content type with custom fields. I'm trying to create a

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.