创建c#dll运行时和加载运行时之后

时间:2015-10-14 22:28:05

标签: c# visual-studio dll unity3d system.reflection

我有这个问题:

我用这段代码创建了一个dll:

public bool PackScripts(string dllPath, string[] scripts)
{
    try
    {
        CodeDomProvider cc = CodeDomProvider.CreateProvider("CSharp");
        CompilerParameters cp = new CompilerParameters();
        var pf = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles);
        cp.ReferencedAssemblies.Add(string.Format(@"{0}\Unity\Editor\Data\Managed\UnityEngine.dll", pf));
        cp.GenerateExecutable = false; //generate executable
        cp.OutputAssembly = dllPath;
        CompilerResults cr = cc.CompileAssemblyFromFile(cp, scripts);
        return cr.Errors.Count == 0;
    }
    catch
    {
        return false;
    }
}

并且工作正常:如果我使用visual studio创建解决方案,请包含创建的dll工作正常!

但我的目标是动态加载此dll。为此我使用此代码:

var assembly = System.Reflection.Assembly.Load("packScripts.dll");

此处出现此错误:

FileNotFoundException: Could not load file or assembly 'C:/Users/Administrator/Documents/Unity/BundlesLoader/Assets/packscripts.dll' or one of its dependencies. The system cannot find the file specified.
System.AppDomain.Load (System.String assemblyString, System.Security.Policy.Evidence assemblySecurity, Boolean refonly) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/AppDomain.cs:746)
System.AppDomain.Load (System.String assemblyString) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/AppDomain.cs:728)
(wrapper remoting-invoke-with-check) System.AppDomain:Load (string)
System.Reflection.Assembly.Load (System.String assemblyString) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/Assembly.cs:576)
Loader+<LoadBundle>c__Iterator0.MoveNext () (at Assets/Scripts/Loader.cs:94)

此外,如果我创建一个新的解决方案并尝试使用相同的代码,它会给出相同的错误.. 任何想法?

提前感谢!

0 个答案:

没有答案