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

The Archive Base Latest Questions

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

This is a theoretical question, I’ve already got a solution to my problem that

  • 0

This is a theoretical question, I’ve already got a solution to my problem that took me down a different path, but I think the question is still potentially interesting.

Can I pass object properties as delegates in the same way I can with methods? For instance:

Let’s say I’ve got a data reader loaded up with data, and each field’s value needs to be passed into properties of differing types having been checked for DBNull. If attempting to get a single field, I might write something like:

if(!rdr["field1"].Equals(DBNull.Value)) myClass.Property1 = rdr["field1"];

But if I’ve got say 100 fields, that becomes unwieldy very quickly. There’s a couple of ways that a call to do this might look nice:

myClass.Property = GetDefaultOrValue<string>(rdr["field1"]); //Which incidentally is the route I took

Which might also look nice as an extension method:

myClass.Property = rdr["field1"].GetDefaultOrValue<string>();

Or:

SetPropertyFromDbValue<string>(myClass.Property1, rdr["field1"]); //Which is the one that I'm interested in on this theoretical level

In the second instance, the property would need to be passed as a delegate in order to set it.

So the question is in two parts:

  1. Is this possible?
  2. What would that look like?

[As this is only theoretical, answers in VB or C# are equally acceptable to me]

Edit: There’s some slick answers here. Thanks all.

  • 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-16T02:57:34+00:00Added an answer on May 16, 2026 at 2:57 am

    (Adding a second answer because it’s on a completely different approach)

    To address your original problem, which is more about wanting a nice API for mapping named values in a datareader to properties on your object, consider System.ComponentModel.TypeDescriptor – an often overlooked alternative to doing reflective dirtywork yourself.

    Here’s a useful snippet:

    var properties = TypeDescriptor.GetProperties(myObject)
        .Cast<PropertyDescriptor>()
        .ToDictionary(pr => pr.Name);
    

    That creates a dictionary of the propertydescriptors of your object.

    Now I can do this:

    properties["Property1"].SetValue(myObject, rdr["item1"]);
    

    PropertyDescriptor‘s SetValue method (unlike System.Reflection.PropertyInfo‘s equivalent) will do type conversion for you – parse strings as ints, and so on.

    What’s useful about this is one can imagine an attribute-driven approach to iterating through that properties collection (PropertyDescriptor has an Attributes property to allow you to get any custom attributes that were added to the property) figuring out which value in the datareader to use; or having a method that receives a dictionary of propertyname – columnname mappings which iterates through and performs all those sets for you.

    I suspect an approach like this may give you the API shortcut you need in a way that lambda-expression reflective trickery – in this case – won’t.

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

Sidebar

Related Questions

This is a theoretical question as I don't have an actual problem, but I
This is more of a theoretical question than an actual problem I have. If
This is a theoretical question, so expect that many details here are not computable
This is a more theoretical question about macros (I think). I know macros take
this is more a theoretical question i asked myself. I remembered that BinarySearch of
This is just a theoretical question. It was born from a real problem in
This is more a theoretical question than a practical one, but given I undestand
As I can't post code I'm asking this as a theoretical question, but giving
This is more of a theoretical question. When I run a PHP script that
This is more of a theoretical question rather than language specific, but I'm mostly

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.