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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:24:58+00:00 2026-05-17T22:24:58+00:00

What would the C# code be to create a (service) method to return an

  • 0

What would the C# code be to create a (service) method to return an object (ViewModel for DDL) using AutoMapper and provide the two field names as parameters?

DDL is for a Drop Down List:

public class DDLitems {
    public string text {get;set;}
    public string value {get;set}
}

My horrible pseudo C# code idea: (yea no idea how to do this yet):

public IList<DDLobject> AutoMapDDLvalue( IList<object> source,
                                         type objectClass.textFieldName,
                                         type objectClass.valueFieldName)
{
    var EntityRepository = new EntityRepository();
    Mapper.CreateMap<source.type, DDLobject>()
          .ForMember(dest => dest.text, 
                      opt => opt.MapFrom(src => src.objectClass.textFieldName))
          .ForMember(dest => dest.value, 
                      opt => opt.MapFrom(src => src.objectClass.valueFieldName));
    return Mapper.Map<IList<source>, IList<DDLobject>>(EntityRepository.Get());
 }

I just thought that the field types would need to be converted to string. I guess we could add a generic .ToString() to each.

  • 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-17T22:24:58+00:00Added an answer on May 17, 2026 at 10:24 pm

    I have absolutely no experience with AutoMapper, but I guess the problem boils down to getting a property from an object, when you know the property name only at runtime.

    If that is the question, that’s what reflection is for 🙂

    You can get a property out of an object using this:

    public object GetProperty(object obj, string propertyName)
    {
      PropertyInfo pi = obj.GetType().GetProperty(propertyName);
      object value = pi.GetValue(o, null);
      //or object value = pi.GetGetMethod().Invoke(obj, null)
      return value;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How does the code looks that would create an object of class: string myClass
I would like to create (in code) tree of controls that would render as
I would like to create my ObjectDataProvider in my C# code behind rather than
Prior to C# generics, everyone would code collections for their business objects by creating
Developing websites are time-consuming. To improve productivity, I would code a prototype to show
How would one code the following C# code in Managed C++ void Foo() {
I thought this code would work, but the regular expression doesn't ever match the
I would like a code sample for a function that takes a tDateTime and
in my code i would like boost::shared_ptr not to call delete but call ptr->deleteMe()
Suppose I have some code that would, in theory, compile against any version of

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.