.net 如何轻松确定引用此对象的对象?

.net 如何轻松确定引用此对象的对象?,.net,serialization,reference,.net,Serialization,Reference,我正在尝试实现一种在应用程序中克隆元素的方法。这些元素是由我定制和设计的(到目前为止,它们还没有那么复杂)。每个项都是一个名为子项的属性,其中列出了更多的实例(将其想象为具有多个子分支的树状视图) 我有一个名为“Environment”的对象,其中包含对所有这些子对象的引用。把它想象成一个果园,里面有我所有的树。当我想复制所有内容时,我希望能够在我的“环境”类上调用“克隆”,并让它给我所有内容的副本 这一切都工作了一段时间,但经过一些更改和代码重构后,它似乎不再工作了,我也不知道为什么。我可以很

我正在尝试实现一种在应用程序中克隆元素的方法。这些元素是由我定制和设计的(到目前为止,它们还没有那么复杂)。每个项都是一个名为
子项
的属性,其中列出了更多的实例(将其想象为具有多个子分支的树状视图)

我有一个名为“Environment”的对象,其中包含对所有这些子对象的引用。把它想象成一个果园,里面有我所有的树。当我想复制所有内容时,我希望能够在我的“环境”类上调用“克隆”,并让它给我所有内容的副本

这一切都工作了一段时间,但经过一些更改和代码重构后,它似乎不再工作了,我也不知道为什么。我可以很好地跟踪堆栈跟踪,但在这种情况下,这是不可能的。这是我的错误的屏幕截图

我还将使用序列化将我的“环境”保存到一个文件中。然而,在我能够做到这一点之前,我必须让序列化正常工作

无论如何,我已经分别检查了我的单个树/分支/肢体对象,它们都成功克隆。我唯一似乎无法克隆的是“环境”类(我的果园)。以下是我的“环境”类的来源:

导入gE.gEngine
导入LuaInterface
导入gE.gEngine.Generic
导入Microsoft.Xna.Framework
导入System.Runtime.Serialization
导入System.IO
导入System.Runtime.Serialization.Formatters.Binary
导入gE.gEngine.WorldObjects
导入System.Windows.Forms
导入Microsoft.Xna.Framework.Input
导入System.Threading.Tasks
导入系统线程
_
公共课堂环境
作为世界服务的公共世界
公共玩家作为世界服务。玩家
公共照明服务.照明
公共环境作为世界服务。环境
公共GenericsLibrary作为WorldServices.GenericsLibrary
_
公共Lua作为新的LuaInterface.Lua
_
公共SafetyContext作为CommonEnum.SafetyContext=CommonEnum.SafetyContext.Safe
公共事件环境已更改(ByVal发送方作为对象,ByVal e作为GenericEventArgument)
Public Sub call_environment已更改(ByVal sender作为对象,ByVal e作为GenericEventArgument)
RaiseEvent环境已更改(发件人,e)
端接头
公共分新()
GlobalShare.Environment=Me
InitializeLua()
照明=新WorldServices.Lighting()
World=新的WorldServices.World()
Players=新世界服务。Players()
环境=新的WorldServices.Environment()
GenericsLibrary=New WorldServices.GenericsLibrary()
将组设置为新的WorldObjects。将组设置为{
.Name=“约束”,
.Parent=GenericsLibrary
}
Dim ordsc As List(属于gEngine.WorldObjects.Lua)=Environment.GetScripts()
排序(函数(x,y)x.RunIndex.CompareTo(y.RunIndex))
对于ordsc中的每个脚本,如gEngine.WorldObjects.Lua
GlobalShare.Environment.RunScript(script.Source,script.Name)
下一个
端接头
公共子集合安全上下文(ByVal e作为CommonEnum.SafetyContext)
如果e CommonEnum.SafetyContext.Unlocked,则
SafetyContext=e
出口接头
如果结束
抛出新异常(“尝试从不安全的Lua脚本解锁安全上下文!”)
端接头
公共子初始值()
Lua(“游戏”)=全球共享。游戏
Lua(“\uuuu环境”)=Me
Lua(“凝胶”)=新凝胶(Me)
RegisterFunction(“Print”、Lua(“gel”)、Lua(“gel”).GetType().GetMethod(“Print”))
RegisterFunction(“print”、Lua(“gel”)、Lua(“gel”).GetType().GetMethod(“print”))
RegisterFunction(“SetSafetyContext”、Lua(“gel”)、Me.GetType().GetMethod(“SetSafetyContext”))
Lua(“矢量2”)=新的Lua_u.Lua_u矢量2
Lua(“向量3”)=新的Lua_u.Lua_u向量3
Lua(“鼠标”)=新的Lua_u.Lua_u鼠标
Lua.NewTable(“助手”)
Lua(“Helpers.EnumHelper”)=新Helpers.EnumHelper
暗淡的游戏计时器作为新的秒表
Lua(“时间”)=游戏计时器
GameTimer.Start()文件
Lua.NewTable(“\uuux”)
端接头
公共函数Clone()作为环境
如果[Object].ReferenceEquals(Me,Nothing),则
一无所获
如果结束
作为IFormatter的Dim格式化程序=新的BinaryFormatter()
将流设置为IO.stream=New MemoryStream()
使用流
序列化(流,Me)
stream.Seek(0,SeekOrigin.Begin)
返回DirectCast(格式化程序.反序列化(流),[Environment])
终端使用
端函数
公共子运行脚本(ByVal scr作为字符串,ByVal name作为字符串)
尝试
Lua.DoString(scr,名称)
特例
Game.CallLogEvent(Me,新的GenericEventArgument(“LogEvent”,“Error”,例如Message))
结束尝试
端接头
末级
代码有点凌乱,因为我没有因为这个特定的问题而对这个特定的类进行太多的处理(我想知道为什么在我继续改变它之前它不会序列化-我不想在这段时间内引起更多的问题)

