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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:43:58+00:00 2026-05-20T02:43:58+00:00

For a SO question i quite recently wrote a generic extension method that should

  • 0

For a SO question i quite recently wrote a generic extension method that should load an object from another, i.e. assign all the properties of the source to the target and do so recursively if the property is a reference-type. I got quite far using reflection but i hit a problem when it came to the types of properties that are reference-types, here is my first approach:

First approach:

    public static void Load<T>(this T target, T source, bool deep)
    {
        foreach (PropertyInfo property in typeof(T).GetProperties())
        {
            if (property.CanWrite && property.CanRead)
            {
                if (!deep || property.PropertyType.IsPrimitive || property.PropertyType == typeof(String))
                {
                    property.SetValue(target, property.GetValue(source, null), null);
                }
                else
                {
                    property.GetValue(target, null).Load(property.GetValue(source, null), deep);
                }
            }
        }
    }

The problem here is that PropertyInfo.GetValue returns an object, subsequently T will equal object in the recursive call and i can no longer get the properties that the object actually has.

I conceived of a workaround which requires you to pass the Type explicitly, which is quite redundant since in theory it should be possible to manage without:

    public static void Load<T>(this T target, Type type, T source, bool deep)
    {
        foreach (PropertyInfo property in type.GetProperties())
        {
            if (property.CanWrite && property.CanRead)
            {
                if (!deep || property.PropertyType.IsPrimitive || property.PropertyType == typeof(String))
                {
                    property.SetValue(target, property.GetValue(source, null), null);
                }
                else
                {
                    object targetPropertyReference = property.GetValue(target, null);
                    targetPropertyReference.Load(targetPropertyReference.GetType(), property.GetValue(source, null), deep);
                }
            }
        }
    }

I also tried using dynamic targetPropertyReference but then i get a runtime exception that the Load method cannot be found, it is infuriating.

Other than that Convert.ChangeType handily returns a bloody object too and i cannot seem to otherwise cast the object to what it is. Of course i have looked for an answer to this on the net but i have been unsuccessful so far.

  • 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-20T02:43:59+00:00Added an answer on May 20, 2026 at 2:43 am

    I haven’t looked at the full code or considered the full consequences of implementing such a scheme (EDIT: what will happen if there are cyclic references?), but I can give you two short fixes for your specific problem:

    Option 1: Dodge the issue

    Use the run-time type of the provided “normal” argument, rather than the type-argument.

    Replace:

    typeof(T).GetProperties()
    

    with:

    // You need null-checks around this:
    // you can't realistically continue if target is null anyway.
    target.GetType().GetProperties()
    

    This of course introduces a minor semantic change in your code, in that it prevents scenarios when one would would want to pass a Giraffe but only want Animal properties copied over. It will also blow up if source and target are of different run-time types (having different properties), but you can work around that without too much trouble (e.g. find the deepest common base-type and use its properties instead).


    Option 2: More reflection / dynamic

    The other solution of course, is to use MakeGenericMethod to allow the type-argument to be provided “dynamically”. This maintains the original semantics of the code (untested):

    typeof(MyClass).GetMethod("Load")
                   .MakeGenericMethod(property.PropertyType)
                   .Invoke(null, property.GetValue(target, null), 
                                 property.GetValue(source, null), deep);
    

    Btw, there’s no reason you can’t use dynamic to accomplish something very similar. I suspect you’re trying to do the call “as” an extension-method, but that won’t work. Just try the normal “static-method” syntax.

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

Sidebar

Related Questions

The question is quite generic. What are the points that should be kept in
I think this question is quite common. Recently I have got a requirement that
Recently I posted a answer to a question that I thought was quite an
My question is quite simple.. I need to convert an Element object into an
My question is quite simple. Is it possible to inactivate all toasts of an
My question is quite simple and with the SharpSvn Api, it should be easy
All of my projects include very similar tasks and quite recently I've been thinking
I use a cluster of about 30 machines that have all recently been reconfigured
Quite recently, I asked a question about debugging a seg fault: What are some
I've been working with UDP sockets quite a lot recently. I've read that UDP

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.