C# NET:如何在具有相似成员(类型和名称)的不同类之间复制数据?
假设我们有两个类:C# NET:如何在具有相似成员(类型和名称)的不同类之间复制数据?,c#,.net,C#,.net,假设我们有两个类: public class A { public int P1 {set; get;} public int P2 {set; get;} } public class B { public int P1 {set; get;} public int P2 {set; get;} public int P3 {set; get;} public int P4 {set; get;} } 我可以用某种方式将其转换为初始化具有相同名称的成员吗 我的
public class A
{
public int P1 {set; get;}
public int P2 {set; get;}
}
public class B
{
public int P1 {set; get;}
public int P2 {set; get;}
public int P3 {set; get;}
public int P4 {set; get;}
}
我可以用某种方式将其转换为初始化具有相同名称的成员吗
我的意思是,如果.NET有一些操作需要排除,例如:
A.P1 = B.P1
A.P2 = B.P2
B.P1 = A.P1
B.P2 = A.P2
忽略其他成员
可以这样做吗?因为类之间没有继承关系,所以不能将一个类强制转换为另一个类 如果
B
继承自A
,则可以执行此操作-然后它将自动获取A
上定义的属性
关于你的例子:
A.P1 = B.P1
A.P2 = B.P2
B.P1 = A.P1
B.P2 = A.P2
由于所有
P1
和P2
成员的类型都是int
,因此始终可以执行此类赋值,因为属性是公共的,并且具有公共的getter和setter。不需要强制转换。您可以将公共属性提取到接口
public interface IMyInterface
{
int P1 {set; get;}
int P2 {set; get;}
}
public class A : IMyInterface
{
public int P1 {set; get;}
public int P2 {set; get;}
}
public class B : IMyInterface
{
public B(IMyInterface i)
{
P1 = i.P1;
P2 = i.P2;
}
public int P1 {set; get;}
public int P2 {set; get;}
public int P3 {set; get;}
public int P4 {set; get;}
}
然后你可以这样做:
A a = new A();
a.P1 = 1;
a.P2 = 2;
B b = new B(a);
Console.WriteLine(b.P1); //Outputs 1
Console.WriteLine(b.P2); //Outputs 2
编辑:
也许你可以看看图书馆你为什么想要这样的东西?如果两个类彼此相关,您可能希望继承它们:
public class A
{
public int P1 {set; get;}
public int P2 {set; get;}
}
public class B : A
{
public int P3 {set; get;}
public int P4 {set; get;}
}
在您拥有的情况下,使用接口或继承都是有效的解决方案,并且可以更改类A和B 如果没有,可以使用反射将属性从一个对象复制到另一个对象。类似上面的东西
A a = new A();
B b = new B();
GenericConverter<A,B>.Convert(a, b);
public static class GenericConverter<TInput, TOutput> where TOutput : new()
{
/// <summary>
/// Converts <paramref name="entity"/> from <see cref="TInput"/> to <see cref="TOutput"/>
/// </summary>
/// <param name="entity">the object to convert</param>
/// <returns>The object converted</returns>
public static TOutput Convert(TInput entity)
{
if(entity is Enum)
throw new NotImplementedException("Entity is an enumeration - Use ConvertNum!");
TOutput output = new TOutput();
Type fromType = entity.GetType();
Type toType = output.GetType();
PropertyInfo[] props = fromType.GetProperties();
foreach (PropertyInfo prop in props)
{
PropertyInfo outputProp = toType.GetProperty(prop.Name);
if (outputProp != null && outputProp.CanWrite)
{
string propertyTypeFullName = prop.PropertyType.FullName;
object value = prop.GetValue(entity, null);
outputProp.SetValue(output, value, null);
}
}
return output;
}
}
A A=新A();
B=新的B();
转换(a,b);
公共静态类GenericConverter,其中TOutput:new()
{
///
///从转变为
///
///要转换的对象
///对象已转换
公共静态TOutput转换(TInput实体)
{
if(实体为枚举)
抛出新的NotImplementedException(“实体是枚举-使用ConvertNum!”);
TOutput输出=新的TOutput();
Type fromType=entity.GetType();
Type toType=output.GetType();
PropertyInfo[]props=fromType.GetProperties();
foreach(PropertyInfo props in props)
{
PropertyInfo outputProp=toType.GetProperty(prop.Name);
if(outputProp!=null&&outputProp.CanWrite)
{
字符串propertyTypeFullName=prop.PropertyType.FullName;
对象值=prop.GetValue(实体,空);
outputProp.SetValue(输出,值,null);
}
}
返回输出;
}
}
然后这两个类将实现相同的接口,这样就可以完成,不需要强制转换。@Terminador不确定还需要做什么?@Terminador啊,不,你不能这样做,我很抱歉afraid@Terminador,我问你是指A还是B,你说是:-)。@Terminador,你绝对应该按照@Simon的建议使用AutoMapper。这很容易,一旦在项目中引用AutoMapper,就可以调用Mapper.CreateMap()
初始化映射,然后调用Mapper.Map(dest,source)代码>其中dest
为DestinationType
且source
为SourceType
您所说的“排除类似的操作”是什么意思?你到底想实现什么?可能是。。。但我更喜欢@Simon的解决方案谢谢你!谢谢你的两分钱!!(+1)