TheNoobieWaffle TheNoobieWaffle - 5 months ago 70
C# Question

How to get Assemblies Generated at Runtime to use Project Dependencies? C#

Is it possible to allow dynamically generated assemblies to access dependencies that exist in the project that generates the new assembly? I am working with Unity and C#, and I add an assembly that contains dependencies that exist in the project it now belongs to, but I get this error: FileNotFoundException: Could not load file or assembly 'ModAssembly000.dll' or one of its dependencies . I get this error because I try to put 'using UnityEngine' at the top of the script. This is the code that already exists in the project that gets the new assembly and invokes a method:

CompilerParameters parameters = new CompilerParameters();
parameters.GenerateExecutable = false;
parameters.GenerateInMemory = true;
parameters.OutputAssembly = generatedName;
CompilerResults r = CodeDomProvider.CreateProvider("CSharp").CompileAssemblyFromFile(parameters, filePath);
r.CompiledAssembly.GetType("ModData").GetMethod("Run").Invoke(null, BindingFlags.Static, null, null, null);

This is the source for the dynamically created assembly:

using UnityEngine;

public class ModData {
public static string modName = "Super kool mod";
public static string modVersion = "1.2.1";

public static void Run() {
Debug.Log("it worked :D");

UnityEngine (thus, Debug.Log) exists in the code that is generating this assembly. Is there a way I can get the new created assembly to use the UnityEngine that exists above it so that I could allow the new code to do anything in the project above it? I know ModAssembly000.dll exists because if I remove the 'using UnityEngine' line, then I can access the static string filds of the dynamic assembly without issue.

Answer Source

My guess is that you need to add Unity to the list of referenced assemblies:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download