如何在.net中重新加载类

让我从头开始,以防万一这比我想象的要简单。 我的问题本质上是这样。我有看起来像这样的代码:     while(true){         睡眠(小睡时间);         do_something_useful();     }

这个while循环存在于一个文件中,我们称它为main.cs。 do_something_useful()的代码位于其他文件中,称为reloadable.cs。 (实际上,我可以将它们放在同一文件中,但我怀疑这样做会有所帮助。)

我想要的是:我应该能够将上面的代码重写为以下形式:     while(true){         睡眠(小睡时间);         reload_code(); //使用魔术?         do_something_useful();     }

然后,如果reloadable.cs被编辑并重新编译,则将在sleep()返回之后获取更改。我不认为可以在Visual Studio中使用“编辑并继续”来完成此技巧,因为那件事要求您在更改之前先处于断点。

根据评论进行编辑:我尝试了此操作,它崩溃了:     课程计划{         静态void Main(){             System.AppDomain域= System.AppDomain.CreateDomain(“ MyDomain”);             byte [] bytes = System.IO.File.ReadAllBytes(@“ C:\ Users \ Mark \ Documents \ Visual Studio 2010 \ Projects \ XnaHero \ reloadable \ bin \ x86 \ Debug \ reloadable.dll”);             domain.Load(bytes);     }     } 错误消息是

System.IO.FileNotFoundException was unhandled
  HResult=-2147024894
  Message=Could not load file or assembly 'reloadable, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
  Source=mscorlib
  FileName=reloadable, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
  FusionLog==== Pre-bind state information ===
LOG: DisplayName = reloadable, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
 (Fully-specified)
LOG: Appbase = file:///C:/Users/Mark/Documents/Visual Studio 2010/Projects/XnaHero/XnaHero/XnaHero/bin/x86/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: No application configuration file found.
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Users/Mark/Documents/Visual Studio 2010/Projects/XnaHero/XnaHero/XnaHero/bin/x86/Debug/reloadable.DLL.
LOG: Attempting download of new URL file:///C:/Users/Mark/Documents/Visual Studio 2010/Projects/XnaHero/XnaHero/XnaHero/bin/x86/Debug/reloadable/reloadable.DLL.
LOG: Attempting download of new URL file:///C:/Users/Mark/Documents/Visual Studio 2010/Projects/XnaHero/XnaHero/XnaHero/bin/x86/Debug/reloadable.EXE.
LOG: Attempting download of new URL file:///C:/Users/Mark/Documents/Visual Studio 2010/Projects/XnaHero/XnaHero/XnaHero/bin/x86/Debug/reloadable/reloadable.EXE.

  StackTrace:
       at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
       at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
       at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
       at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean forIntrospection)
       at System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection)
       at System.Reflection.Assembly.Load(String assemblyString)
       at System.UnitySerializationHolder.GetRealObject(StreamingContext context)
       at System.AppDomain.Load(Byte[] rawAssembly)
       at Program.Main() in C:\Users\Mark\Documents\Visual Studio 2010\Projects\XnaHero\XnaHero\XnaHero\XnaHero.cs:line 143
  InnerException: