小编Nic*_* Lu的帖子

使用Mono.cecil修改程序集后,获取"System.IO.FileNotFoundException"错误

为了重新产生问题,我创建了这样一个简单的类,这个文件将被编译为"SourceDLL.dll".

namespace SourceDll
{
    public class Class1
    {
        static public int Add(int b, int c)
        {
            return b + c;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我使用Mono.Cecil修改它,我只是打开并保存回来.

namespace InstrumentSystemDll
{
    class Program
    {
        static void Main(string[] args)
        {
            var fileName = args[0];
            var assembly = AssemblyDefinition.ReadAssembly(string.Format(@"..\..\..\Dependences\{0}.dll", fileName));
            Console.WriteLine(string.Format(@"..\..\..\Dependences\{0}.Patched.dll", fileName));
            assembly.Write(string.Format(@"..\..\..\Dependences\{0}.Patched.dll", fileName));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我有一个修改过的"SourceDll.Patched.dll"文件,然后我尝试使用这个文件.创建了一个consloe应用程序并引用了"SourceDll.Patched.dll".

namespace TestInstrumentDll
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(Class1.Add(1, 1));
        }
    }
Run Code Online (Sandbox Code Playgroud)

}

不幸的是,我有这样的错误:

未处理的异常:System.IO.FileNotFoundException:无法加载文件或包含'SourceDll,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'或其依赖项之一.该系统找不到指定的文件.在TestInstrumentDll.Program.Main(String [] args)

如果我切换到原始SourceDll.dll,它工作并打印一个数字"2",如我们所料.

c# mono .net-assembly mono.cecil

2
推荐指数
1
解决办法
1549
查看次数

标签 统计

.net-assembly ×1

c# ×1

mono ×1

mono.cecil ×1