0

I am using CodeDom/Reflection to compile a C# script from a file. My current code is:

string sourceCode = File.ReadAllText(item);
var compParms = new CompilerParameters
{
    GenerateExecutable = false,
    GenerateInMemory = true
};
var csProvider = new CSharpCodeProvider();
compParms.GenerateExecutable = false;


compParms.OutputAssembly = "agame/temp/dll/common_" + Guid.NewGuid().ToString() + ".dll";

compParms.ReferencedAssemblies.Add("System.Windows.Forms.dll");
compParms.ReferencedAssemblies.Add("System.dll");
compParms.ReferencedAssemblies.Add("System.Windows.Forms.dll");
compParms.ReferencedAssemblies.Add("System.IO.dll");

compParms.ReferencedAssemblies.Add("C:/Users/" + Environment.UserName + "/_ache/external_acs.dll");


compParms.GenerateInMemory = true;

CompilerResults compilerResults = csProvider.CompileAssemblyFromSource(compParms, sourceCode);


object typeInstance = compilerResults.CompiledAssembly.CreateInstance("SomeClass");

MethodInfo mi = typeInstance.GetType().GetMethod("Add42");
int methodOutput = (int)mi.Invoke(typeInstance, new object[] { 1 });

MessageBox.Show(methodOutput.ToString());

An error occurs at the fourth ReferencedAssemblies.Add(), which is where I was trying to reference an external DLL.
My error was: "could not load file or assembly 'dll location' or one of its dependences. The system cannot find the file specified"
I also tried referencing all of the external DLL's dependencies, as is shown below:

compParms.ReferencedAssemblies.Add("C:/Users/" + Environment.UserName + "/_ache/external_acs.dll");

compParms.ReferencedAssemblies.Add("C:/Users/" + Environment.UserName + "/_ache/csfml-audio-2.dll");

compParms.ReferencedAssemblies.Add("C:/Users/" + Environment.UserName + "/_ache/csfml-graphics-2.dll");

compParms.ReferencedAssemblies.Add("C:/Users/" + Environment.UserName + "/_ache/csfml-network-2.dll");

compParms.ReferencedAssemblies.Add("C:/Users/" + Environment.UserName + "/_ache/csfml-system-2.dll");

compParms.ReferencedAssemblies.Add("C:/Users/" + Environment.UserName + "/_ache/csfml-window-2.dll");

compParms.ReferencedAssemblies.Add("C:/Users/" + Environment.UserName + "/_ache/libsndfile-1.dll");

compParms.ReferencedAssemblies.Add("C:/Users/" + Environment.UserName + "/_ache/openal32.dll");

compParms.ReferencedAssemblies.Add("C:/Users/" + Environment.UserName + "/_ache/Otter.dll");

compParms.ReferencedAssemblies.Add("C:/Users/" + Environment.UserName + "/_ache/sfmlnet-audio-2.dll");

compParms.ReferencedAssemblies.Add("C:/Users/" + Environment.UserName + "/_ache/sfmlnet-graphics-2.dll");

compParms.ReferencedAssemblies.Add("C:/Users/" + Environment.UserName + "/_ache/sfmlnet-system-2.dll");

compParms.ReferencedAssemblies.Add("C:/Users/" + Environment.UserName + "/_ache/sfmlnet-window-2.dll");

How do I reference an external DLL by the file path? If referencing it by the file path is not possible, is there a way to reference an external DLL at all? I am using it to allow scripting in my custom C# game engine.
Help with this problem would be greatly appreciated!

5
  • I edited my question to have the error message. My mistake! Commented Dec 4, 2019 at 23:29
  • Why was my question downvoted? Commented Dec 4, 2019 at 23:32
  • Does the file/dll definitely exist at that path? Commented Dec 4, 2019 at 23:34
  • Yes, it exists at that path. Commented Dec 4, 2019 at 23:34
  • 1
    In general full path is supported for the .ReferencedAssemblies.Add... For question to be answerable on SO you need minimal reproducible example which is realistically unlikely for this type of problems... There are plenty of debugging help for this error around - you may start with Assembly.LoadFrom to confirm that assembly you claim is perfect actually can be loaded into your program... Than figure out what dependency is missing... (basically you have to work it out yourself) Commented Dec 5, 2019 at 0:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.