请解释为什么对象引用在我的代码中丢失

发布于 2024-11-10 12:14:40 字数 2350 浏览 5 评论 0原文

您好,我正在做一些与反射相关的事情,我不明白我的代码有什么问题。我尝试清理我的代码,但是,第一段代码不会更新我的实例值,当我单步执行调试器时,我可以看到“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);
}

Hi I am doing something related to Reflection, I don't understand what's wrong with my code. I try to clean up my codes however, the first piece of code will not update my instance values, when I step through the debugger I can see the correct result from "newobj", however the "next" reference is lost as a result of not updating my instance values. The only change I have done is to add "this" to queue, to me it is no difference. Can someone explain the reason behind 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);

        }
    }

Other support codes

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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余生共白头 2024-11-17 12:14:40

我想我自己发现了我的问题,我的 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);
        }

I think I found my problem myself, my ValueAssign() has some bug. I found a similar method on the net which works perfectly!

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);
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文