2011-05-29 55 views
1

嗨,我正在做一些与Reflection有关的事情,我不明白我的代码有什么问题。然而,我尝试清理我的代码,第一段代码不会更新我的实例值,当我逐步调试时,我可以看到“newobj”的正确结果,但是“next”引用由于不更新我的实例值。我所做的唯一改变就是添加“this”来排队,对我来说没有什么区别。有人能解释这背后的原因吗?请解释为什么我的代码中丢失了对象引用

private void UpdateBreathFirst()// This code is WRONG!!! but why? 
{ 
    RootQueue = new Queue<object>(); 
    RootQueue.Enqueue(this); 
    while (RootQueue.Count > 0) 
    { 
     var next = RootQueue.Dequeue(); 
     EnqueueChildren(next); 

     var newobj = next.GetType().GetMethod("Get").Invoke(next, null); 
     ValueAssign(next, newobj); 

    } 
} 



private void UpdateBreathFirst()//This code produces correct result. 
    { 
     RootQueue = new Queue<object>(); 
     var val = GetType().GetMethod("Get").Invoke(this, null); 
     ValueAssign(this, val); 
     EnqueueChildren(this); 
     while (RootQueue.Count > 0) 
     { 
      var next = RootQueue.Dequeue(); 
      EnqueueChildren(next); 

      var newobj = next.GetType().GetMethod("Get").Invoke(next, null); 
      ValueAssign(next, newobj); 

     } 
    } 

其他支持代码

private Queue<object> RootQueue; 

private void EnqueueChildren(object obj) 
{ 
    if (BaseTypeCompare(obj.GetType(), typeof(SerializedEntity<>))) 
    { 
     foreach (var propertyInfo in obj.GetType().GetProperties()) 
     { 
      if (BaseTypeCompare(propertyInfo.PropertyType, typeof (List<>))) 
      { 
       var list = (IList) propertyInfo.GetValue(obj, null); 
       if (list != null) 
       { 
        foreach (object item in list) 
        { 
         RootQueue.Enqueue(item); 
        } 

       } 

      } 
     } 
    } 
} 

public static void ValueAssign(object a, object b) 
{ 
    foreach (var p in a.GetType().GetProperties()) 
    { 
     foreach (var p2 in b.GetType().GetProperties()) 
     { 
      if (p.Name == p2.Name && BaseTypeCompare(p.GetType(), p2.GetType())) 
      { 
       p.SetValue(a, p2.GetValue(b, null), null); 
      } 
     } 
    } 
} 

public static bool BaseTypeCompare(Type t, Type t2) 
{ 
    if (t.FullName.StartsWith(t2.FullName)) return true; 
    if (t == typeof(object)) return false; 
    return BaseTypeCompare(t.BaseType, t2); 
} 
+2

为什么我觉得反射在这里是不必要的? – ChaosPandion 2011-05-29 19:20:58

+2

“宽度”,而不是“呼吸”,是对深度的补充。 – 2011-05-29 19:24:23

+0

它是一个代码生成类,我不得不使用反射。我从自动完成标签列表中选择了广度优先搜索,本网站表示我无法创建新标签(呼吸优先搜索)作为新成员。 – 2011-05-29 20:02:22

回答

0

我想我找到了我的问题我自己,我的ValueAssign()有一些bug。我在网上发现了一个类似的方法,完美地工作!

private static void CopyObject(object sourceObject, ref object destObject) 
    { 
     // If either the source, or destination is null, return 
     if (sourceObject == null || destObject == null) 
      return; 

     // Get the type of each object 
     Type sourceType = sourceObject.GetType(); 
     Type targetType = destObject.GetType(); 

     // Loop through the source properties 
     foreach (PropertyInfo p in sourceType.GetProperties()) 
     { 
      // Get the matching property in the destination object 
      PropertyInfo targetObj = targetType.GetProperty(p.Name); 
      // If there is none, skip 
      if (targetObj == null) 
       continue; 

      // Set the value in the destination 
      targetObj.SetValue(destObject, p.GetValue(sourceObject, null), null); 
     } 
相关问题