当我想从另一个项目使用我的类库时,找不到它
我有一个类库,我想将其用作其他项目的参考。但是,当我尝试创建此库中的公共类的实例时,我无法构建该项目。它表示找不到类型或命名空间 ClassLibName。 (您是否缺少 using 指令或程序集引用?)。我检查了参考并再次添加。我也添加了命名空间。您对这个问题有什么建议吗?
I have a class library and I want to use it as a reference from another projects. But, when I try to create an instance of a public class wich in this library, I can't build the project. It says, type or namespace ClassLibName could not be found. (are you missing a using directive or an assembly reference?). I checked reference and add it again. I add namespace too. Do you have any suggestion about this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
确保您尝试创建的类的实例在类库中声明为 public:
然后在客户端项目中添加程序集作为引用,然后
Make sure that instance of the class you are trying to create is declared as public in the class library:
and then inside the client project you add the assembly as reference and then
我的类库和其他项目有不同的目标框架。我将它们设置为.Net Framework4,问题解决了。感谢您的帮助。
My class library and other project had different target framework. I set them to .Net Framework4, the problem is solved. Thank you for your helps.
也许您的库是使用比您的项目更高版本的框架构建的...
如果是这样,您将必须升级您的项目才能构建它(如果可能)...或者降级库!
Maybe your Library is built with a greater version of the framework than your project...
If so, you'll have to upgrade your project to build it if possible... or downgrade the library!
您尝试实例化的类是
internal
吗?这是默认设置:如果删除实例化类的代码,但保留命名空间的 using 指令和程序集的引用,它是否可以编译?如果是这样,那就表明这确实是特定类的问题,而不是您引用库的方式的问题。
Is the class you're trying to instantiate
internal
by any chance? That's the default:If you get rid of the code instantiating the class, but do keep the
using
directive for the namespace and the reference to the assembly, does it compile? If so, that would suggest it really is a problem with the specific class rather than how you're referring to the library.您缺少 using 指令,或者您没有在项目中添加对程序集的引用,或者两者兼而有之。
在解决方案资源管理器中,右键单击“引用”,单击“添加引用”,转到“浏览”,选择其他项目的已编译 dll。
例如,如果您的类位于命名空间
Hello.World
中,您还应该在文件的开头添加。
You are missing an using directive or you didn't add a reference to the assembly in your project, or both.
In solution explorer, right click on References, click Add references, go to Browse, select the compiled dll of the other project.
If, for example, your class is in namespace
Hello.World
, you should also addat the beginning of the file.