1

こんにちは私はReflectionに関連する何かをしています、私は私のコードの何が悪いのか理解していません。コードをクリーンアップしようとしましたが、最初のコードはインスタンス値を更新しません。デバッガーをステップスルーすると、「newobj」から正しい結果が表示されますが、「次の」参照は次の結果として失われます。インスタンス値を更新していません。私が行った唯一の変更は、キューに「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);
}
4

1 に答える 1

0

私は自分の問題を自分で見つけたと思います。ValueAssign()にはいくつかのバグがあります。私はネット上で完璧に機能する同様の方法を見つけました!

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);
        }
于 2011-07-07T13:19:02.170 に答える