コード例 #1
0
        private AppDomain CreateAppDomain()
        {
            Evidence evidence = new Evidence(AppDomain.CurrentDomain.Evidence);

            AppDomain newDomainName = AppDomain.CreateDomain("New Domain", evidence,
                                                             new AppDomainSetup()
            {
                ApplicationName    = "AnimalLoaderAndExecuter",
                ApplicationBase    = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath),
                DynamicBase        = Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath),
                ConfigurationFile  = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile,
                LoaderOptimization = LoaderOptimization.MultiDomainHost,
                PrivateBinPath     = GetPrivateBin(AppDomain.CurrentDomain.SetupInformation.ApplicationBase)
            });

            this._animalLoaderAndExecuter = (AnimalLoaderAndExecuter)newDomainName.CreateInstanceAndUnwrap(
                Assembly.GetExecutingAssembly().FullName, typeof(AnimalLoaderAndExecuter).FullName);
            return(newDomainName);
        }
コード例 #2
0
        public void ExecuteDynamicAssembly()
        {
            string sourceFilePath      = Path.Combine(sourceDirectory, sourceFileName);
            string destinationFilePath = Path.Combine(destinationDirectory, sourceFileName);

            if (File.Exists(destinationFilePath))
            {
                File.SetAttributes(destinationFilePath, FileAttributes.Normal);
            }
            File.Copy(sourceFilePath, destinationFilePath, true);

            AppDomain appDomain = null;

            try
            {
                appDomain = AppDomain.CreateDomain("ProxyDomain");

                _animalLoaderAndExecuter = (AnimalLoaderAndExecuter)appDomain.CreateInstanceAndUnwrap(
                    typeof(AnimalLoaderAndExecuter).Assembly.FullName,
                    typeof(AnimalLoaderAndExecuter).FullName);
                LoadAssembly(FileToByteArray(destinationFilePath));
                var testRefMarshall = _animalLoaderAndExecuter.Execute();

                List <AnimalValue> ojala = testRefMarshall.MarshalledAnimalGameValuesList;

                Console.WriteLine(ojala[0].TextProperty);
            }
            catch (InvalidOperationException ioe)
            {
                Console.WriteLine(ioe);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            finally
            {
                AppDomain.Unload(appDomain);
            }
        }