C# 如何动态迭代两个对象的属性

C# 如何动态迭代两个对象的属性,c#,.net,reflection,C#,.net,Reflection,我有两个相同类的对象,我想用脏列表中的字段更新p2。到目前为止,我成功地编写了以下代码,但很难获得p1属性的值。我应该在这里将什么对象作为参数传递给GetValue方法 Person p1 = new Person(); p1.FirstName = "Test"; Person p2 = new Person(); var allDirtyFields = p1.GetAllDirtyFields(); foreach (var dirtyField in allDirtyFields) {

我有两个相同类的对象,我想用脏列表中的字段更新p2。到目前为止,我成功地编写了以下代码,但很难获得p1属性的值。我应该在这里将什么对象作为参数传递给
GetValue
方法

Person p1 = new Person();
p1.FirstName = "Test";
Person p2 = new Person();

var allDirtyFields = p1.GetAllDirtyFields();
foreach (var dirtyField in allDirtyFields)
{
  p2.GetType()
    .GetProperty(dirtyField)
    .SetValue(p1.GetType().GetProperty(dirtyField).GetValue());
}     

_context.UpdateObject(p2);
_context.SaveChanges();
提前感谢。

您需要通过,就像这样:

p1.GetType().GetProperty(dirtyField).GetValue(p1, null)
第二个参数,如果属性类型已被索引,则可用于检索特定索引处的值。

您需要传递,如下所示:

p1.GetType().GetProperty(dirtyField).GetValue(p1, null)

第二个参数,如果属性类型已被索引,则可用于在特定索引处检索值。

IIrc发送p1表示保存该值的实例,发送null表示未搜索特定索引值。

IIrc发送p1表示保存该值的实例,发送null表示未搜索特定索引值。

您应该尝试:

foreach (var dirtyField in allDirtyFields)
{
    var prop = p2.GetType().GetProperty(dirtyField);
    prop.SetValue(p2, prop.GetValue(p1));
}
最好将
PropertyInfo
实例存储在变量中,然后尝试两次解析它。

您应该尝试:

foreach (var dirtyField in allDirtyFields)
{
    var prop = p2.GetType().GetProperty(dirtyField);
    prop.SetValue(p2, prop.GetValue(p1));
}

最好将
PropertyInfo
实例存储在变量中,然后尝试对其进行两次解析。

在每次迭代中,您必须获得对
PropertyInfo
的引用。调用它的
SetValue
方法时,应该传入两个参数,即要为其设置属性的对象和正在设置的实际值。对于后一种方法,您应该在同一属性上调用
GetValue
方法,将
p1
对象作为参数传入,即值的源

试试这个:

foreach (var dirtyField in allDirtyFields)
{
    var p = p2.GetType().GetProperty(dirtyField);
    p.SetValue(p2, p.GetValue(p1));
}
我建议您将
dirtyField
变量保存在字典中,并从此字典中检索关联的
PropertyInfo
对象。应该快得多。 首先,在类中声明一些静态变量:

static Dictionary<string, PropertyInfo> 
    personProps = new Dictionary<string, PropertyInfo>();

在每次迭代中,必须获得对
属性info
的引用。调用它的
SetValue
方法时,应该传入两个参数,即要为其设置属性的对象和正在设置的实际值。对于后一种方法,您应该在同一属性上调用
GetValue
方法,将
p1
对象作为参数传入,即值的源

试试这个:

foreach (var dirtyField in allDirtyFields)
{
    var p = p2.GetType().GetProperty(dirtyField);
    p.SetValue(p2, p.GetValue(p1));
}
我建议您将
dirtyField
变量保存在字典中,并从此字典中检索关联的
PropertyInfo
对象。应该快得多。 首先,在类中声明一些静态变量:

static Dictionary<string, PropertyInfo> 
    personProps = new Dictionary<string, PropertyInfo>();

您知道不需要检索每个对象的属性吗

类型元数据对于整个类型的任何对象都是通用的

例如:

// Firstly, get dirty property informations!
IEnumerable<PropertyInfo> dirtyProperties = p2.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public)
              .Where
              (
                   property => allDirtyFields.Any
                   (
                       field => property.Name == field
                   )
              );

// Then, just iterate the whole property informations, but give the
// "obj" GetValue/SetValue first argument the references "p2" or "p1" as follows:
foreach(PropertyInfo dirtyProperty in dirtyProperties)
{          
       dirtyProperty.SetValue(p2, dirtyProperty.GetValue(p1)); 
}
//首先,获取脏属性信息!
IEnumerable dirtyProperties=p2.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public)
哪里
(
属性=>allDirtyFields.Any
(
字段=>属性。名称==字段
)
);
//然后,只需迭代整个属性信息,但给出
//“obj”GetValue/SetValue第一个参数引用“p2”或“p1”,如下所示:
foreach(dirtyProperty中的PropertyInfo dirtyProperty)
{          
dirtyProperty.SetValue(p2,dirtyProperty.GetValue(p1));
}

检查和的第一个参数是否为要获取或设置整个属性值的对象

您知道不需要检索每个对象的属性吗

类型元数据对于整个类型的任何对象都是通用的

例如:

// Firstly, get dirty property informations!
IEnumerable<PropertyInfo> dirtyProperties = p2.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public)
              .Where
              (
                   property => allDirtyFields.Any
                   (
                       field => property.Name == field
                   )
              );

// Then, just iterate the whole property informations, but give the
// "obj" GetValue/SetValue first argument the references "p2" or "p1" as follows:
foreach(PropertyInfo dirtyProperty in dirtyProperties)
{          
       dirtyProperty.SetValue(p2, dirtyProperty.GetValue(p1)); 
}
//首先,获取脏属性信息!
IEnumerable dirtyProperties=p2.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public)
哪里
(
属性=>allDirtyFields.Any
(
字段=>属性。名称==字段
)
);
//然后,只需迭代整个属性信息,但给出
//“obj”GetValue/SetValue第一个参数引用“p2”或“p1”,如下所示:
foreach(dirtyProperty中的PropertyInfo dirtyProperty)
{          
dirtyProperty.SetValue(p2,dirtyProperty.GetValue(p1));
}

检查和的第一个参数是否为要获取或设置整个属性值的对象

谢谢,你能举个例子说明我如何为每个变量存储PropertyInfo吗实际上,它已经在我的代码中了,不是吗
prop
local变量,而不是
p2.GetType().SetValue()
p1.GetType().GetValue()
保存一个
GetType()
调用,这是一个非常昂贵的调用。谢谢,你能举个例子说明我如何为每个变量存储PropertyInfo吗实际上,它已经在我的代码中了,不是吗
prop
局部变量,而不是
p2.GetType().SetValue()
p1.GetType().GetValue()
保存一个
GetType()
调用,这是一个非常昂贵的调用。