我搞了几天都没找到答案,所以来问问!
下面是问题的详细描述:
1 D:Program目录下有两个同一级别的子目录:HelloNS,TestNS。在相应目录下有两个CS文件:Hello.cs ,Test.cs。如下图所示:
D:\Program---HelloNS---Hello.cs
|
|--TestNS---Test.cs2 Hello.cs的源代码很简单:
//Hello.cs
using System;
namespace HelloNS{
public class Hello{
public String GetHelloWorld(){
return "hello world!";
}
}
}
3 Test.cs的源代码也很简单:访问上述Hello.cs中类Hello的方法GetHelloWorld()。
//Test.cs
using System;
using HelloNS;
namespace TestNS{
public class Test{
public static void Main(string[] args){
Hello hello=new Hello();
String str=hello.GetHelloWorld();
Console.Write(str);
}
}
}4 好了,现在分别编译:
(1)首先编译Hello.cs,产生Hello.dll文件:
d:\Program\HelloNS>csc /t:library Hello.cs
上述命令会在d:\Program\HelloNS目录中产生Hello.dll文件
(2)然后在d:\Program\TestNS目录中编译产生Test.exe文件:
d:\Program\TestNS>csc /r:"../Hello/Hello.dll" Test.cs
上述命令会在d:\Program\TestNS目录中产生Test.exe文件5 但是问题来了:运行上述d:\Program\TestNS目录中的Test.exe文件,产生错误:
d:\Program\TestNS>Test.exe 报错如下:
“Unhandled Exception :System.IO.FileNotFoundException:Could not load file or assembly 'Hello,Version=0.0.0.0,Culture=neutral,PublicKeyToken=null' or one of its dependencies.系统找不到指定的文件。
at TestNS.Test.Main(String[] args).-------------------------------------------------------
(注意上述命令都是在DOS环境中,不是在Visual Studio 做,当然在Visual Studio中通过引用是可以完成的。同时也要注意不要把Hello.dll拷贝到TestNS目录中 ,当然把Hello.dll拷贝到TestNS 目录中也是能运行成功的,但我不想这样。)
(其实归纳下来就是:在DOS环境中,如何通过CSC命令来解决这样的一个问题:访问任意一个目录中dll文件中的类)
-------------------------------------------------------
下面是问题的详细描述:
1 D:Program目录下有两个同一级别的子目录:HelloNS,TestNS。在相应目录下有两个CS文件:Hello.cs ,Test.cs。如下图所示:
D:\Program---HelloNS---Hello.cs
|
|--TestNS---Test.cs2 Hello.cs的源代码很简单:
//Hello.cs
using System;
namespace HelloNS{
public class Hello{
public String GetHelloWorld(){
return "hello world!";
}
}
}
3 Test.cs的源代码也很简单:访问上述Hello.cs中类Hello的方法GetHelloWorld()。
//Test.cs
using System;
using HelloNS;
namespace TestNS{
public class Test{
public static void Main(string[] args){
Hello hello=new Hello();
String str=hello.GetHelloWorld();
Console.Write(str);
}
}
}4 好了,现在分别编译:
(1)首先编译Hello.cs,产生Hello.dll文件:
d:\Program\HelloNS>csc /t:library Hello.cs
上述命令会在d:\Program\HelloNS目录中产生Hello.dll文件
(2)然后在d:\Program\TestNS目录中编译产生Test.exe文件:
d:\Program\TestNS>csc /r:"../Hello/Hello.dll" Test.cs
上述命令会在d:\Program\TestNS目录中产生Test.exe文件5 但是问题来了:运行上述d:\Program\TestNS目录中的Test.exe文件,产生错误:
d:\Program\TestNS>Test.exe 报错如下:
“Unhandled Exception :System.IO.FileNotFoundException:Could not load file or assembly 'Hello,Version=0.0.0.0,Culture=neutral,PublicKeyToken=null' or one of its dependencies.系统找不到指定的文件。
at TestNS.Test.Main(String[] args).-------------------------------------------------------
(注意上述命令都是在DOS环境中,不是在Visual Studio 做,当然在Visual Studio中通过引用是可以完成的。同时也要注意不要把Hello.dll拷贝到TestNS目录中 ,当然把Hello.dll拷贝到TestNS 目录中也是能运行成功的,但我不想这样。)
(其实归纳下来就是:在DOS环境中,如何通过CSC命令来解决这样的一个问题:访问任意一个目录中dll文件中的类)
-------------------------------------------------------
GAC;同目录;强命名+配置文件
如果对这方面感兴趣,还可以研究一下custom build rules,只有vc++项目里有这个东东。