Ejemplo n.º 1
0
        private MetadataReference GetReference()
        {
            if (File.Exists(assemblyPath) == false)
            {
                if (assemblyImage != null && assemblyImage.Length > 0)
                {
                    // Create refererence from image
                    return(AssemblyReference.FromImage(assemblyImage).Reference);
                }
            }
            else
            {
                // Create reference from path
                return(AssemblyReference.FromNameOrFile(assemblyPath).Reference);
            }

            throw new Exception("Assembly reference asset is invalid!");
        }
Ejemplo n.º 2
0
    // Start is called before the first frame update
    IEnumerator Start()
    {
        ScriptDomain domain    = ScriptDomain.CreateDomain("MyDomain", true);
        string       source    = ConnectionManager.ToCode(begin.GetComponent <RectTransform> ()) + "}}}";
        string       reference = "UnityEngine.dll";

        using (UnityWebRequest uwr = UnityWebRequest.Get(Application.streamingAssetsPath + "/bin/Data/Managed" + "/" + reference)) {
            yield return(uwr.SendWebRequest());

            if (uwr.isNetworkError || uwr.isHttpError)
            {
                Debug.Log("www error:" + uwr.error + " " + ("jar:file://" + Application.dataPath + "!/assets/" + reference));
            }
            else
            {
                Debug.Log("1");
                domain.RoslynCompilerService.ReferenceAssemblies.Add(AssemblyReference.FromImage(uwr.downloadHandler.data));
                Debug.Log(uwr.downloadHandler.data);
                ScriptType type = domain.CompileAndLoadMainSource(source);
            }
        }
    }
Ejemplo n.º 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");
        }