将类复制到其他类 [英] Copy class to other class

查看:67
本文介绍了将类复制到其他类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个ClassA和ClassB都具有相同的属性。是否可以将ClassA分配给ClassB意味着

 ClassB = ClassA 





请帮忙。



我是什么尝试过:



其他明智的我必须将ClassA的所有属性设置为ClassB

解决方案

< blockquote>请参阅我对该问题的评论。首先,没有复制类这样的东西。



请看我过去的回答:如何为列表类实现ICloneable? [ ^ ]。



最好的方法是解决方案3.



-SA


< blockquote>您可以编写隐式转换运算符并将其用于转换类的实例(但不能复制精确副本)。您不能使用ClassB = ClassA,因为它们是类。但如果你有类似的东西



ClassA a = new ClassA();

ClassB b = new ClassB();



您可以使用b = a;如果已定义隐式转换运算符,则为转换。在转换操作符中,您可以编写复制逻辑。





请参阅:

使用转换运算符(C#编程指南) [ ^ ]


使用反射:



 使用系统; 
使用 System.Reflection;



class 测试
{
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(< span class =code-keyword> 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,< span class =code-sdkkeyword> value , null );
}
}

}

class ClassA
{
public int ID { get ; set ; }
public string 名称{ get ; set ; }
}

class ClassB
{
public int ID { get ; set ; }
public string 名称{ get ; set ; }
}


Hi All,
I have a ClassA and ClassB both having same property .Is it possible to assign ClassA to ClassB means

ClassB=ClassA



Please help .

What I have tried:

Other wise i have to set all property of ClassA to ClassB

解决方案

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


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)[^]


using Reflection:

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; }
}


这篇关于将类复制到其他类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