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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:17:53+00:00 2026-05-17T23:17:53+00:00

I have a method where I am passing in two object, which have the

  • 0

I have a method where I am passing in two object, which have the same property names, and I’m using Reflection to get the values from one object and set the values on the other. My problem is when I come across a property that is a collection, the original is EntityCollection and the one getting set is ObservableCollection, and I’m obviously going to throw a casting error when I try to set the value.

So how would I go about this? I thought one way would be to get the instance of the EntityCollection property and in a loop use Activator.CreateInstance() to add a new item to the ObservableCollection. But I come across another question of how to get the original instance.

I would be greatly appreciative for some insight into this dilemma. I’ll post the code for the method that I’m working with. It currently doesn’t work, mainly I posted it just for a reference. So please don’t point out the obvious. Thanks in advance.

protected void SetValues(Object thisObject, Object entity)
{
    PropertyInfo[] properties = entity.GetType().GetProperties();
    foreach (PropertyInfo property in properties)
    {
        var value = property.GetValue(entity, null);
        var thisObjectsProperty = thisObject.GetType().GetProperty(property.Name);

        if (thisObjectsProperty != null && value != null)
        {
            if (thisObjectsProperty.PropertyType.GetInterface("ICollection", true) != null                && thisObjectsProperty.PropertyType.GetGenericArguments().Count() > 0)
            {
                Type genericType = thisObjectsProperty.PropertyType.GetGenericArguments()[0];
                Type entityCollectionType = property.PropertyType;
                Type thisCollectionType = thisObjectsProperty.PropertyType;

                IList entityCollection = (IList)Activator.CreateInstance(entityCollectionType.MakeGenericType(genericType));
                IList observableCollection = (IList)Activator.CreateInstance(thisCollectionType.MakeGenericType(genericType));

                foreach (var item in entityCollection)
                {
                    String typeString = String.Concat("RulesGenerator.DependencyObjects.", genericType.Name);
                    Type newItemType = Type.GetType(typeString, false, true);
                    if (newItemType != null)
                    {
                        var newItem = Activator.CreateInstance(newItemType);
                        SetValues(newItem, item);
                        observableCollection.Add(newItem);
                    }
                }
            }
            else
                thisObjectsProperty.SetValue(thisObject, value, null);
        }
    }
}
  • 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-17T23:17:54+00:00Added an answer on May 17, 2026 at 11:17 pm

    Implement the ICloneable interface to provide a deep copy. You should be able to find some examples, but here is a starting point:

    http://msdn.microsoft.com/en-us/library/system.icloneable%28v=VS.71%29.aspx

    http://en.csharp-online.net/ICloneable

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Diagnostics;
    
    
    public class testMain : ICloneable
    {
    
        private string m_TestProp;
    
        private List<Record> m_Items = new List<Record>();
        public string TestProp
        {
            get { return m_TestProp; }
            set { m_TestProp = value; }
        }
    
        public List<Record> Items
        {
            get { return m_Items; }
        }
    
    
        public object Clone()
        {
    
            testMain cpy =(testMain) this.MemberwiseClone();
    
            foreach (Record rec in this.Items)
            {
                Record recCpy = (Record)rec.Clone();
                cpy.Items.Add(recCpy);
            }
    
            return cpy;
        }
    
    }
    
    public class Record : ICloneable
    {
    
        private string m_TestProp;
    
        public string TestProp
        {
            get { return m_TestProp; }
            set { m_TestProp = value; }
        }
    
        public object Clone()
        {
            return this.MemberwiseClone();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a get method passing in an object of type Store to the
I am working on a method that compares two objects using reflection. The object
I have some difficulties for using Ruby block, passing in a method. As in
I have a search class that I am using to fetch results from two
Object A calls method M of object B , passing it two callbacks for
I have created an DialogFragment which is building and returning AlertDialog from onCreateDialog method.
I have a section of code which is passing an object across AppDomains and
I have some class that I'm passing as a result of a service method,
I have following code for loading image from url in xml parsing endElement method
I have a console application which is parsing HTML documents via the WebRequest method

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.