根据错误,它看起来好像在尝试序列化我的应用程序窗口(StudioWindow),但是,在我的应用程序中找不到对它的任何引用

所以我不是真的要求你们帮我找到参考资料(据我所知,它可能隐藏在我项目的其他部分),但我很想找到一种方法来缩小这个问题。我一直在对部分进行注释,并试图消除项目中的某些部分是问题所在,但这有点困难,因为如果没有其他部分的运行,我的应用程序的某些方面将无法运行

感谢您的帮助。如果你想知道更多的信息,就问吧。我可能会
Imports gE.gEngine
Imports LuaInterface
Imports gE.gEngine.Generic
Imports Microsoft.Xna.Framework
Imports System.Runtime.Serialization
Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Imports gE.gEngine.WorldObjects
Imports System.Windows.Forms
Imports Microsoft.Xna.Framework.Input
Imports System.Threading.Tasks
Imports System.Threading

<Serializable()> _
Public Class Environment

    Public World As WorldServices.World
    Public Players As WorldServices.Players
    Public Lighting As WorldServices.Lighting
    Public Environment As WorldServices.Environment
    Public GenericsLibrary As WorldServices.GenericsLibrary

    <NonSerialized()> _
    Public Lua As New LuaInterface.Lua
    <NonSerialized()> _
    Public SafetyContext As CommonEnum.SafetyContext = CommonEnum.SafetyContext.Safe

    Public Event EnvironmentChanged(ByVal sender As Object, ByVal e As GenericEventArgument)

    Public Sub call_EnvironmentChanged(ByVal sender As Object, ByVal e As GenericEventArgument)
        RaiseEvent EnvironmentChanged(sender, e)
    End Sub

    Public Sub New()
        GlobalShare.Environment = Me
        InitializeLua()

        Lighting = New WorldServices.Lighting()
        World = New WorldServices.World()
        Players = New WorldServices.Players()
        Environment = New WorldServices.Environment()
        GenericsLibrary = New WorldServices.GenericsLibrary()

        Dim ConstraintesGroup As New WorldObjects.Group With {
            .Name = "Constraintes",
            .Parent = GenericsLibrary
        }

        Dim ordsc As List(Of gEngine.WorldObjects.Lua) = Environment.GetScripts()
        ordsc.Sort(Function(x, y) x.RunIndex.CompareTo(y.RunIndex))
        For Each script As gEngine.WorldObjects.Lua In ordsc
            GlobalShare.Environment.RunScript(script.Source, script.Name)
        Next
    End Sub

    Public Sub SetSafetyContext(ByVal e As CommonEnum.SafetyContext)
        If e <> CommonEnum.SafetyContext.Unlocked Then
            SafetyContext = e
            Exit Sub
        End If
        Throw New Exception("Attempt to unlock safety context from unsafe Lua script!")
    End Sub

    Public Sub InitializeLua()
        Lua("Game") = GlobalShare.Game
        Lua("__Environment") = Me
        Lua("gel") = New gELua(Me)
        Lua.RegisterFunction("Print", Lua("gel"), Lua("gel").GetType().GetMethod("Print"))
        Lua.RegisterFunction("print", Lua("gel"), Lua("gel").GetType().GetMethod("Print"))
        Lua.RegisterFunction("SetSafetyContext", Lua("gel"), Me.GetType().GetMethod("SetSafetyContext"))

        Lua("Vector2") = New Lua_.Lua_Vector2
        Lua("Vector3") = New Lua_.Lua_Vector3
        Lua("Mouse") = New Lua_.Lua_Mouse

        Lua.NewTable("Helpers")
        Lua("Helpers.EnumHelper") = New Helpers.EnumHelper

        Dim GameTimer As New Stopwatch
        Lua("Time") = GameTimer
        GameTimer.Start()

        Lua.NewTable("__X")
    End Sub

    Public Function Clone() As Environment
        If [Object].ReferenceEquals(Me, Nothing) Then
            Return Nothing
        End If

        Dim formatter As IFormatter = New BinaryFormatter()
        Dim stream As IO.Stream = New MemoryStream()
        Using stream
            formatter.Serialize(stream, Me)
            stream.Seek(0, SeekOrigin.Begin)
            Return DirectCast(formatter.Deserialize(stream), [Environment])
        End Using
    End Function

    Public Sub RunScript(ByVal scr As String, ByVal name As String)
        Try
            Lua.DoString(scr, name)
        Catch ex As Exception
            Game.CallLogEvent(Me, New GenericEventArgument("LogEvent", "Error", ex.Message))
        End Try
    End Sub

End Class