C# .NET:是否有一个类可以将一个类的属性复制到另一个类
我编写了一个函数,它将一个类的属性复制到另一个类,因此可以复制一个对象 大概是C# .NET:是否有一个类可以将一个类的属性复制到另一个类,c#,.net,vb.net,C#,.net,Vb.net,我编写了一个函数,它将一个类的属性复制到另一个类,因此可以复制一个对象 大概是 MyObject myObject = myOtherObject.MyCustomCopy(myObject) 其中,myObject和myOtherObject属于同一类型。我基本上是这样做的 myObject.prop1 = myOtherObject.prop1 myObject.prop2 = myOtherObject.prop2 myObject.prop3 = myOtherObject.prop3
MyObject myObject = myOtherObject.MyCustomCopy(myObject)
其中,myObject和myOtherObject属于同一类型。我基本上是这样做的
myObject.prop1 = myOtherObject.prop1
myObject.prop2 = myOtherObject.prop2
myObject.prop3 = myOtherObject.prop3
return myObject
我敢肯定,在过去,我使用了一个.NET对象,我想是通过反射自动完成的,但我不记得了。。。或者我想象这样一种方法的存在
是的,我知道auto mapper,但我确信(现在不是那么多)有一个.NET对象来完成这项工作。也许不是 尝试此链接中的描述:
您可以看一看。您应该使用它,它是为此作业构建的。此代码应该适用于基本属性类型,但不确定它将如何适用于任何复杂的属性(列表、数组、自定义类)。不过,这应该是一个起点:
public class MyClass
{
public int A { get; set; }
public string B { get; set; }
}
private void button1_Click(object sender, EventArgs e)
{
MyClass orig = new MyClass() { A = 1, B = "hello" };
MyClass copy = new MyClass();
PropertyInfo[] infos = typeof(MyClass).GetProperties();
foreach (PropertyInfo info in infos)
{
info.SetValue(copy, info.GetValue(orig, null), null);
}
Console.WriteLine(copy.A + ", " + copy.B);
}
公共静态类ext
{
公共静态void CopyAllTo(此T源,T目标)
{
var类型=类型(T);
foreach(类型.GetProperties()中的var sourceProperty)
{
var targetProperty=type.GetProperty(sourceProperty.Name);
SetValue(target,sourceProperty.GetValue(source,null),null);
}
foreach(类型为.GetFields()的var sourceField)
{
var targetField=type.GetField(sourceField.Name);
targetField.SetValue(目标、源字段.GetValue(源));
}
}
}
用非常简单的术语:正如我们所知,类是C#NET中的引用类型,即当我们创建类的对象时,例如
Customer C1=new Customer();
C1.Id=1;
C1.Name="Rakesh";
然后C1(引用变量)存储在内存堆栈上,对象new Customer()存储在堆上
因此,当我们将一个类复制到另一个类中时,这基本上是您的问题,您可以执行以下操作:
Customer C2=C1;
执行上述操作会将C1引用变量复制到C2中,但为什么我要写堆栈和堆,因为使用C2引用变量可以更改对象属性,即C1和C2都指向堆中的同一对象
C2.Id=1;
C2.Name="Mukesh";
现在,如果您尝试访问
C1.Name
,您将看到它被更改为“Mukesh”。我知道OP没有要求将一个类型转换为另一个类型,但我的变体是我在startup.cs中使用的DI,用于云和本地开发环境之间的配置不匹配。我的本地类通常在后台使用接口类来映射配置。然后我使用这个方法复制属性,这些属性只在名称上匹配。我没有检查属性类型,因为它们是配置。建议使用AutoMapper。我不使用AutoMapper,因为我们受到美国国防部的限制,仅限于某些供应商。仅仅使用.NET获得ATO已经够难的了,我们不需要再悲伤了
using System.Linq;
public static class PropertyCopy
{
public static void CopyAllTo<T,T1>(this T source, T1 target)
{
var type = typeof(T);
var type1 = typeof(T1);
foreach (var sourceProperty in type.GetProperties())
{
foreach (var targetProperty in type1.GetProperties()
.Where(targetProperty => sourceProperty.Name == targetProperty.Name)
.Where(targetProperty => targetProperty.SetMethod != null))
{
targetProperty.SetValue(target, sourceProperty.GetValue(source, null), null);
}
}
foreach (var sourceField in type.GetFields())
{
foreach (var targetField in type1.GetFields()
.Where(targetField => sourceField.Name == targetField.Name))
{
targetField.SetValue(target, sourceField.GetValue(source));
}
}
}
}
使用System.Linq;
公共静态类属性副本
{
公共静态void CopyAllTo(此T源,T1目标)
{
var类型=类型(T);
var type1=typeof(T1);
foreach(类型.GetProperties()中的var sourceProperty)
{
foreach(type1.GetProperties()中的var targetProperty)
.Where(targetProperty=>sourceProperty.Name==targetProperty.Name)
.Where(targetProperty=>targetProperty.SetMethod!=null))
{
SetValue(target,sourceProperty.GetValue(source,null),null);
}
}
foreach(类型为.GetFields()的var sourceField)
{
foreach(type1.GetFields()中的var targetField)
.Where(targetField=>sourceField.Name==targetField.Name))
{
targetField.SetValue(目标、源字段.GetValue(源));
}
}
}
}
必须检查此对象的时间戳:)问题是如何复制对象,而不是如何将对象的属性值复制到其他类型的对象。您也可以通过这种方式克隆对象。嗨,bitbonk,它实际上不复制引用类型,只复制上面提到的引用自动映射器功能更强,但是对于像我这样的简单用例,使用它运行起来更简单、更快。谢谢Automapper将为您提供一个具有复制属性的新引用对象,而此解决方案将在现有引用之间复制属性,这正是问题所需要的
using System.Linq;
public static class PropertyCopy
{
public static void CopyAllTo<T,T1>(this T source, T1 target)
{
var type = typeof(T);
var type1 = typeof(T1);
foreach (var sourceProperty in type.GetProperties())
{
foreach (var targetProperty in type1.GetProperties()
.Where(targetProperty => sourceProperty.Name == targetProperty.Name)
.Where(targetProperty => targetProperty.SetMethod != null))
{
targetProperty.SetValue(target, sourceProperty.GetValue(source, null), null);
}
}
foreach (var sourceField in type.GetFields())
{
foreach (var targetField in type1.GetFields()
.Where(targetField => sourceField.Name == targetField.Name))
{
targetField.SetValue(target, sourceField.GetValue(source));
}
}
}
}