adding custom .dll class library to asp.net/C# website

狂风中的少年 提交于 2019-12-23 02:53:28

问题


I have been working on an asp.net/C# website for a while and decided to put my SQL C# code into a class library (.dll). The problem is, when I try to add it to my website application in vs2010 it sees it, but doesn't at the same time. What I mean is the web application tells you that it is added as a reference under the reference folder, but when compiling the code it will say you don't have the reference.

The way I add the reference is by right clicking the solution and selecting "add reference". I browse for the reference (.dll file) and then add it.

here is the error:

    CS0246: The type or namespace name SQL_lib could not be found (are you missing a using directive or an assembly reference?)

I did add all the references in the web application that the dll file code had so it can't be that... I really don't know whats going on. Thank you in advance :-)

edit:

The only time the web application shows that it cannot find the reference is when it is done compiling and just executed. In the browser it shows the error above. When I go into the code I see no underlined error markings in the code for some reason and I can access the class within the reference. I will further look into it...

Also it has the error in aspx based cs file and plain cs files.


回答1:


I struggled with a similar problem and found my way here. The answers here didn't work for me, but here is what I did to solve it.

I had to add the file to the GAC.

gacutil.exe /i MyFile.dll

Then you can get the version and

gacutil /l MyFile

The Global Assembly Cache contains the following assemblies:

MyFile, Version=6.8.0.6, Culture=neutral, PublicKeyToken=99ttt95b5a5 d634b, processorArchitecture=MSIL

Then you take the values and copy them into the top of your aspx page to Register and then include.




回答2:


Make sure you have added reference to the namespace in which the SQL_lib class is defined:

using Namespace_In_Which_You_Declared_The_SQL_libClass;

Now you could use the SQL_lib class. Also make sure that this class is public:

public class SQL_lib
{
    ...
}



回答3:


Either add a using for your namespace, like this:

using YourNamespaceForSQL_Lib;

or fully qualify the object name in your code, like this:

YourNamespaceForSQL_Lib.YourClass = new YourNamespaceForSQL_Lib.YourClass();

Note: If you have correctly added the reference and you have a class name underlined, then you can click on the class name and press Ctrl + . and it will offer to automatically add the correct using namespace entry in your code file. Visual Studio will reflect across all DLLs in your References and offer multiple namespace choices if they exist.




回答4:


In Visual studio right click on the method or class thats causing the error in the code window and click 'resolve', this will suggest adding the using reference for you.



来源:https://stackoverflow.com/questions/18363932/adding-custom-dll-class-library-to-asp-net-c-website

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!