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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:22:57+00:00 2026-05-16T21:22:57+00:00

This is mostly for educational purposes. I’m trying to create the InputMapper class, which

  • 0

This is mostly for educational purposes. I’m trying to create the InputMapper class, which is used in this example:

var mapper = new InputMapper<SomeType>();
mapper.Map("some user input", someType => someType.IntProperty, "Input was not an integer");
mapper.Map("some user input", someType => someType.BoolProperty, "Input was not a bool");

SomeType someTypeInstance = mapper.CreateInstance();

My InputMapper class holds a collection of all of the mappings created using the Map() method. CreateInstance() will loop through the mappings trying to convert the user input and assign it to the property used in the lambda expression. As it loops through, it will save a collection of any FormatExceptions that are thrown.

My questions are:

  • In the InputMapper.Map() method, what should the lambda parameter type be?
  • In the InputMapper.CreateInstance() method, how do I go about trying to set the properties on the instance of T that I have created?

Thanks!

Update

Dr. Skeet asked for some more information about my intentions.

The InputMapper class will be used to assign user input to the members of any object, taking care of the conversion of the user input to the properties type. The interface of the class can be inferred from the example above.

Update 2

After some hand holding, Jon and Dan, got me there. Can you suggest improvements? Here’s what I have: http://pastebin.com/RaYG5n2h

  • 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-16T21:22:58+00:00Added an answer on May 16, 2026 at 9:22 pm

    For your first question, the Map method should probably be generic. For example:

    public class InputMapper<TSource> where TSource : new()
    {
        public void Map<TResult>(string input,
                                 Expression<Func<TSource, TResult>> projection,
                                 string text)
        {
            ...
        }
    }
    

    Now it’s worth noting that your lambda expressions represent the property getters, but presumably you want to call the property setters. That means your code won’t be compile-time safe – there could be a mapped property which is read-only, for example. Also, there’s nothing to restrict the lambda expression to only refer to a property. It could do anything. You’d have to put in execution-time guards against that.

    Once you have found the property setters though, you just need to create an instance of TSource using new TSource() (note the constructor constraint on TSource above) and then perform the appropriate conversions and call the property setters.

    Without more details on what you’re trying to do, I’m afraid it’s not terribly easy to give a more detailed answer.

    EDIT: The code to work out the property would look something like this:

    var memberExpression = projection.Body as MemberExpression;
    if (memberExpression == null)
    {
        throw new ArgumentException("Lambda was not a member access");
    }
    var propertyInfo = memberExpression.Member as PropertyInfo;
    if (propertyInfo == null)
    {
        throw new ArgumentException("Lambda was not a property access");
    }
    if (projection.Parameters.Count != 1 ||
        projection.Parameters[0] != memberExpression.Expression)
    {
        throw new ArgumentException("Property was not invoked on parameter");
    }
    if (!propertyInfo.CanWrite)
    {
        throw new ArgumentException("Property is read-only");
    }
    // Now we've got a PropertyInfo which we should be able to write to - 
    // although the setter may be private. (Add more tests for that...)
    // Stash propertyInfo appropriately, and use PropertyInfo.SetValue when you 
    // need to.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am mostly following this page: http://tomcat.apache.org/tomcat-6.0-doc/ssl-howto.html I used this command to create the
I think this is mostly because I'm new to PHP OOP, but I have
I'm starting on a new iPhone project, and this application mostly relies on MySQL.
I'm trying to write this code using mostly arrays. I'm trying to get Correct
This is mostly just out of curiosity, and is potentially a silly question. :)
This is mostly a data warehouse philosophy question. My project involves an Oracle forms
This question is mostly academic. I ask out of curiosity, not because this poses
I have this thing working mostly. What I don't get is, if I have
This is the situation : I mostly program C# and have written types in
This is the query. Im mostly interested if there is a better way to

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.