Click here to Skip to main content
15,067,335 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,
I have a ClassA and ClassB both having same property .Is it possible to assign ClassA to ClassB means
C#
ClassB=ClassA


Please help .

What I have tried:

Other wise i have to set all property of ClassA to ClassB
Posted
Updated 6-Jun-16 21:17pm
Comments
Karthik_Mahalingam 7-Jun-16 2:04am
   
no, it is not possible to assign one class object to another.
you shall use reflection to copy the properties from one class to another.
Dmitriy Gakh 7-Jun-16 2:23am
   
It is possible without reflection.
Raj tilak Bose 7-Jun-16 4:35am
   
can you please share me a sample example?
Dmitriy Gakh 7-Jun-16 8:14am
   
Conversion operators - see my solution and reference below. If you will stuck, I can help you because I have big experience with conversion operators.
   
First, there is no such thing as "copy a class". You copy the instances.
"Not possible" is simply not true.
If one class is derived from another one, the referenced could be copies (one way).
"Set all property" means cloning.
—SA
Raj tilak Bose 7-Jun-16 2:14am
   
ok thanks

Please see my comment to the question. First of all, there is no such thing as copying classes.

Please see my past answer: How to implement ICloneable for a List Class?[^].

The best approach is Solution 3.

—SA
   
v2
Comments
Raj tilak Bose 7-Jun-16 4:56am
   
Thanks Sir
   
You are welcome.
Will you accept this solution formally?
—SA
Dmitriy Gakh 7-Jun-16 8:08am
   
This solution is the best indeed because it points to Class/Instance mistake and based on interface that is best from architectural point of view (the best practice).
   
Thank you, Dmitriy.
—SA
You can write implicit conversion operator and use it for converting instances of the classes (but not copying in term of exact copy). You cannot use ClassB=ClassA because they are classes. But if you have something like this

ClassA a = new ClassA();
ClassB b = new ClassB();

You can use b = a; as conversion if you have defined implicit conversion operator. Inside the conversion operator you can write your copy logic.


See this:
Using Conversion Operators (C# Programming Guide)[^]
   
v2
Comments
Raj tilak Bose 7-Jun-16 4:55am
   
Thanks
Dmitriy Gakh 7-Jun-16 8:11am
   
I improved the solution stating copying instances, not classes.
using Reflection:

C#
using System;
using System.Reflection;



class Test
{
    public static void Main()
    { 

        ClassA objClassA = new ClassA();
        objClassA.ID = 1;
        objClassA.Name = "NameValue";

        ClassB objClassB = new ClassB();
        CopyProperties(objClassA, objClassB);

        int id = objClassB.ID; // 1
        string name = objClassB.Name; // NameValue
         

    }

    private static void CopyProperties(object objectA, object objectB)
    {
        if (objectA == null || objectB == null) throw new ArgumentNullException();

        var props = objectA.GetType().GetProperties();
        foreach (PropertyInfo pi in props)
        {
            object value = pi.GetValue(objectA, null);
            var prop = objectB.GetType().GetProperty(pi.Name);
            if (prop != null)
                prop.SetValue(objectB, value, null);
        }
    }

}

class ClassA  
{
    public int ID { get; set; }
    public string Name { get; set; }
}

class ClassB  
{
    public int ID { get; set; }
    public string Name { get; set; }
}
   
Comments
Raj tilak Bose 7-Jun-16 4:55am
   
Thanks its helprd me :)
Karthik_Mahalingam 7-Jun-16 4:56am
   
welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900