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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:09:03+00:00 2026-06-09T06:09:03+00:00

Suppose I get data from a service (that I can’t control) as: public class

  • 0

Suppose I get data from a service (that I can’t control) as:

public class Data
{
    // an array of column names
    public string[] ColumnNames { get; set; }

    // an array of rows that contain arrays of strings as column values
    public string[][] Rows { get; get; }
}

and on the middle tier I would like to map/translate this to an IEnumerable<Entity> where column names in Data may be represented as properties in my Entity class. I said may because I may not need all the data returned by the service but just some of it.

Transformation

This is an abstraction of an algorithm that would do the translation:

  1. create an IDictionary<string, int> of ColumnNames so I can easily map individual column names to array indices in individual rows.
  2. use reflection to examine my Entity properties’ names so I’m able to match them with column names
  3. iterate through Data.Rows and create my Entity objects and populate properties according to mapping done in #1. Likely using reflection and SetValue on properties to set them.

Optimisation

Upper algorithm would of course work, but I think that because it uses reflection it should do some caching and possibly some on the fly compilation, that could speed things up considerably.

When steps 1 and 2 are done, we could actually generate a method that takes an array of strings and instantiates my entities using indices directly and compile it and cache it for future reuse.

I’m usually getting a page of results, so subsequent requests would reuse the same compiled method.

Additional fact

This is not imperative to the question (and answers) but I also created two attributes that help with column-to-property mapping when these don’t match in names. I created the most obvious MapNameAttribute (that takes a string and optionally also enable case sensitivity) and IgnoreMappingAttribute for properties on my Entity that shouldn’t map to any data. But these attributes are read in step 2 of the upper algorithm so property names are collected and renamed according to this declarative metadata so they match column names.

Question

What is the best and easiest way to generate and compile such a method? Lambda expressions? CSharpCodeProvider class?

Do you maybe have an example of generated and compiled code that does a similar thing? I guess that mappings are a rather common scenario.

Note: In the meantime I will be examining PetaPoco (and maybe also Massive) because afaik they both do compilation and caching on the fly exactly for mapping purposes.

  • 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-09T06:09:05+00:00Added an answer on June 9, 2026 at 6:09 am

    Suggestion: obtain FastMember from NuGet

    Then just use:

    var accessor = TypeAccessor.Create(typeof(Entity));
    

    Then just in your loop, when you have found the memberName and newValue for the current iteration:

    accessor[obj, memberName] = newValue;
    

    This is designed to do what you are asking; internally, it maintains a set of types if has seen before. When a new type is seen, it creates a new subclass of TypeAccessor on-the-fly (via TypeBuilder) and caches it. Each unique TypeAccessor is aware of the properties for that type, and basically just acts like a:

    switch(memberName) {
        case "Foo": obj.Foo = (int)newValue;
        case "Bar": obj.Bar = (string)newValue;
        // etc
    }
    

    Because this is cached, you only pay any cost (and not really a big cost) the first time it ever sees your type; the rest of the time, it is free. Because it uses ILGenerator directly, it also avoids any unnecessary abstraction, for example via Expression or CodeDom, so it is about as fast as it can be.

    (I should also clarify that for dynamic types, i.e. types that implement IDynamicMetaObjectProvider, it can use a single instance to support every object).


    Additional:

    What you could do is: take the existing FastMember code, and edit it to process MapNameAttribute and IgnoreMappingAttribute during WriteGetter and WriteSetter; then all the voodoo happens on your data names, rather than the member names.

    This would mean changing the lines:

    il.Emit(OpCodes.Ldstr, prop.Name);
    

    and

    il.Emit(OpCodes.Ldstr, field.Name);
    

    in both WriteGetter and WriteSetter, and doing a continue at the start of the foreach loops if it should be ignored.

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

Sidebar

Related Questions

Suppose I have: class Foo { public String Bar { get; set; } }
I have a page with a datatable that reads data from a service class.
So i have a query that is suppose to get me data from two
I am supposed to get some XML data from the server say, <?xml><a></a><?xml><a></a><?xml><a></a><?xml><a></a> Suppose
I have one simple app that suppose to get information from user and send
I have a listview that's populated by rows that get their data from a
A quick question regarding the java.lang.VerifyError exception. Suppose I get an error that looks
Suppose you want to get a record from database which returns a large amount
Suppose I have these domain objects: DataPoint ========= public int Id {get;set;} public int
Suppose I read data from SerialPort whenever there is 100 bytes available or else

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.