匿名方法访问局部变量时抛出运行时异常
为什么下面的代码会抛出以下错误?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
C# 编译器生成一个不可见的帮助器类来实现 lambda。您可以在异常消息中看到它的名称
<>c__DisplayClass3
。由于 lambda 在添加的应用程序域中运行,因此需要将该帮助程序类的实例从主域序列化到该应用程序域。这是行不通的,这些辅助类没有 [Serializable] 属性。此处不能使用 lambda,只需在静态辅助函数上使用常规事件分配语法即可。像这样:
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:
这是因为您尝试在
DomainUnload
方法内使用cd
变量,并且该变量是在外部范围中定义的。It's because you are trying to use the
cd
variable inside theDomainUnload
method and this variable is defined in the outside scope.