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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:15:41+00:00 2026-05-23T16:15:41+00:00

I’m working in c# with several workspaces that have one specific class which his

  • 0

I’m working in c# with several workspaces that have one specific class which his always the same in each workspace.
I would like to be able have a copy of this class to be able to work with it without dealing with namespaces differences.
example :

namespace1 {
    class class1{
        public class2;
    }

    class class2{
        public string;
    }

}

namespace2 {
    class class1{
        public class2;
    }

    class class2{
        public string;
    }
}

In my copied Class I’ve got a function to copy all data’s to one of the namespace’s class.
It’s working if i only have c# standard types. I got exeption ( “Object does not match target type.” ) as soon as I’m dealing with class2 object (which is also from different namespaces)

public Object toNamespaceClass(Object namespaceClass)
{
    try
    {
        Type fromType = this.GetType();
        Type toType = namespaceClass.GetType();

        PropertyInfo[] fromProps = fromType.GetProperties();
        PropertyInfo[] toProps = toType.GetProperties();

        for (int i = 0; i < fromProps.Length; i++)
        {
            PropertyInfo fromProp = fromProps[i];
            PropertyInfo toProp = toType.GetProperty(fromProp.Name);
            if (toProp != null)
            {
                toProp.SetValue(this, fromProp.GetValue(namespaceClass, null), null);
            }
        }

    }
    catch (Exception ex)
    {
    }
    return namespaceClass;
}

Anyone do have any idea of how to deal with this kind of “recursivity reflection”.

I hope eveything is understandable.

Thanks, Bye!

Edit :
I think i got it solved (at least in my mind), I’ll try the solution back at work tomorrow. Taking my function out of my class and using it recursively if a property is not a standard type is maybe the solution.

  • 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-23T16:15:42+00:00Added an answer on May 23, 2026 at 4:15 pm

    I got it solved , just to let you know how I did it :
    This solution is sot perfect because it handle only 1 dimensions array not more.

    public static Object CopyObject(Object from , Object to)
    {
        try
        {
            Type fromType = from.GetType();
            Type toType = to.GetType();
    
            PropertyInfo[] fromProps = fromType.GetProperties();
            PropertyInfo[] toProps = toType.GetProperties();
    
            for (int i = 0; i < fromProps.Length; i++)
            {
                PropertyInfo fromProp = fromProps[i];
                PropertyInfo toProp = toType.GetProperty(fromProp.Name);
                if (toProp != null)
                {
                    if (toProp.PropertyType.Module.ScopeName != "CommonLanguageRuntimeLibrary")
                    {
                        if (!toProp.PropertyType.IsArray)
                        {
                            ConstructorInfo ci = toProp.PropertyType.GetConstructor(new Type[0]);
                            if (ci != null)
                            {
                                toProp.SetValue(to, ci.Invoke(null), null);
                                toProp.SetValue(to, gestionRefelexion.CopyObject(fromProp.GetValue(from, null), toProp.GetValue(to, null)), null);
                            }
                        }
                        else
                        {
                            Type typeToArray = toProp.PropertyType.GetElementType();
                            Array fromArray = fromProp.GetValue(from, null) as Array;
                            toProp.SetValue(to, copyArray(fromArray, typeToArray), null);
                        }
                    }
                    else
                    {
                        toProp.SetValue(to, fromProp.GetValue(from, null), null);
                    }
                }
            }
        }
        catch (Exception ex)
        {
        }
        return to;
    }
    
    public static Array copyArray(Array from, Type toType)
    {
        Array toArray =null;
        if (from != null)
        {
            toArray= Array.CreateInstance(toType, from.Length);
    
            for (int i = 0; i < from.Length; i++)
            {
                ConstructorInfo ci = toType.GetConstructor(new Type[0]);
                if (ci != null)
                {
                    toArray.SetValue(ci.Invoke(null), i);
                    toArray.SetValue(gestionRefelexion.CopyObject(from.GetValue(i), toArray.GetValue(i)), i);
                }
            }
        }
        return toArray;
    }
    

    Hope this can help some people.
    Thanks for helping everyone.
    Cheers

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small

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.