Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a class say T
C#
class T
{
public string Name{get;set;}
public string LName{get;set;}
}

Class T1
{
public string Name{get;set;}
public string LName{get;set;}
}

List<T> T_list = new List<T>
List<T1> T1_list = new List<T1>

How to do copy 
T1_list = T_list ????


What I have tried:

i can do this using foreach loop like
C#
T1 t1 = null;
foreach(var item in T_list)
{
t1 = new T1();
t1.Name = item.name;
t1.LName = item.LName;

T1_list(t1);
}

but i have more properties so is thee any better way to do this.
Posted
Updated 25-Apr-19 17:05pm

As you have two different classes, the easiest thing I think is to create a constructor on T1 that takes a T and builds the T1 from the T. That puts that code in the class where it belongs and you can use it anywhere by using T1_list.Add(new T1(item).

T1_list(t1);


I have literally no clue what you think this will do. Also, your new statements are missing the brackets.
 
Share this answer
 
 
Share this answer
 
Comments
Maciej Los 24-Apr-19 8:26am    
5ed!
Note: you don't need to create two classes with different names if their fields, members are the same!

For example:
C#
class T
{
public string Name{get;set;}
public string LName{get;set;}
}

//usage:
List<T> firstlist = new List<T>();
List<T> secondlist = new List<T>();

//copy data from first list
secondlist.AddRange(firstlist);


For further details, please see: List<T>.AddRange(IEnumerable<T>) Method (System.Collections.Generic) | Microsoft Docs[^]
 
Share this answer
 
v2
If you need to make a list of mixed type, then you must make both lists of an object. Since every class is derived from an object, the list is generic enough to contain anything. can do something like below:

C#
public class T
{
    public string Name { get; set; }
    public string LName { get; set; }
}

public class T1
{
    public string Name { get; set; }
    public string LName { get; set; }
}

List<object> T_list = new List<object>();
List<object> T1_list = new List<object>();

T_list.Add(new T { Name = "T_firstName", LName = "T_FirstName" });
T1_list.Add(new T1 { Name = "T1_firstName", LName = "T1_LastName" });

T1_list.AddRange(T_list);


Now, T1_list contains both the elements from T_listand T1_list and is now a mixed type (i.e., T and T1).
 
Share this answer
 
Automatically and dynamically copy all fields and properties of a class T to another class T1


You must use Reflection and Extention method.


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace runtimesetval
{


    static class ObjectExtensions
    {
      
        public static void CopyAllFieldsTo(this object obj, object src)
        {

            //Getting Type of Src
            Type tModelType = src.GetType();
            BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | 
                BindingFlags.SetField | BindingFlags.SetProperty;
            //We will be defining a PropertyInfo Object which contains details about the class property 
            PropertyInfo[] arrayPropertyInfos = tModelType.GetProperties(flags);


            //Now we will loop in all properties one by one to get value
            foreach (PropertyInfo property in arrayPropertyInfos)
            {
               
                PropertyInfo pi = obj.GetType().GetProperty(property.Name);

                if (pi != null && pi.CanWrite)
                {
                    pi.SetValue
                    (
                        obj,
                        Convert.ChangeType(property.GetValue(src, null), pi.PropertyType),
                        null
                    );
                }



            }




        }

    }

    class TestObject
    {
        public string p1 { get; set; }
        public int p2 { get; set; }
    }



    class TestObject1
    {
        public string p1 { get; set; }
        public int p2 { get; set; }
        public string p3 { get; set; }
        public int p4 { get; set; }
    }




    class Program
    {
        static void Main(string[] args)
        {
            TestObject o = new TestObject();
            TestObject1 o1 = new TestObject1();


            o.p1 = "o.p1";
			o.p2 =1;
			
            o1.p1 = "o1.p1"; 
            o1.p2 =2;

			
			
			//Copy all fields values from o(TestObject) to o1(TestObject1)
			
            o1.CopyAllFieldsTo(o);


			//Copy all fields values from o1(TestObject1) to o(TestObject)
			
            o.CopyAllFieldsTo(o1);



            
            Console.ReadKey();
            
        }
    }
}
 
Share this answer
 

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