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

  • SEARCH
  • Home
  • 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 1009207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:53:21+00:00 2026-05-16T08:53:21+00:00

If I have some classes that implement the same interface, so they all contain

  • 0

If I have some classes that implement the same interface, so they all contain the same property. Is there a way to add a formatter to these properties? I only found the possibility to add a formatter to a specific property type.

Here’s some code that should clarify what I mean:

public interface ITaggable
{
    IList<string> Tags { get; set; }
}

public class Post : ITaggable
{
    public IList<string> Tags { get; set; }
    public IList<string> Categories { get; set; }
    ...
}

public class Page : ITaggable
{
    public IList<string> Tags { get; set; }
    ....
}

I’d like to map these to view models that look something like this:

public class PostViewModel
{
    public string Tags { get; set; }
    public IList<string> Categories { get; set; }
    ...
}

public class PageViewModel
{
    public string Tags { get; set; }
    ...
}

If a Post has the tags “foo” and “bar”, then the PostViewModel‘s Tags property should contain the string “foo, bar”. Categories should remain an IList<string>.

I could accomplish this by creating a custom formatter and then add it on every mapping, like this:

protected override void Configure()
{
    CreateMap<Post, PostViewModel>()
        .ForMember(x => x.Tags, opt => opt.AddFormatter<TagsFormatter>());
    CreateMap<Page, PageViewModel>()
        .ForMember(x => x.Tags, opt => opt.AddFormatter<TagsFormatter>());
}

But I’d like to do something like this (this code doesn’t work 😉

protected override void Configure()
{
    ForSourceType<ITaggable>()
        .ForMember(x => x.Tags, opt => opt.AddFormatter<TagsFormatter>());

    CreateMap<Post, PostViewModel>();
    CreateMap<Page, PageViewModel>();
}
  • 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-16T08:53:21+00:00Added an answer on May 16, 2026 at 8:53 am

    It’s not exactly an answer to the question I was asking, but a solution to the problem:

    I built a TagsFormatter that checks the name of the property for “Tags”:

    public class TagsFormatter : IValueFormatter
    {
        public string FormatValue(ResolutionContext context)
        {
            if (context.MemberName.Equals("Tags", StringComparison.InvariantCultureIgnoreCase))
            {
                var tags = context.SourceValue as IList<string>;
                if (tags != null)
                    return String.Join(", ", tags);
            }
            return context.SourceValue.ToString();
        }
    }
    

    In the configuration I can then register this Formatter for all mappings:

    public class ViewModelProfile : AutoMapper.Profile
    {
        protected override void Configure()
        {
            ForSourceType<IList<string>>().AddFormatter<TagsFormatter>();
    
            CreateMap<Post, PostViewModel>();
            CreateMap<Page, PageViewModel>();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have developed some classes with similar behavior, they all implement the same interface.
I have some classes that all implement the same Java interface, which essentially allows
The situation is this: You have two classes that both implement the same interface
I have some classes that are used for Styling and all of them display
I have some classes that represent immutable objects (Quantity, Price, Probability). Is there some
I have an interface, and some classes that inherit from it. public interface IFoo
Sometimes we have several classes that have some methods with the same signature, but
I have a set of classes that implement a particular interface and I have
I have implemented PullRefreshTableViewController with some classes that take no delegates, and it works
I have some classes that, for one reason or another, cannot be or need

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.