匿名方法访问局部变量时抛出运行时异常

发布于 2024-10-08 02:59:56 字数 1770 浏览 2 评论 0原文

为什么下面的代码会抛出以下错误?

private static void CreateNewAppDomain() {
   var cd = AppDomain.CreateDomain("CustomDomain1");
   cd.DomainUnload += (sender, args) => Console.WriteLine("Domain 0 unloading,       sender{0}, args{1} domain {2}", sender, args,cd);
}



System.Runtime.Serialization.SerializationException was unhandled  Message=Type 'CoreConstructs.AppDomainPlay+<>c__DisplayClass3' in assembly 'CoreConstructs, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2642f93804f4bbd8' is not marked as serializable.  Source=mscorlib
  StackTrace:
       at System.AppDomain.add_ProcessExit(EventHandler value)
       at CoreConstructs.AppDomainPlay.CreateNewAppDomain() in C:\work\sampleCode\exploreCsharp\exploreCSharp\ParameterPassing\AppDomainPlay.cs:line 31
       at CoreConstructs.AppDomainPlay.ExploreAppDomain() in C:\work\sampleCode\exploreCsharp\exploreCSharp\ParameterPassing\AppDomainPlay.cs:line 19
       at CoreConstructs.Program.Main(String[] args) in C:\work\sampleCode\exploreCsharp\exploreCSharp\ParameterPassing\Program.cs:line 14
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

Why does the following code throw the following error?

private static void CreateNewAppDomain() {
   var cd = AppDomain.CreateDomain("CustomDomain1");
   cd.DomainUnload += (sender, args) => Console.WriteLine("Domain 0 unloading,       sender{0}, args{1} domain {2}", sender, args,cd);
}



System.Runtime.Serialization.SerializationException was unhandled  Message=Type 'CoreConstructs.AppDomainPlay+<>c__DisplayClass3' in assembly 'CoreConstructs, Version=1.0.0.0, Culture=neutral, PublicKeyToken=2642f93804f4bbd8' is not marked as serializable.  Source=mscorlib
  StackTrace:
       at System.AppDomain.add_ProcessExit(EventHandler value)
       at CoreConstructs.AppDomainPlay.CreateNewAppDomain() in C:\work\sampleCode\exploreCsharp\exploreCSharp\ParameterPassing\AppDomainPlay.cs:line 31
       at CoreConstructs.AppDomainPlay.ExploreAppDomain() in C:\work\sampleCode\exploreCsharp\exploreCSharp\ParameterPassing\AppDomainPlay.cs:line 19
       at CoreConstructs.Program.Main(String[] args) in C:\work\sampleCode\exploreCsharp\exploreCSharp\ParameterPassing\Program.cs:line 14
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

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

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

发布评论

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

评论(2

北恋 2024-10-15 02:59:56

C# 编译器生成一个不可见的帮助器类来实现 lambda。您可以在异常消息中看到它的名称<>c__DisplayClass3。由于 lambda 在添加的应用程序域中运行,因此需要将该帮助程序类的实例从主域序列化到该应用程序域。

这是行不通的,这些辅助类没有 [Serializable] 属性。此处不能使用 lambda,只需在静态辅助函数上使用常规事件分配语法即可。像这样:

       cd.DomainUnload += NewAppDomain_DomainUnload;
    ...

    private static void NewAppDomain_DomainUnload(object sender, EventArgs e) {
        Console.WriteLine("AppDomain {0} unloading", AppDomain.CurrentDomain);
    }

The C# compiler generates an invisible helper class to implement the lambda. You can see its name in the exception message, <>c__DisplayClass3. Since the lambda runs in the added appdomain, the instance of this helper class needs to be serialized from the primary domain to that appdomain.

That cannot work, these helper classes don't have the [Serializable] attribute. You cannot use a lambda here, just use the regular event assignment syntax on a static helper function. Like this:

       cd.DomainUnload += NewAppDomain_DomainUnload;
    ...

    private static void NewAppDomain_DomainUnload(object sender, EventArgs e) {
        Console.WriteLine("AppDomain {0} unloading", AppDomain.CurrentDomain);
    }
流殇 2024-10-15 02:59:56

这是因为您尝试在 DomainUnload 方法内使用 cd 变量,并且该变量是在外部范围中定义的。

It's because you are trying to use the cd variable inside the DomainUnload method and this variable is defined in the outside scope.

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