コード例 #1
0
        /// <summary>
        /// Called by Unity.
        /// </summary>
        public void Start()
        {
            // Create domain
            domain = ScriptDomain.CreateDomain("Example Domain");

            // We will be accessing the compiler service so make sure it is available
            // This is only a demonstration and passing 'true' to the 'CreateDomain' method will ensure that the compiler service is initialized
            if (domain.IsCompilerServiceInitialized == false)
            {
                throw new InvalidOperationException("Compiler service is not initialized");
            }


            // Add a reference to 'System.Core' - 'HashSet<>' is defined in 'System.Core.dll'
            Assembly systemCoreAssembly = typeof(System.Collections.Generic.HashSet <>).Assembly;

            // Add a compiler reference
            domain.RoslynCompilerService.ReferenceAssemblies.Add(AssemblyReference.FromAssembly(systemCoreAssembly));



            // Add a reference to an unloaded assembly via file path
            string referenceAssemblyPath = "path/to/reference/assembly.dll";

            // Add a compiler reference
            domain.RoslynCompilerService.ReferenceAssemblies.Add(AssemblyReference.FromNameOrFile(referenceAssemblyPath));
        }
コード例 #2
0
        /// <summary>
        /// Called by Unity.
        /// </summary>
        public void Start()
        {
            // Create the domain
            domain = ScriptDomain.CreateDomain("MazeCrawlerCode", true);

            // Support netstandard
            try
            {
                IMetadataReferenceProvider reference = AssemblyReference.FromNameOrFile("netstandard");

                if (reference.TryResolveReference() == true)
                {
                    domain.RoslynCompilerService.ReferenceAssemblies.Add(reference);
                }
            }
            catch { }

            // Add assembly reference to Assembly-CSharp
            domain.RoslynCompilerService.ReferenceAssemblies.Add(AssemblyReference.FromAssembly(typeof(MazeCrawlerExample).Assembly));
            domain.RoslynCompilerService.ReferenceAssemblies.Add(AssemblyReference.FromAssembly(typeof(UnityEngine.Object).Assembly));
            domain.RoslynCompilerService.ReferenceAssemblies.Add(AssemblyReference.FromAssembly(typeof(Stack <>).Assembly));
            domain.RoslynCompilerService.ReferenceAssemblies.Add(AssemblyReference.FromAssembly(typeof(HashSet <>).Assembly));

            if (showCompletedCodeOnStartup == true)
            {
                // Load the solution code
                runCrawlerInput.text = mazeCodeSolution.text;
            }
            else
            {
                // Load the template code
                runCrawlerInput.text = mazeCodeTemplate.text;
            }
        }
コード例 #3
0
        // Methods
        /// <summary>
        /// Called by Unity.
        /// </summary>
        public void Start()
        {
            // Create domain
            domain = ScriptDomain.CreateDomain("Example Domain");


            // Compile and load the first batch of source code.
            // The public types in this source code will be accessible to any assemblies that reference it.
            ScriptAssembly assemblyA = domain.CompileAndLoadSource(sourceCodeA, ScriptSecurityMode.UseSettings);

            // Compile and load the second batch of source code.
            // Note that we pass 'assemblyA' as part of the third argument 'additionalAssemblyReferences'. This will allow the code we are compiling to access any public types defined in assemblyA.
            // We could also add many more reference assemblies if required by providing a bigger array of references.
            ScriptAssembly assemblyB = domain.CompileAndLoadSource(sourceCodeB, ScriptSecurityMode.UseSettings, new IMetadataReferenceProvider[] { assemblyA });

            // Call the static method 'SayHello' which will call the method 'LogToConsole' which is defined in assemblyA
            assemblyB.MainType.SafeCallStatic("SayHello");

            // Note that there are many other ways to add assembly references.
            // Any type that implements 'IMetadataAssemblyProvider' can be passed including RoslynCSharp.ScriptAssembly, and RoslynCSharp.Compiler.CompilationResult.
            // You can also use the 'RoslynCSharp.Compiler.AssemblyReference' type to reference other assemblies in a few different ways. All of the following AssemblyReference method calls return an IMetadataReferenceProvider value.

            // Get a metadata reference from a System.Reflection.Assembly which is already loaded. Note that the 'Location' property of the assembly cannot be empty otherwise this will fail.
            AssemblyReference.FromAssembly(typeof(object).Assembly);

            // Get a metadata reference from an assembly image data array. The source array can come from anywhere as long as it is a valid assembly image.
            byte[] bytes = File.ReadAllBytes("C:/Assemblies/MyAssembly.dll");
            AssemblyReference.FromImage(bytes);

            // Get a metadata reference from a System.IO.Stream containing assembly image data.
            Stream assemblyStream = File.OpenRead("C:/Assemblies/MyAssembly.dll");

            AssemblyReference.FromStream(assemblyStream);

            // Get a metadata reference from a loaded assembly with the specified name or an assembly at the specified path. Note that the 'Location' property of the loaded assembly cannot be empty if providing only and assembly name.
            AssemblyReference.FromNameOrFile("mscorlib");
            AssemblyReference.FromNameOrFile("C:/Assemblies/MyAssembly.dll");
        }